简单邮件传输协议(SMTP)是一种协议,用于在邮件服务器之间发送电子邮件和路由电子邮件。html
Python提供smtplib
模块,该模块定义了一个SMTP客户端会话对象,可用于使用SMTP或ESMTP侦听器守护程序向任何互联网机器发送邮件。python
这是一个简单的语法,用来建立一个SMTP对象,稍后将演示如何用它来发送电子邮件 -c#
import smtplib smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )
这里是上面语法的参数细节 -服务器
host - 这是运行SMTP服务器的主机。能够指定主机的IP地址或相似yiibai.com
的域名。这是一个可选参数。网络
port - 若是提供主机参数,则须要指定SMTP服务器正在侦听的端口。一般这个端口默认值是:25
。app
local_hostname - 若是SMTP服务器在本地计算机上运行,那么能够只指定localhost
选项。dom
SMTP对象有一个sendmail
的实例方法,该方法一般用于执行邮件发送的工做。它须要三个参数 -yii
sender - 具备发件人地址的字符串。测试
receivers - 字符串列表,每一个收件人一个。ui
message - 做为格式如在各类RFC中指定的字符串。
示例
如下是使用Python脚本发送一封电子邮件的简单方法 -
#!/usr/bin/python3import smtplib sender = 'from@fromdomain.com'receivers = ['to@todomain.com'] message = """From: From Person <from@fromdomain.com> To: To Person <to@todomain.com> Subject: SMTP e-mail test This is a test e-mail message. """try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message) print "Successfully sent email"except SMTPException: print "Error: unable to send email"
在这里,已经发送了一封基本的电子邮件,使用三重引号,请注意正确格式化标题。一封电子邮件须要一个From
,To
和一个Subject
标题,与电子邮件的正文与空白行分开。
要发送邮件,使用smtpObj
链接到本地机器上的SMTP服务器。 而后使用sendmail
方法以及消息,从地址和目标地址做为参数(即便来自和地址在电子邮件自己内,这些并不老是用于路由邮件)。
若是没有在本地计算机上运行SMTP服务器,则可使用smtplib
客户端与远程SMTP服务器进行通讯。除非您使用网络邮件服务(如gmail或Yahoo! Mail),不然您的电子邮件提供商必须向您提供能够提供的邮件服务器详细信息。以腾讯QQ邮箱为例,具体以下:
mail = smtplib.SMTP('smtp.qq.com', 587) # 端口465或587
当使用Python发送邮件信息时,全部内容都被视为简单文本。 即便在短信中包含HTML标签,它也将显示为简单的文本,HTML标签将不会根据HTML语法进行格式化。 可是,Python提供了将HTML消息做为HTML消息发送的选项。
发送电子邮件时,能够指定一个Mime版本,内容类型和发送HTML电子邮件的字符集。
如下是将HTML内容做为电子邮件发送的示例 -
#!/usr/bin/python3import smtplib message = """From: From Person <from@fromdomain.com> To: To Person <to@todomain.com> MIME-Version: 1.0 Content-type: text/html Subject: SMTP HTML e-mail test This is an e-mail message to be sent in HTML format <b>This is HTML message.</b> <h1>This is headline.</h1> """try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message) print "Successfully sent email"except SMTPException: print "Error: unable to send email"
要发送具备混合内容的电子邮件,须要将Content-type标题设置为multipart / mixed。 而后,能够在边界内指定文本和附件部分。
一个边界以两个连字符开始,后跟一个惟一的编号,不能出如今电子邮件的消息部分。 表示电子邮件最终部分的最后一个边界也必须以两个连字符结尾。
所附的文件应该用包(“m”)功能编码,以便在传输以前具备基本的64编码。
首先咱们要知道用python代理登陆qq邮箱发邮件,是须要更改本身qq邮箱设置的。在这里你们须要作两件事情:邮箱开启SMTP功能 、得到受权码。以后咱们来看看如何更改模板代码,实现使用Python登陆QQ邮箱发送QQ邮件。
注意:也可使用其余服务商的 SMTP 访问(QQ、网易、Gmail等)。
使用QQ邮件发送邮件以前如何设置受权码,参考:
http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
4.1.发送一纯文本邮件到指定邮件
#! /usr/bin/env python#coding=utf-8from email.mime.text import MIMETextfrom email.header import Headerfrom smtplib import SMTP_SSL#qq邮箱smtp服务器host_server = 'smtp.qq.com'#sender_qq为发件人的qq号码sender_qq = '7697****@qq.com'#pwd为qq邮箱的受权码pwd = '****kenbb***' ## xh**********bdc#发件人的邮箱sender_qq_mail = '7697****@qq.com'#收件人邮箱receiver = 'yiibai.com@gmail.com'#邮件的正文内容mail_content = '你好,这是使用python登陆qq邮箱发邮件的测试'#邮件标题mail_title = 'Maxsu的邮件'#ssl登陆smtp = SMTP_SSL(host_server)#set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式smtp.set_debuglevel(1) smtp.ehlo(host_server) smtp.login(sender_qq, pwd) msg = MIMEText(mail_content, "plain", 'utf-8') msg["Subject"] = Header(mail_title, 'utf-8') msg["From"] = sender_qq_mail msg["To"] = receiver smtp.sendmail(sender_qq_mail, receiver, msg.as_string()) smtp.quit()
执行上面代码后,登陆接收邮件的邮件账号,这里接收邮件的帐号为:yiibai.com@gmail.com
,登陆 http://gmail.com 应该会看到有接收到邮件以下 -
注意:有时可能被认为是垃圾邮件,若是没有找到可从“垃圾邮件”查找一下。
4.2.给多我的发送邮件
#! /usr/bin/env python#coding=utf-8from email.mime.text import MIMETextfrom email.header import Headerfrom smtplib import SMTP_SSL#qq邮箱smtp服务器host_server = 'smtp.qq.com'#sender_qq为发件人的qq号码sender_qq = '7697****@qq.com'#pwd为qq邮箱的受权码pwd = 'h**********bdc' ## h**********bdc#发件人的邮箱sender_qq_mail = '7697****@qq.com'#收件人邮箱receivers = ['yiibai.com@gmail.com','****su@gmail.com']#邮件的正文内容mail_content = '你好,这是使用python登陆qq邮箱发邮件的测试'#邮件标题mail_title = 'Maxsu的邮件'#ssl登陆smtp = SMTP_SSL(host_server)#set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式smtp.set_debuglevel(1) smtp.ehlo(host_server) smtp.login(sender_qq, pwd) msg = MIMEText(mail_content, "plain", 'utf-8') msg["Subject"] = Header(mail_title, 'utf-8') msg["From"] = sender_qq_mail msg["To"] = Header("接收者测试", 'utf-8') ## 接收者的别名smtp.sendmail(sender_qq_mail, receivers, msg.as_string()) smtp.quit()
执行上面代码后,登陆接收邮件的邮件账号,这里接收邮件的帐号为:yiibai.com@gmail.com
,登陆 http://gmail.com 应该会看到有接收到邮件以下 -
4.3.使用Python发送HTML格式的邮件
Python发送HTML格式的邮件与发送纯文本消息的邮件不一样之处就是将MIMEText中_subtype
设置为html
。代码以下:
#! /usr/bin/env python#coding=utf-8from email.mime.text import MIMETextfrom email.header import Headerfrom smtplib import SMTP_SSL#qq邮箱smtp服务器host_server = 'smtp.qq.com'#sender_qq为发件人的qq号码sender_qq = '7697****@qq.com'#pwd为qq邮箱的受权码pwd = '***bmke********' ###发件人的邮箱sender_qq_mail = '7697****@qq.com'#收件人邮箱receiver = 'yiibai.com@gmail.com'#邮件的正文内容mail_content = "你好,<p>这是使用python登陆qq邮箱发送HTML格式邮件的测试:</p><p><a href='http://www.yiibai.com'>易百教程</a></p>"#邮件标题mail_title = 'Maxsu的邮件'#ssl登陆smtp = SMTP_SSL(host_server)#set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式smtp.set_debuglevel(1) smtp.ehlo(host_server) smtp.login(sender_qq, pwd) msg = MIMEText(mail_content, "html", 'utf-8') msg["Subject"] = Header(mail_title, 'utf-8') msg["From"] = sender_qq_mail msg["To"] = Header("接收者测试", 'utf-8') ## 接收者的别名smtp.sendmail(sender_qq_mail, receiver, msg.as_string()) smtp.quit()
执行上面代码后,登陆接收邮件的邮件账号,这里接收邮件的帐号为:yiibai.com@gmail.com
,登陆 http://gmail.com 应该会看到有接收到邮件以下 -
4.4.Python发送带附件的邮件
要发送带附件的邮件,首先要建立MIMEMultipart()
实例,而后构造附件,若是有多个附件,可依次构造,最后使用smtplib.smtp
发送。
实现代码以下所示 -
#! /usr/bin/env python#coding=utf-8import smtplibfrom email.mime.text import MIMETextfrom email.header import Headerfrom smtplib import SMTP_SSLfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.header import Header#qq邮箱smtp服务器host_server = 'smtp.qq.com'#sender_qq为发件人的qq号码sender_qq = '7697****@qq.com'#pwd为qq邮箱的受权码pwd = '*****mkenb****' ###发件人的邮箱sender_qq_mail = '7697****@qq.com'#收件人邮箱receiver = 'yiibai.com@gmail.com'#邮件的正文内容mail_content = "你好,<p>这是使用python登陆qq邮箱发送HTML格式邮件的测试:</p><p><a href='http://www.yiibai.com'>易百教程</a></p>"#邮件标题mail_title = 'Maxsu的邮件'#邮件正文内容msg = MIMEMultipart()#msg = MIMEText(mail_content, "plain", 'utf-8')msg["Subject"] = Header(mail_title, 'utf-8') msg["From"] = sender_qq_mail msg["To"] = Header("接收者测试", 'utf-8') ## 接收者的别名#邮件正文内容msg.attach(MIMEText(mail_content, 'html', 'utf-8')) # 构造附件1,传送当前目录下的 test.txt 文件att1 = MIMEText(open('attach.txt', 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream'# 这里的filename能够任意写,写什么名字,邮件中显示什么名字att1["Content-Disposition"] = 'attachment; filename="attach.txt"'msg.attach(att1) # 构造附件2,传送当前目录下的 runoob.txt 文件att2 = MIMEText(open('yiibai.txt', 'rb').read(), 'base64', 'utf-8') att2["Content-Type"] = 'application/octet-stream'att2["Content-Disposition"] = 'attachment; filename="yiibai.txt"'msg.attach(att2)#ssl登陆smtp = SMTP_SSL(host_server)#set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式smtp.set_debuglevel(1) smtp.ehlo(host_server) smtp.login(sender_qq, pwd) smtp.sendmail(sender_qq_mail, receiver, msg.as_string()) smtp.quit()
执行上面代码后,登陆接收邮件的邮件账号,这里接收邮件的帐号为:yiibai.com@gmail.com
,登陆 http://gmail.com 应该会看到有接收到邮件以下 -
4.5.在 HTML 文本中添加图片
邮件的HTML文本中通常邮件服务商添加外链是无效的,因此要发送带图片的邮件内容,能够参考下面的实例代码实现:
#! /usr/bin/env python#coding=utf-8import smtplibfrom email.mime.text import MIMETextfrom email.header import Headerfrom smtplib import SMTP_SSLfrom email.mime.image import MIMEImagefrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMEText#qq邮箱smtp服务器host_server = 'smtp.qq.com'#sender_qq为发件人的qq号码sender_qq = '7697****3@qq.com'#pwd为qq邮箱的受权码pwd = 'h******mk*****' ##发件人的邮箱sender_qq_mail = '7697****3@qq.com'#收件人邮箱receiver = ['yiibai.com@gmail.com','h****u@qq.com']#邮件的正文内容mail_content = ""#邮件标题mail_title = 'Maxsu的邮件'#邮件正文内容#msg = MIMEMultipart()msg = MIMEMultipart('related')
msg["Subject"] = Header(mail_title, 'utf-8')
msg["From"] = sender_qq_mail
msg["To"] = Header("接收者测试", 'utf-8') ## 接收者的别名msgAlternative = MIMEMultipart('alternative')
msg.attach(msgAlternative)#邮件正文内容mail_body = """
<p>你好,Python 邮件发送测试...</p>
<p>这是使用python登陆qq邮箱发送HTML格式和图片的测试邮件:</p>
<p><a href='http://www.yiibai.com'>易百教程</a></p>
<p>图片演示:</p>
<p></p>
"""#msg.attach(MIMEText(mail_body, 'html', 'utf-8'))msgText = (MIMEText(mail_body, 'html', 'utf-8'))
msgAlternative.attach(msgText)
# 指定图片为当前目录fp = open('my.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
# 定义图片 ID,在 HTML 文本中引用msgImage.add_header('Content-ID', '<send_image>')
msg.attach(msgImage)#ssl登陆smtp = SMTP_SSL(host_server)#set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式smtp.set_debuglevel(1)
smtp.ehlo(host_server)
smtp.login(sender_qq, pwd)
smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
smtp.quit() 手动叉车
执行上面代码后,登陆接收邮件的邮件账号,这里接收邮件的帐号为:yiibai.com@gmail.com
,登陆 http://gmail.com 应该会看到有接收到邮件以下 -