python语言实现发邮件功能
2018-01-08 11:49
162 查看
HTML形式的邮件
带图片的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 from email.header import Header sender = '***' receiver = '***' subject = 'python email test' smtpserver = 'smtp.163.com' username = '***' password = '***' msg = MIMEText('你好','text','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()
相关文章推荐
- 使用python简单实现邮件群发的功能
- python实现邮件发送功能
- 使用python 3实现发送邮件功能
- linux , python 发送邮件,基本实现了mutt常用功能
- python实现SMTP邮件发送功能
- python发送邮件功能实现代码
- Python实现读取邮箱中的邮件功能示例【含文本及附件】
- python实现邮件发送功能
- Python3实现发送QQ邮件功能(文本)
- python实现发送邮件功能代码
- python脚本实现发送邮件功能
- python实现SMTP邮件发送功能
- Python实现自动发送邮件功能
- python3使用smtplib实现发送邮件功能
- Python脚本实现发送邮件功能
- Python 使用SMTP 实现发邮件功能
- 利用python收发邮件功能实现远程电脑的控制
- Python smtplib实现发送邮件功能
- python3.4实现邮件发送功能
- Python3实现发送QQ邮件功能(附件)