学习地址:http://blog.szwyll.com/archives/146
1.登录邮箱
# 定义一些常量
# 收件人email, 发件人email,登录邮箱的授权码(mail_password)
server = smtplib.SMTP()
server.connect('smtp.qq.com', 25)
server.login(mail_name, mail_password)
2.发送邮件
# 定义邮件内容
# 纯文本邮件,html文件,包含附件的文件
# 邮件的必须内容:From:发件人email To: 收件人email(多个收件人用,隔开) Subject: 邮件主题
发送邮件
server.sendmail(mail_name, receiver, msg.as_string())
server.quit()
一、发送纯文本文件
import smtplib from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import Header receiver = ['xxx@qq.com'] mail_name = 'xxxx@qq.com' # 发送人邮箱地址 mail_password = 'pqymqfvvlmdabcbc' # 注意:这里不是密码,而应该填写授权码!! # 编辑邮件内容 # 1.收件人,主题,正文 msg = MIMEText('hello, send by Python...', 'plain', 'utf-8') msg['From'] = mail_name msg['To'] = ''.join(receiver) msg['Subject'] = Header('来自SMTP的问候...', 'utf-8').encode() # 发送内容 try: # 登陆邮箱 server = smtplib.SMTP() server.connect('smtp.qq.com', 25) server.login(mail_name, mail_password) # 发送邮件 server.sendmail(mail_name, receiver, msg.as_string()) server.quit() print('Success') except smtplib.SMTPException as e: print('Failure:', str(e))
二、发送HTML邮件
import smtplib from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import Header receiver = ['xxx@qq.com'] mail_name = 'xxxx@qq.com' # 发送人邮箱地址 mail_password = 'pqymqfvvlmdabcbi' # 注意:这里不是密码,而应该填写授权码!! # 编辑邮件内容 # 1.收件人,主题,正文 msg = MIMEText('<html>' '<body>' '<h1>Hello</h1>' '<p>send by ' '<a href="http://www.baidu.com">baidu</a>' '...' '</p></body></html>', 'html', 'utf-8') msg['From'] = mail_name msg['To'] = ''.join(receiver) msg['Subject'] = Header('来自SMTP的问候...', 'utf-8').encode() # 发送内容 try: # 登陆邮箱 server = smtplib.SMTP() server.connect('smtp.qq.com', 25) server.login(mail_name, mail_password) # 发送邮件 server.sendmail(mail_name, receiver, msg.as_string()) server.quit() print('Success') except smtplib.SMTPException as e: print('Failure:', str(e))
三、发送附件邮件
import smtplib from email import encoders from email.mime.application import MIMEApplication from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import Header receiver = ['xxx@qq.com'] mail_name = 'xxxx@qq.com' # 发送人邮箱地址 mail_password = 'pqymqfvvlmdabcbc' # 注意:这里不是密码,而应该填写授权码!! # 编辑邮件内容 # 1.收件人,主题,正文 # 邮件本身(MIMEMultipart) = 邮件正文(MIMEText) + MIMEBase(附件) # msg = MIMEMultipart() # 定义发件人,收件人,邮件主题 msg['From'] = mail_name msg['To'] = ''.join(receiver) msg['Subject'] = Header('来自SMTP的问候...', 'utf-8').encode() # 添加邮件正文 msg.attach(MIMEText('send with file...', 'plain', 'utf-8')) # 添加附件 with open('F:\python_requests\myU.jpg', 'rb') as f: # 设置附件的MIME和文件名 mime = MIMEBase('image', 'jpg', filename='myU.jpg') # 加上必要的头信息: mime.add_header('Content-Disposition', 'attachment', filename='test.png') mime.add_header('Content-ID', '<0>') mime.add_header('X-Attachment-Id', '0') # 读取附件内容 mime.set_payload(f.read()) # 用Base64编码 encoders.encode_base64(mime) # 添加到MIMEMultipart msg.attach(mime) try: # 登陆邮箱 server = smtplib.SMTP() server.connect('smtp.qq.com', 25) server.login(mail_name, mail_password) # 发送邮件 server.sendmail(mail_name, receiver, msg.as_string()) server.quit() print('Success') except smtplib.SMTPException as e: print('Failure:', str(e))
四、再三的基础上将附件嵌入到正文
import smtplib from email import encoders from email.mime.application import MIMEApplication from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import Header receiver = ['xxx@qq.com'] mail_name = 'xxxx@qq.com' # 发送人邮箱地址 mail_password = 'pqymqfvvlmdabcbc' # 注意:这里不是密码,而应该填写授权码!! # 编辑邮件内容 # 1.收件人,主题,正文 # 邮件本身(MIMEMultipart) = 邮件正文(MIMEText) + MIMEBase(附件) # alternative:利用MIMEMultipart就可以组合一个HTML和Plain,要注意指定subtype是alternative: # 如果收件人无法查看HTML格式的邮件,就可以自动降级查看纯文本邮件。 msg = MIMEMultipart('alternative') # 定义发件人,收件人,邮件主题 smtp_server = 'smtp.qq.com' smtp_port = 587 msg['From'] = mail_name msg['To'] = ''.join(receiver) msg['Subject'] = Header('来自SMTP的问候...', 'utf-8').encode() # 添加邮件正文 msg.attach(MIMEText('hello', 'plain', 'utf-8')) msg.attach(MIMEText('<html><body><h1>Hello</h1>' + '<p><img src="cid:0"></p>' + '</body></html>', 'html', 'utf-8')) # 添加附件 with open('F:\python_requests\myU.jpg', 'rb') as f: # 设置附件的MIME和文件名 mime = MIMEBase('image', 'jpg', filename='myU.jpg') # 加上必要的头信息: mime.add_header('Content-Disposition', 'attachment', filename='test.png') mime.add_header('Content-ID', '<0>') mime.add_header('X-Attachment-Id', '0') # 读取附件内容 mime.set_payload(f.read()) # 用Base64编码 encoders.encode_base64(mime) # 添加到MIMEMultipart msg.attach(mime) try: # 登陆邮箱 server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() # server = smtplib.SMTP() # server.connect('smtp.qq.com', 25) server.login(mail_name, mail_password) # 发送邮件 server.sendmail(mail_name, receiver, msg.as_string()) server.quit() print('Success') except smtplib.SMTPException as e: print('Failure:', str(e))