如何用python发送邮件

直接看代码,将服务器信息,收件人发件人修改即可。

#!/usr/bin/python
#coding:utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL
from email.mime.multipart import MIMEMultipart
#send mail
def sendMail():
        host_server = 'smtp.exmail.qq.com'
        sender_qq = '[email protected]'
        pwd = 'password'

        #发件人的邮箱
        sender_qq_mail = '[email protected]'
        #收件人邮箱
        receiver = '[email protected]'
        #邮件的正文内容
        mail_content = "你好,

每日统计开机率低于30%酒店,请查收附件

" #邮件标题 mail_title = '开机率低于30%酒店数据统计' #邮件正文内容 msg = MIMEMultipart() #msg = MIMEText(mail_content, "plain", 'utf-8') msg["Subject"] = Header(mail_title, 'utf-8') msg["From"] = sender_qq_mail msg["To"] = Header("运维通知邮件组", 'utf-8') ## 接收者的别名 #邮件正文内容 msg.attach(MIMEText(mail_content, 'html', 'utf-8')) # 构造附件1,传送当前目录下的 test.txt 文件 att1 = MIMEText(open("example-"+Yesterday+".xls", 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream' # 这里的filename可以任意写,写什么名字,邮件中显示什么名字 att1["Content-Disposition"] = 'attachment; filename="example.xls"' msg.attach(att1) #ssl登录 smtp = SMTP_SSL(host_server) #set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式 smtp.set_debuglevel(1) smtp.ehlo(host_server) smtp.login(sender_qq, pwd) smtp.sendmail(sender_qq_mail, receiver, msg.as_string()) smtp.quit()

你可能感兴趣的:(如何用python发送邮件)