一、文件形式的邮件
二、HTML形式的邮件
','html','utf-8') <br><br>msg['Subject'] = subject <br><br>smtp = smtplib.SMTP()<br>smtp.connect('smtp.163.com')<br>smtp.login(username, password)<br>smtp.sendmail(sender, receiver, msg.as_string())<br>smtp.quit()
<p><strong>三、带图片的HTML邮件<br></strong></p><div class="codetitle">
<span><u>复制代码</u></span> 代码如下:<div class="codebody" id="code58012">
<br>#!/usr/bin/env python3<br>#coding: utf-8<br>import smtplib<br>from email.mime.multipart import MIMEMultipart<br>from email.mime.text import MIMEText<br>from email.mime.image import MIMEImage <br><br>sender = '***'<br>receiver = '***'<br>subject = 'python email test'<br>smtpserver = 'smtp.163.com'<br>username = '***'<br>password = '***' <br><br>msgRoot = MIMEMultipart('related')<br>msgRoot['Subject'] = 'test message' <br><br>msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img alt="" src="cid:image1"><br>good!','html','utf-8')<br>msgRoot.attach(msgText) <br><br>fp = open('h:\python\1.jpg', 'rb')<br>msgImage = MIMEImage(fp.read())<br>fp.close() <br><br>msgImage.add_header('Content-ID', '')<br>msgRoot.attach(msgImage) <br><br>smtp = smtplib.SMTP()<br>smtp.connect('smtp.163.com')<br>smtp.login(username, password)<br>smtp.sendmail(sender, receiver, msgRoot.as_string())<br>smtp.quit()
<p><strong>四、带附件的邮件<br></strong></p>
<div class="codetitle">
<span><u>复制代码</u></span> 代码如下:<div class="codebody" id="code97533">#!/usr/bin/env python3<br>#coding: utf-8<br>import smtplib<br>from email.mime.multipart import MIMEMultipart<br>from email.mime.text import MIMEText<br>from email.mime.image import MIMEImage <br><br>sender = '***'<br>receiver = '***'<br>subject = 'python email test'<br>smtpserver = 'smtp.163.com'<br>username = '***'<br>password = '***' <br><br>msgRoot = MIMEMultipart('related')<br>msgRoot['Subject'] = 'test message' <br><br>#构造附件<br>att = MIMEText(open('h:\python\1.jpg', 'rb').read(), 'base64', 'utf-8')<br>att["Content-Type"] = 'application/octet-stream'<br>att["Content-Disposition"] = 'attachment; filename="1.jpg"'<br>msgRoot.attach(att) <br><br>smtp = smtplib.SMTP()<br>smtp.connect('smtp.163.com')<br>smtp.login(username, password)<br>smtp.sendmail(sender, receiver, msgRoot.as_string())<br>smtp.quit()
<p><strong>五、群邮件<br></strong></p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/1961">
<img src="https://img.php.cn/upload/ai_manual/001/246/273/68b6d112d0513186.png" alt="Trae国内版">
</a>
<div class="aritcle_card_info">
<a href="/ai/1961">Trae国内版</a>
<p>国内首款AI原生IDE,专为中国开发者打造</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="Trae国内版">
<span>815</span>
</div>
</div>
<a href="/ai/1961" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="Trae国内版">
</a>
</div>
<div class="codetitle">
<span><u>复制代码</u></span> 代码如下:<div class="codebody" id="code78971">#!/usr/bin/env python3<br>#coding: utf-8<br>import smtplib<br>from email.mime.text import MIMEText <br><br>sender = '***'<br>receiver = ['***','****',……]<br>subject = 'python email test'<br>smtpserver = 'smtp.163.com'<br>username = '***'<br>password = '***' <br><br>msg = MIMEText('你好','text','utf-8') <br><br>msg['Subject'] = subject <br><br>smtp = smtplib.SMTP()<br>smtp.connect('smtp.163.com')<br>smtp.login(username, password)<br>smtp.sendmail(sender, receiver, msg.as_string())<br>smtp.quit()<br><br><strong>六、各种元素都包含的邮件<br></strong><div class="codetitle">
<span><u>复制代码</u></span> 代码如下:<div class="codebody" id="code61217">#!/usr/bin/env python3<br>#coding: utf-8<br>import smtplib<br>from email.mime.multipart import MIMEMultipart<br>from email.mime.text import MIMEText<br>from email.mime.image import MIMEImage <br><br>sender = '***'<br>receiver = '***'<br>subject = 'python email test'<br>smtpserver = 'smtp.163.com'<br>username = '***'<br>password = '***' <br><br># Create message container - the correct MIME type is multipart/alternative.<br>msg = MIMEMultipart('alternative')<br>msg['Subject'] = "Link" <br><br># Create the body of the message (a plain-text and an HTML version).<br>text = "Hi!
How are you?
Here is the link you wanted:
http://www.python.org"<br>html = """<br><br> <br>Hi!<br><br> How are you?<br><br> Here is the <a href="http://www.python.org">link</a> you wanted.<br><br> <br><br>""" <br><br># Record the MIME types of both parts - text/plain and text/html.<br>part1 = MIMEText(text, 'plain')<br>part2 = MIMEText(html, 'html') <br><br># Attach parts into message container.<br># According to RFC 2046, the last part of a multipart message, in this case<br># the HTML message, is best and preferred.<br>msg.attach(part1)<br>msg.attach(part2)<br>#构造附件<br>att = MIMEText(open('h:\python\1.jpg', 'rb').read(), 'base64', 'utf-8')<br>att["Content-Type"] = 'application/octet-stream'<br>att["Content-Disposition"] = 'attachment; filename="1.jpg"'<br>msg.attach(att) <br><br>smtp = smtplib.SMTP()<br>smtp.connect('smtp.163.com')<br>smtp.login(username, password)<br>smtp.sendmail(sender, receiver, msg.as_string())<br>smtp.quit()
<p><strong>七、基于SSL的邮件</strong></p>
<p></p>
<div class="codetitle">
<span><u>复制代码</u></span> 代码如下:<div class="codebody" id="code87920">#!/usr/bin/env python3<br>#coding: utf-8<br>import smtplib<br>from email.mime.text import MIMEText<br>from email.header import Header<br>sender = '***'<br>receiver = '***'<br>subject = 'python email test'<br>smtpserver = 'smtp.163.com'<br>username = '***'<br>password = '***' <br><br>msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要<br>msg['Subject'] = Header(subject, 'utf-8') <br><br>smtp = smtplib.SMTP()<br>smtp.connect('smtp.163.com')<br>smtp.ehlo()<br>smtp.starttls()<br>smtp.ehlo()<br>smtp.set_debuglevel(1)<br>smtp.login(username, password)<br>smtp.sendmail(sender, receiver, msg.as_string())<br>smtp.quit()</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号