您的位置:首页 > 编程语言 > Python开发

Python实现邮件发送

2018-01-08 10:31 363 查看
使用smtplib模块发送邮件,它对smtp协议进行了简单的封装。
smtp协议的基本命令包括:

HELO 向服务器标识用户身份

MAIL 初始化邮件传输 mail from:

RCPT 标识单个的邮件接收人;常在MAIL命令后面,可有多个rcpt to:

DATA 在单个或多个RCPT命令后,表示所有的邮件接收人已标识,并初始化数据传输,以.结束

VRFY 用于验证指定的用户/邮箱是否存在;由于安全方面的原因,服务器常禁止此命令

EXPN 验证给定的邮箱列表是否存在,扩充邮箱列表,也常被禁用

HELP 查询服务器支持什么命令

NOOP 无操作,服务器应响应OK

QUIT 结束会话

RSET 重置会话,当前传输被取消

MAIL FROM 指定发送者地址

RCPT TO 指明的接收者地址

一般smtp会话有两种方式,一种是邮件直接投递,就是说,比如你要发邮件給zzz@163.com,那就直接连接163.com的邮件服务器,把信投給zzz@163.com; 另一种是验证过后的发信,它的过程是,比如你要发邮件給zzz@163.com,你不是直接投到163.com,而是通过自己在sina.com的另一个邮箱来发。这样就要先连接sina.com的smtp服务器,然后认证,之后在把要发到163.com的信件投到sina.com上,sina.com会帮你把信投递到163.com。

文件形式的邮件
#!/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('<html><h1>你好</h1></html>','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.<br><img src="cid:image1"><br>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', '<image1>')

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()

也可使用yagmail模块来快速实现
实现代码如下:
import yagmail
yag = yagmail.SMTP(user='XXX@gmail.com',password='XXX'
yag.send(to = 'XXX@qq.com',subject ='test',contents = 'This is a test e-mail from Windows CMD tools with the yagmai')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python smtplib yagmail