美文网首页Python
Python—Email

Python—Email

作者: 八戒无戒 | 来源:发表于2020-04-19 20:42 被阅读0次

Python自动发送邮件,使用到python内置库email和 smtplib协议库。

"""
(1) MIMEMultipart类型基本格式
● MIMEMultipart(‘mixed’)类型
如果一封邮件中含有附件,那邮件的中必须定义multipart/mixed类型,邮件通过multipart/mixed
类型中定义的boundary标识将附件内容同邮件其它内容分成不同的段。基本格式如下:
msg=MIMEMultipart(‘mixed’)

● MIMEMultipart(‘alternative’)类型
MIME邮件可以传送超文本内容,但出于兼容性的考虑,一般在发送超文本格式内容的同时会同时发送一
个纯文本内容的副本,如果邮件中同时存在纯文本和超文本内容,则邮件需要在Content-Type域中定义
multipart/alternative类型,邮件通过其boundary中的分段标识将纯文本、超文本和邮件的其它内
容分成不同的段。基本格式如下:
msg=MIMEMultipart(‘alternative’)

● MIMEMultipart(‘related’)类型
MIME邮件中除了可以携带各种附件外,还可以将其它内容以内嵌资源的方式存储在邮件中。比如我们在
发送html格式的邮件内容时,可能使用图像作为 html的背景,html文本会被存储在alternative段中,
而作为背景的图像则会存储在multipart/related类型定义的段中。基本格式如下:
msg=MIMEMultipart(‘related’)
"""
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage

file_img = r"C:\Users\bingo\Desktop\The crawler\1.jpg"
file_xml = r"C:\Users\bingo\Desktop\The crawler\out.xml"
file_js = r"C:\Users\bingo\Desktop\The crawler\a.js"

qq_email = "1126878602@qq.com"
qq_smtp_no = "klsnipdnjptohade"    # 不是邮箱密码,是IMAP/SMTP或者POP3/SMTP服务授权码,需自己开通
receive_email = "bingyl123@163.com"

# 加密,一般为465端口
# server = smtplib.SMTP_SSL("smtp.qq.com", 465)
# # 不加密,明文传输
server = smtplib.SMTP("smtp.qq.com", 25)
# 登陆服务器
server.login(qq_email, qq_smtp_no)

# 实例一个总的邮件对象,声明邮件主题,收件人发件人信息
msg_root = MIMEMultipart("related")                           # 声明有见类型
msg_root["From"] = Header("Bingo<{}>".format(qq_email))       # 自定义邮件发送人
msg_root["To"] = Header(receive_email)                        # 自定义有见邮件接收人
msg_root["Subject"] = Header("测试邮件Test Email", "utf8")     # 自定义邮件主题

# 文本消息
# msg_root.attach(MIMEText("你好,请查收附件"))
# msg_txt = MIMEText("你好,请查收附件", "plain", "utf-8")
# msg_root.attach(msg_txt)

# 文本附件
f_obj = open(file_xml, "rb")
msg_xml = MIMEText(f_obj.read(), "base64", "utf-8")
f_obj.close()
msg_xml["Content-Type"] = 'application/octet-stream'
msg_xml['Content-Disposition']='attachment; filename="out.xml"'
msg_root.attach(msg_xml)

f_obj = open(file_js, "rb")
msg_js = MIMEText(f_obj.read(), "base64", "utf-8")
f_obj.close()
msg_js["Content-Type"] = 'application/octet-stream'
msg_js['Content-Disposition']='attachment; filename="a.js"'
msg_root.attach(msg_js)

# ++++++++++++ 超文本+图片显示 +++++++++++++++++++++
msg_alternative = MIMEMultipart("alternative")
mail_msg = """
<p>你好:</p>
<p>&emsp;&emsp;请查收附件!</p>
<p><a href="http://www.baidu.com">百度一下,你就知道</a></p>
<p>图片演示:</p>
<p><img src="cid:image1" width=500 height=300></p>
"""
msg_html = MIMEText(mail_msg, "html", "utf-8")
msg_alternative.attach(msg_html)
msg_root.attach(msg_alternative)


f_obj = open(file_img, "rb")
msg_img = MIMEImage(f_obj.read())
f_obj.close()
msg_img.add_header("Content-ID", "image1")
msg_root.attach(msg_img)
# ++++++++++++++++++++++++++++++++++++++++++++++++++

# 图片附件
f_obj = open(file_img, "rb")
msg_img = MIMEImage(f_obj.read(), "image")
f_obj.close()
msg_img["Content-Type"] = 'application/octet-stream'
msg_img['Content-Disposition']='attachment; filename="111.jpg"'
msg_root.attach(msg_img)

res = server.sendmail(qq_email, [receive_email], msg_root.as_string())
print("邮件发送成...")
server.quit()                    # 关闭服务 

运行结果:

G:\Anaconda\python.exe "C:/Users/bingo/Desktop/The crawler/学习/邮件.py"
邮件发送成...
Process finished with exit code 0

接收到的邮件:

示例.png

相关文章

  • 即用即弃型邮箱

    python源码 使用示例: email = Email() 构造邮箱类 email = Email(proxy=...

  • Python—Email

    Python自动发送邮件,使用到python内置库email和 smtplib协议库。 运行结果: G:\Ana...

  • python sendmail

    http://www.runoob.com/python/python-email.html更多查看:https:...

  • 报错 No module named 'email.utils'

    运行python程序报错 :import email.utilsModuleNotFoundError: No ...

  • 2020-03-09 sendmail

    python发邮件简单三步,python发邮件python-email.htmlpython smtp邮件pyth...

  • 邮件发送

    python书写的邮件发送程序,使用到了email和smtplib两个库,email需要pip insta...

  • python_email

    发送邮件问题网上案例很多,基本的都差不多,这是我的代码: 虽说都差不多,但是这里认为主要有几个需要注意的地方: 1...

  • python send email

    windows下运行 python .\send_mail.py .\primer1.txt 注:primer1....

  • python—smtplib、email群发邮件

    使用python内置包、模块smtplib、email实现自动化、集群式发邮件,其中email负责构造邮件,smt...

  • python 邮件模块

    Python内置对SMTP的支持,其中细化为smtplib和email两个模块,email负责构造邮件,smtpl...

网友评论

    本文标题:Python—Email

    本文链接:https://www.haomeiwen.com/subject/wahibhtx.html