python 七种邮件内容发送方法实例

php中文网
发布: 2016-06-16 08:44:26
原创
1319人浏览过

一、文件形式的邮件

复制代码 代码如下:
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header

sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8',单字节字符不需要
msg['Subject'] = Header(subject, 'utf-8')

smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

二、HTML形式的邮件

复制代码 代码如下:

#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText

sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'

msg = MIMEText('

你好


','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在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号