用python發送帶附件的郵件


from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.header import Header
import smtplib
def send_mail(newReport):
    
    smtpserver = 'smtp.mxhichina.com'
    user = '******@**.com'
    password = "password"
    sender = '******@**.com'
    receiver = "******@**.com"
    
    subject = 'auto test report'
           
    att = MIMEApplication(open(newReport[0], 'rb').read())
    att.add_header('Content-Disposition', 'attachment', filename=newReport[1])
        
    f = open(newReport[0], 'rb')
    mail_body = f.read()
    f.close()  
    msg = MIMEText(mail_body, 'html', 'utf-8')
       
    msgRoot = MIMEMultipart('related')
    msgRoot['Subject'] = subject
    msgRoot.attach(msg)
    msgRoot.attach(att)
    
    smtp = smtplib.SMTP_SSL()
    smtp.connect(smtpserver)
    smtp.login(user, password)
    smtp.sendmail(sender, receiver, msgRoot.as_string())
    smtp.quit()
注意:smtplib.SMTPAuthenticationError: (530, 'Error: A secure connection is requiered(such as ssl). More information at  http://service.mail.qq.com/cgi-bin/help?id=28')
問題原因:發送郵箱沒有開啟IMAP/SMTP服務
解決辦法:開啟郵箱IMAP/SMTP服務【smtp = smtplib.SMTP_SSL() 】


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM