Python 发送email的方法

来源:互联网 发布:whatsapp for mac 编辑:程序博客网 时间:2024/05/19 17:56

import smtplibfrom email.MIMEMultipart import MIMEMultipartfrom email.MIMEText import MIMETextfrom email.MIMEBase import MIMEBasefrom email import Encoders##------------------------------------------------------------------------------def sendEmail(subject, body, host, sender, recipients):##    recipients = getRecipientList(recipients)    header = "Subject: %s\r\nFrom: %s\r\nTo: %s\r\n\r\n" % (                subject, sender, ", ".join(recipients))        smtp = smtplib.SMTP(host)           smtp.sendmail(sender, recipients, header + body)    smtp.quit()##------------------------------------------------------------------------------  def sendEmailWithAttachments(subject, body, host, sender, recipients, attachments):    msg = MIMEMultipart()    msg['Subject'] = subject    msg['From'] = sender    msg['To'] = ", ".join(recipients)    msg.preamble = subject    msg.epilogue = ''    # guarantees the message ends in a newline    if body:        msg.attach(MIMEText(body))    # Handle the attachments:    for filename in attachments:        msg.attach(getMIMEMessage(filename))    smtp = smtplib.SMTP(host)        smtp.sendmail(sender, recipients, msg.as_string())    smtp.quit()##------------------------------------------------------------------------------  def sendEmailWithHtmlContent(subject, body, host, sender, recipients):    msg = MIMEMultipart('alternative')    msg['Subject'] = subject    msg['From'] = sender    msg['To'] = ", ".join(recipients)    msg.preamble = subject    msg.epilogue = ''    # guarantees the message ends in a newline    if body:        msg.attach(MIMEText(body,'html'))    smtp = smtplib.SMTP(host)        smtp.sendmail(sender, recipients, msg.as_string())    smtp.quit()    ##------------------------------------------------------------------------------def getMIMEMessage(filename):    fp = open(filename, 'rb')    msg = MIMEBase('application', 'octet-stream')    msg.set_payload(fp.read())    fp.close()        Encoders.encode_base64(msg)         # Set the filename parameter:    msg.add_header('Content-Disposition', 'attachment', filename=filename)    return msg##------------------------------------------------------------------------------def getRecipientList(filename):    lines = file(filename).readlines()    return [l.strip() for l in lines            if l.strip() and not l.startswith('#')]  ##------------------------------------------------------------------------------if __name__ == '__main__':#     sendEmail(subject='Testing',#               body='This is just a test',#               host='internalmail.qq.com',#               sender='EmailTester@qq.com',#               recipients=['mike.cheng@qq.com'])#     sendEmailWithAttachments(#         subject='Testing',#         body='This is just a test',#         host='internalmail.qq.com',#         sender='EmailTester@qq.com',#         recipients=['feihong.hsu@qq.com'],#         attachments=['mail.py'])# #  sendEmailWithHtmlContent #     mailcontent= open('reboot_success.html','r').read()#     mailcontent = mailcontent.replace('{processName}', '')#     mailcontent = mailcontent.replace('{computerName}', '')#     mailcontent = mailcontent.replace('{serviceName}', '')#     sendEmailWithHtmlContent(subject='[Warn] has been restarted successfully on ',#            body=mailcontent,#            host='internalmail.qq.com',#            sender='DataOperation.Monitor@qq.com',#            recipients=['SZDATAPRODUCTION@qq.com'])                        print '\nDone!\n'