一、邮件分析
此处发送邮件使用smtplib模块,不用下载,pycharm自带的有此模块。 我们通过实例化 smtplib 模块的 SMTP 对象 smtpObj 来连接到 SMTP 访问,并使用 sendmail 方法来发送信息。语法如下:
SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options]
参数说明: from_addr: 邮件发送者地址。 to_addrs: 字符串列表,邮件发送地址。 msg: 发送消息 这里要注意一下第三个参数,msg是字符串,表示邮件。我们知道邮件一般由标题,发信人,收件人,邮件内容,附件等构成,发送邮件的时候,要注意msg的格式。这个格式就是smtp协议中定义的格式。
邮件有发送方sender、接收方receivers。一个邮件有需要三个头部信息: From, To, 和 Subject ,每个信息直接使用空行分割。
二、发送纯文本邮件
代码如下: 第一种
import smtplib
from email.mime.text import MIMEText
from email.header import Header
message = MIMEText('我来验证!')
message['From'] = Header('小爱')
message['To'] = Header('小蓝枣')
message['Subject'] = Header('来自异世界的一封信!')
mail = smtplib.SMTP()
mail.connect("smtp.qq.com")
mail.login("接收方邮箱@qq.com", "授权码")
mail.sendmail("发送方邮箱@qq.com", ["接收方邮箱@qq.com"], message.as_string())
关于授权码的问题,需要网上登录qq邮箱,设置中进入自己账户,打开SMIP服务。 黄色的消息框中有生成授权码,需要向腾讯科技发个信息来验证一下,再运行代码,即可收到短信。
第二种 和第一种几乎一样。
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = '发送方邮箱@qq.com'
receivers = ['接收方邮箱@qq.com']
message = MIMEText('再验证一下SMIP模块', 'plain', 'utf-8')
message['From'] = Header("张", 'utf-8')
message['To'] = Header("艳", 'utf-8')
subject = 'Python SMTP 邮件测试'
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP()
smtpObj.connect("smtp.qq.com")
smtpObj.login("接收方邮箱@qq.com", "cyxacyjvmrtxcjbe")
smtpObj.sendmail(sender, receivers, message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("Error: 无法发送邮件")
结果运行成功。 最终手机上收到了两份邮件。
三、发送html格式邮件
注意修改语句message = MIMEText(mail_msg, 'html', 'utf-8') ,传输的是html文件。代码如下:
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = '发送方邮箱@qq.com'
receivers = ['接收方邮箱@qq.com']
mail_msg = """
<p>Python 邮件发送测试...</p>
<p><a href="http://www.runoob.com">这是一个链接</a></p>
"""
message = MIMEText(mail_msg, 'html', 'utf-8')
message['From'] = Header("张", 'utf-8')
message['To'] = Header("艳", 'utf-8')
subject = 'Python SMTP 发送html格式邮件测试'
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP()
smtpObj.connect("smtp.qq.com")
smtpObj.login("接收方邮箱@qq.com", "udfhrmjpxyhcdbbf")
smtpObj.sendmail(sender, receivers, message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("Error: 无法发送邮件")
四、发送html链接加附件邮件
传递两个附件,第一个是txt文件,第二个是png图片。先把png图片放到运行项目文件路径下。 利用MIMEMultipart() 作附件对象。 代码如下:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
sender = '发送方邮箱@qq.com'
receivers = ['接收方邮箱@qq.com']
message = MIMEMultipart()
message['From'] = Header("张", 'utf-8')
message['To'] = Header("艳", 'utf-8')
subject = 'Python SMTP html格式加附件邮件测试'
message['Subject'] = Header(subject, 'utf-8')
mail_msg = """
<p>Python 邮件发送测试...</p>
<p><a href="http://www.runoob.com">这是一个链接</a></p>
"""
message.attach(MIMEText(mail_msg, 'html', 'utf-8'))
a=open('zy5.txt', 'rb').read()
att1 = MIMEText(a, 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="zy5.txt"'
message.attach(att1)
b=open('1.png', 'rb').read()
att2 = MIMEText(b, 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="1.png"'
message.attach(att2)
try:
smtpObj = smtplib.SMTP()
smtpObj.connect("smtp.qq.com")
smtpObj.login('接收方邮箱@qq.com', 'udfhrmjpxyhcdbbf')
smtpObj.sendmail(sender, receivers, message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("Error: 无法发送邮件")
结果如下:
五、发送html图片、链接加附件邮件
插入上面作为附件的1.png图片,需要在邮件正文内容中添加捎带html图片的代码:
mail_msg = """
<p>Python 邮件发送测试...</p>
<p><a href="http://www.runoob.com">这是一个链接</a></p>
<p>图片演示:</p>
<p><img src="cid:image1"></p>#这里关于图片的名字不用做任何修改,会有下面的代码进行读取1.png
"""
还需要读取1.png图片内容:
fp = open('1.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<image1>')
message.attach(msgImage)
整体代码如下:
import smtplib
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
sender = '3268743433@qq.com'
receivers = ['3268743433@qq.com']
message = MIMEMultipart()
message['From'] = Header("张", 'utf-8')
message['To'] = Header("艳", 'utf-8')
subject = 'Python SMTP html格式加附件邮件测试'
message['Subject'] = Header(subject, 'utf-8')
mail_msg = """
<p>Python 邮件发送测试...</p>
<p><a href="http://www.runoob.com">这是一个链接</a></p>
<p>图片演示:</p>
<p><img src="cid:image1"></p>
"""
message.attach(MIMEText(mail_msg, 'html', 'utf-8'))
a=open('zy5.txt', 'rb').read()
att1 = MIMEText(a, 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="zy5.txt"'
message.attach(att1)
b=open('1.png', 'rb').read()
att2 = MIMEText(b, 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="1.png"'
message.attach(att2)
fp = open('1.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<image1>')
message.attach(msgImage)
try:
smtpObj = smtplib.SMTP()
smtpObj.connect("smtp.qq.com")
smtpObj.login('3268743433@qq.com', 'udfhrmjpxyhcdbbf')
smtpObj.sendmail(sender, receivers, message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("Error: 无法发送邮件")
实现效果如图:
|