1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Python3实现发送邮件 发送图片 附件等

Python3实现发送邮件 发送图片 附件等

时间:2018-12-30 02:56:07

相关推荐

Python3实现发送邮件 发送图片 附件等

生成QQ邮箱授权码

点击开启,然后按照提示短信验证即可。PS:友情提示,珍爱生命,远离网易163…

简单邮件发送

# coding=utf-8import smtplibfrom email.mime.text import MIMEText# 配置邮箱信息sender = 'xxxxxx@' # 发件人的地址password = 'xxxxxxxxx' # 此处是我们刚刚在邮箱中获取的授权码receivers = 'xxxxxx@' # 邮件接受方邮箱地址,可以配置多个,实现群发,注意这里要是字符串# 邮件内容设置message = MIMEText('你好呀,这是来自QQ邮箱的信息--from python发送~!', 'plain', 'utf-8')# 邮件标题设置message['Subject'] = 'Python企鹅问候-2'# 发件人信息message['From'] = sender# 收件人信息message['To'] = receivers# 通过授权码,登录邮箱,并发送邮件try:server = smtplib.SMTP('') # 配置QQ邮箱的smtp服务器地址server.login(sender, password)server.sendmail(sender, receivers.split(','), message.as_string())print('发送成功')server.quit()except smtplib.SMTPException as e:print('error', e)

我的网易163邮箱成功收取到对应的内容

邮件内容格式化

有一些时候,我们需要对邮件内容做一些美化,比如加粗,标注颜色等,这个时候我们就需要用到格式化的一些功能,代码如下:

# coding=utf-8import smtplibfrom email.mime.text import MIMEText# 配置邮箱信息sender = 'xxxxxxxxx@' # 发件人的地址password = 'xxxxxxxxx' # 此处是我们刚刚在邮箱中获取的授权码receivers = 'xxxxxxxxx@' # 邮件接受方邮箱地址,可以配置多个,实现群发,注意这里要是字符串# 邮件内容设置message = msg = MIMEText("<html><h2>努力赚钱才是正经事,穷人的精力更多是在思考如何生活,富人才有精力享受生活。比如,她晚上邀你去她家做客,没钱的人或许会因为心疼打车钱而止步,有钱的人只会因为正在另一位姑娘家做客而拒绝。</h2>""<br>""<a href='/zcm545186061'>我的CSDN</a></html>""<br>""<font color='red' size='10'>这是红色字体</font>",_subtype="html", _charset="utf-8")# 邮件标题设置message['Subject'] = 'python使用HTML语法格式发送邮件内容'# 发件人信息message['From'] = sender# 收件人信息message['To'] = receivers# 通过授权码,登录邮箱,并发送邮件try:server = smtplib.SMTP('') # 配置QQ邮箱的smtp服务器地址server.login(sender, password)server.sendmail(sender, receivers.split(','), message.as_string())print('发送成功')server.quit()except smtplib.SMTPException as e:print('error', e)

简单的说明下HTML语法的意思

h2:字体标题h2

br:换行

a href:超链接

font:设置字体大小和颜色等

发送附件(图片、Excel、压缩文件等)

# coding=utf-8import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.mime.image import MIMEImagefrom email.mime.application import MIMEApplication# 配置邮箱信息sender = '545186061@' # 发件人的地址password = 'ucpjszogfwbsbfhh' # 此处是我们刚刚在邮箱中获取的授权码receivers = 'scrapy_zhang@' # 邮件接受方邮箱地址,可以配置多个,实现群发,注意这里要是字符串# 邮件内容设置content = MIMEText("<html><h2>发送图片、EXcel、PDF等附件测试...</h2>", _subtype="html", _charset="utf-8")msg = MIMEMultipart('related')msg.attach(content)# 添加图片附件imageFile = r"C:\Users\Administrator\Desktop\boy.png"imageApart = MIMEImage(open(imageFile, 'rb').read(), imageFile.split('.')[-1])imageApart.add_header('Content-Disposition', 'attachment', filename=imageFile)msg.attach(imageApart)# 添加Excel附件excelFile = r'C:\Users\Administrator\Desktop\top100电影.xlsx'excelApart = MIMEApplication(open(excelFile, 'rb').read())excelApart.add_header('Content-Disposition', 'attachment', filename=excelFile)msg.attach(excelApart)# 邮件标题设置msg['Subject'] = 'python发送附件测试-图片、Excel'# 发件人信息msg['From'] = sender# 收件人信息msg['To'] = receivers# 通过授权码,登录邮箱,并发送邮件try:server = smtplib.SMTP('') # 配置QQ邮箱的smtp服务器地址server.login(sender, password)server.sendmail(msg['From'], msg['To'].split(','), msg.as_string())print('发送成功')server.quit()except smtplib.SMTPException as e:print('error', e)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。