python3 发送任意文件邮件
2018-01-22 19:13
281 查看
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
import email.mime.multipart
import email.mime.text
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
def send_email(smtpHost, sendAddr, password, recipientAddrs, subject='', content=''):
msg = email.mime.multipart.MIMEMultipart()
msg['from'] = sendAddr
msg['to'] = recipientAddrs
msg['subject'] = subject
content = content
txt = email.mime.text.MIMEText(content, 'plain', 'utf-8')
msg.attach(txt)
# 添加附件,传送D:/mydev/yasuo.rar文件
part = MIMEApplication(open('D:/mydev/6.rar','rb').read())
part.add_header('Content-Disposition', 'attachment', filename="yasuo.rar")
msg.attach(part)
smtp = smtplib.SMTP()
smtp.connect(smtpHost, '25')
smtp.login(sendAddr, password)
smtp.sendmail(sendAddr, recipientAddrs, str(msg))
print("发送成功!")
smtp.quit()
try:
subject = 'Python 测试邮件'
content = '这是一封来自 Python 编写的测试邮件。'
send_email('smtp.163.com', '18310161797@163.com', '邮箱密码', '526189064@qq.com', subject, content)
except Exception as err:
print(err)
# -*- coding: UTF-8 -*-
import smtplib
import email.mime.multipart
import email.mime.text
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
def send_email(smtpHost, sendAddr, password, recipientAddrs, subject='', content=''):
msg = email.mime.multipart.MIMEMultipart()
msg['from'] = sendAddr
msg['to'] = recipientAddrs
msg['subject'] = subject
content = content
txt = email.mime.text.MIMEText(content, 'plain', 'utf-8')
msg.attach(txt)
# 添加附件,传送D:/mydev/yasuo.rar文件
part = MIMEApplication(open('D:/mydev/6.rar','rb').read())
part.add_header('Content-Disposition', 'attachment', filename="yasuo.rar")
msg.attach(part)
smtp = smtplib.SMTP()
smtp.connect(smtpHost, '25')
smtp.login(sendAddr, password)
smtp.sendmail(sendAddr, recipientAddrs, str(msg))
print("发送成功!")
smtp.quit()
try:
subject = 'Python 测试邮件'
content = '这是一封来自 Python 编写的测试邮件。'
send_email('smtp.163.com', '18310161797@163.com', '邮箱密码', '526189064@qq.com', subject, content)
except Exception as err:
print(err)
相关文章推荐
- python3 发送任意文件邮件的实例
- 一个python的邮件发送脚本,自动,定时,可以附件发送,抄送,附有说明文件
- Python爬虫-爬取集思录的金融信息,并写入文件和检测数据变化发送邮件通知
- python 检测文件,有异常信息就发送邮件
- python读取\t\n的文件转换成html表格发送邮件(解决中文乱码问题)
- python 发送邮件 -- 解析配置文件
- python监控文件,并且发送告警邮件
- python监控文件并且发送告警邮件
- 一个python的邮件发送脚本,自动,定时,可以附件发送,抄送,附有说明文件
- python3第三方使用smtp发送邮件附带xlsx文件【已解决中文名报错】
- python应用系列教程——python使用smtp协议发送邮件:html文本邮件、图片邮件、文件附件邮件
- 一个python的邮件发送脚本,自动,定时,可以附件发送,抄送,附有说明文件 (本脚本是python的2.7.x环境下可用)
- python3 发邮件,从文件读邮件内容和发送html内容
- Python3.3 邮件发送 含附件(各种类型文件)
- Python提取MySQL数据为EXCEL文件后邮件发送
- Python3.5 email发送邮件,包含txt、图片、HTML、附件
- laravel5.6 发送邮件附带邮件时,Unable to open file for reading,报错文件路径问题
- python发送邮件
- Python邮件发送
- Python简单学习(lecture7)CGI编程、MySQL、网络编程、SMTP发送邮件