Python3 notes

简介: Python3 notes

在 HTML 文本中添加图片

邮件的 HTML 文本中一般邮件服务商添加外链是无效的,正确添加图片的实例如下所示:

实例

#!/usr/bin/python3

 

import smtplib

from email.mime.image import MIMEImage

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.header import Header

 

sender = 'from@runoob.com'

receivers = ['429240967@qq.com']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

 

msgRoot = MIMEMultipart('related')

msgRoot['From'] = Header("菜鸟教程", 'utf-8')

msgRoot['To'] =  Header("测试", 'utf-8')

subject = 'Python SMTP 邮件测试'

msgRoot['Subject'] = Header(subject, 'utf-8')

 

msgAlternative = MIMEMultipart('alternative')

msgRoot.attach(msgAlternative)

 

 

mail_msg = """

Python 邮件发送测试...


图片演示:

"""

msgAlternative.attach(MIMEText(mail_msg, 'html', 'utf-8'))

 

# 指定图片为当前目录

fp = open('test.png', 'rb')

msgImage = MIMEImage(fp.read())

fp.close()

 

# 定义图片 ID,在 HTML 文本中引用

msgImage.add_header('Content-ID', '')

msgRoot.attach(msgImage)

 

try:

   smtpObj = smtplib.SMTP('localhost')

   smtpObj.sendmail(sender, receivers, msgRoot.as_string())

   print ("邮件发送成功")

except smtplib.SMTPException:

   print ("Error: 无法发送邮件")

$ python3 test.py  

邮件发送成功

相关文章
|
5月前
|
Linux Apache Python
Python3 notes
Python3 notes
|
12月前
|
安全 Python
Python3 notes
Python3 notes
|
12月前
|
Python
Python3 notes
Python3 notes
|
12月前
|
Linux Python
Python3 notes
Python3 notes
|
索引 Python
|
存储 数据可视化 API
70个注意的Python小Notes
Python读书笔记:70个注意的小Notes 作者:白宁超 2018年7月9日10:58:18 摘要:在阅读python相关书籍中,对其进行简单的笔记纪要。旨在注意一些细节问题,在今后项目中灵活运用,并对部分小notes进行代码标注。
1321 0
|
Python C语言 .NET
Python chapter 8 learning notes
版权声明:本文为博主原创文章,原文均发表自http://www.yushuai.me。未经允许,禁止转载。 https://blog.csdn.net/davidcheungchina/article/details/78267298 ...
952 0
|
Python
Python chapter 2&3 learning notes
版权声明:本文为博主原创文章,原文均发表自http://www.yushuai.me。未经允许,禁止转载。 https://blog.csdn.net/davidcheungchina/article/details/78243401 方法是Python对数据执行的操作。
1312 0
|
Python
Python chapter 4 learning notes
版权声明:本文为博主原创文章,原文均发表自http://www.yushuai.me。未经允许,禁止转载。 https://blog.csdn.net/davidcheungchina/article/details/78243661 1.
975 0