python 发送邮件

方式1,不带附件:

备注:Report.html与运行的脚本在同一路径下;如用163邮箱发送邮件,需要163邮箱开通smtp服务;https://jingyan.baidu.com/article/c275f6ba33a95de33d7567d9.html

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
def send_email():
    reportpath = sys.path[0]+ '/Report.html'  #同路径下存在Report.html文件
    with open (reportpath) as f:
        file = ''
        lines = f.readlines()
        for line in lines:
            file +=line
    # 第三方 SMTP 服务
    mail_host='smtp.sinochiptp.com'  #设置服务器或使用'smtp.163.com'  
    mail_user='soc_test@sinochiptp.com'  #用户名
    mail_pass='123456a?' #邮箱密码:需要使用授权码
    sender = 'soc_test@sinochiptp.com' #发送者
    #receivers =['xue.yang@scsemicon.com','pan.zhao@scsemicon.com']#多人接收者列表
    receivers =['xue.yang@scsemicon.com']#接收者列表
    message = MIMEText(file, 'html', 'utf-8') #发送的正文
    message['From'] = sender
    message['To'] = ','.join(receivers)
    subject = 'NFC TEST REPORT' #发送的主题
    message['Subject'] = Header(subject, 'utf-8')
    smtpObj = smtplib.SMTP() 
    smtpObj.connect(mail_host,25)  #邮箱的服务器和端口号
    smtpObj.login(mail_user,mail_pass) #登录邮箱 
    smtpObj.sendmail(sender, message['To'].split(','), message.as_string())#参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串
    smtpObj.quit() # 发送完毕后退出smtp

if __name__ == "__main__":
    send_email()

效果图:

 

方式2,带附件:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
def send_email():
    reportpath = sys.path[0]+ '/Report.html'  #同路径下存在Report.html文件
    with open (reportpath) as f:
        file = ''
        lines = f.readlines()
        for line in lines:
            file +=line
    # 第三方 SMTP 服务
    mail_host='smtp.sinochiptp.com'  #设置服务器或使用'smtp.163.com'  
    mail_user='soc_test@sinochiptp.com'  #用户名
    mail_pass='123456a?' #邮箱密码:需要使用授权码
    sender = 'soc_test@sinochiptp.com' #发送者
    #receivers =['xue.yang@scsemicon.com','pan.zhao@scsemicon.com']#多人接收者列表
    receivers =['xue.yang@scsemicon.com']#接收者列表
    message = MIMEMultipart()
    message.attach(MIMEText(file, 'html', 'utf-8')) #邮件正文内容
    
    # 构造附件1,传送当前目录下的 test.txt 文件
    att1 = MIMEText(open('Report.html', 'rb').read(), 'base64', 'utf-8')
    att1["Content-Type"] = 'application/octet-stream'
    # 这里的filename可以任意写,写什么名字,邮件中显示什么名字
    att1["Content-Disposition"] = 'attachment; filename="NFC_Report.html"'
    message.attach(att1)
    message['From'] = sender
    message['To'] = ','.join(receivers)
    subject = 'NFC TEST REPORT' #发送的主题
    message['Subject'] = Header(subject, 'utf-8')
    smtpObj = smtplib.SMTP() 
    smtpObj.connect(mail_host,25)  #邮箱的服务器和端口号
    smtpObj.login(mail_user,mail_pass) #登录邮箱 
    smtpObj.sendmail(sender, message['To'].split(','), message.as_string())#参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串
    smtpObj.quit() # 发送完毕后退出smtp

if __name__ == "__main__":
    send_email()

 效果图:

 

posted @ 2019-07-02 17:51  yangxueupc  阅读(89)  评论(0)    收藏  举报