python 发送邮件
本人刚学习python,用python写一些小工具,如有写的不好的地方,请大家多多包含!!config.ini内容如下:
#邮箱账号(发件人)
sender=
#邮箱密码
password=
#添加收件人如多个收件人用逗号分割如receivers=a@qq.com,b@qq.com
receivers=xxx@xx.com
SendMail.py内容如下:
import smtplib
from configparser import ConfigParser
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
import os
def SendMail(subject,sendfilepath,text_info):
subject = subject
sendfilepath = sendfilepath
#邮件内容
text_info = text_info
#加载配置文件
config = ConfigParser()
cfg = config.read('config.ini', encoding='utf-8')
sender = config.get('sentmail', 'sender')
password = config.get('sentmail', 'password')
receivers = config.get('sentmail', 'receivers')
#定义邮件的基本格式
msg = MIMEMultipart('mixed')
msg["subject"] = Header(subject, 'utf-8')
msg["From"] = sender
receivers =
msg["To"] = ','.join(receivers)
fname = os.path.basename(sendfilepath)
#添加文本信息
text_info = MIMEText(text_info, 'plain', 'utf-8')
msg.attach(text_info)
#添加附件
file = open(sendfilepath, 'rb').read()
att = MIMEText(file, 'base64', 'utf-8')
att["Content_type"] = 'application/octet-stream'
att.add_header('Content-Disposition', 'attachment', filename = fname)
msg.attach(att)
#发送邮件
try:
smtpObj = smtplib.SMTP(“填写你使用邮箱SMTP”)
smtpObj.login(user=sender, password=password)
smtpObj.sendmail(sender, receivers, msg.as_string())
smtpObj.quit()
print ("邮件发送成功")
except Exception as e:
print ("Error: 无法发送邮件") 有库你不用... 哈哈,python最好用的地方就是什么都有库 厉害!! 邮箱练手挺好的谢谢分享 支持新人多发原创帖子 不错,之前写了一个监控网站 然后发邮件的 支持一下楼主 我有邮箱账号密码,我为啥还要写个程序来发邮件呢{:301_992:}
页:
[1]
2