python 七种邮件内容发送方法实例
时间:2022-01-13 21:06:38
一、文件形式的邮件
#!/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('</pre>
<h1>你好</h1>
<pre>','html','utf-8')
msg['Subject'] = subject
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.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'test message'
msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.
<img alt="" src="cid:image1" />
good!','html','utf-8')
msgRoot.attach(msgText)
fp = open('h:\\python\\1.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '')
msgRoot.attach(msgImage)
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()
四、带附件的邮件
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'test message'
#构造附件
att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="1.jpg"'
msgRoot.attach(att)
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()
五、群邮件
#!/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('你好','text','utf-8')
msg['Subject'] = subject
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
六、各种元素都包含的邮件
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
Hi!
How are you?
Here is the <a href="http://www.python.org">link</a> you wanted.
"""
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
#构造附件
att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="1.jpg"'
msg.attach(att)
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
七、基于SSL的邮件
#!/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.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.set_debuglevel(1)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
标签:python,邮件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
楼层数横排比竖排好
2008-04-26 07:28:00
![](https://img.aspxhome.com/file/UploadPic/20084/26/200842673425574s.jpg)
js实现浏览器倒计时跳转页面效果
2023-07-18 06:49:57
![](https://img.aspxhome.com/file/2023/5/55915_0s.jpg)
Python基础 while循环与break、continue关键字
2023-11-19 03:41:42
![](https://img.aspxhome.com/file/2023/3/64283_0s.jpg)
python arcpy练习之面要素重叠拓扑检查
2021-12-24 12:41:42
![](https://img.aspxhome.com/file/2023/7/81797_0s.jpg)
Python3 实现随机生成一组不重复数并按行写入文件
2021-11-25 18:05:35
菜鸟来看看:制作个人主页有诀窍
2007-10-05 09:03:00
ASP Recordset 分页显示数据的方法(修正版)
2011-04-10 10:42:00
MS Server和Oracle中对NULL处理的一些细节差异
2009-06-10 17:35:00
Python依赖包迁移到断网环境操作
2021-06-27 06:58:29
详细讲解如何为MySQL数据库添加新函数
2008-11-27 17:06:00
JS代码混淆加密工具
2008-05-25 13:49:00
python判断一个变量是否已经设置的方法
2022-06-09 19:33:15
基于python+selenium自动健康打卡的实现代码
2022-04-28 06:31:28
![](https://img.aspxhome.com/file/2023/0/90780_0s.png)
利用Python实现端口扫描器的全过程
2021-07-08 01:25:32
![](https://img.aspxhome.com/file/2023/7/90517_0s.gif)
python 文本单词提取和词频统计的实例
2022-10-25 04:53:03
无序列表 li ul
2008-07-29 13:00:00
Python中关于浮点数的冷知识
2023-11-13 14:01:57
使用 Python 处理3万多条数据只要几秒钟
2023-08-03 15:21:06
![](https://img.aspxhome.com/file/2023/6/60296_0s.jpg)
python爬虫 urllib模块url编码处理详解
2021-09-13 02:24:37
asp的分词技术
2007-08-25 17:50:00