Python 发送email的方法


import smtplib

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from 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'


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值