摘要:

     平时运行一些脚本,需要把结果发送到邮箱,可以用python来处理,如下:

1,有邮件服务器:

#!/bin/env python
# -*- encoding: utf-8 -*-
#-------------------------------------------------------------------------------
# Purpose: 发送邮件
# Author: zhoujy
# Created: 2013-05-07
# update: 2013-05-07
#-------------------------------------------------------------------------------
import os
import sys
import time
import datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.Utils import COMMASPACE, formatdate

reload(sys)
sys.setdefaultencoding('utf8')

def send_mail(to, subject, text, from_mail, server="localhost"): #发送邮件的函数
message = MIMEMultipart()
message['From'] = from_mail
message['To'] = COMMASPACE.join(to)
message['Date'] = formatdate(localtime=True)
message['Subject'] = subject
message.attach(MIMEText(text,_charset='utf-8'))
smtp = smtplib.SMTP(server)
smtp.sendmail(from_mail, to, message.as_string())
smtp.close()

if __name__ == "__main__":
mail_list = ['xxx@xxx.com']
subject = '测试邮件'
text = '测试邮件发送!'
from_mail ='test@stmp.xxx.cn'
server = '192.168.111.111' #邮件服务器
send_mail(mail_list, subject.encode("utf8"), text.encode("utf8"), from_mail, server)

执行:

zhoujy@zhoujy:~/script$ python send_email.py

效果:

Python 发送邮件 和 发送带附件邮件_sendmail

 

2,没有邮件服务器:

#!/bin/env python
# -*- encoding: utf-8 -*-
#-------------------------------------------------------------------------------
# Purpose: 发送邮件
# Author: zhoujy
# Created: 2013-05-07
# update: 2013-05-07
#-------------------------------------------------------------------------------
import os
import sys
import time
import datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.Utils import COMMASPACE, formatdate

reload(sys)
sys.setdefaultencoding('utf8')

def send_mail(to, subject, text, from_mail, server="smtp.163.com"):
message = MIMEMultipart()
message['From'] = from_mail
message['To'] = COMMASPACE.join(to)
message['Date'] = formatdate(localtime=True)
message['Subject'] = subject
message.attach(MIMEText(text,_charset='utf-8'))
smtp = smtplib.SMTP()
smtp.connect(server)
smtp.login("mr_mablevi@163.com","*******")
smtp.sendmail(from_mail, to, message.as_string())
smtp.close()

if __name__ == "__main__":
mail_list = ['xxx@xxx.com']
subject = '测试邮件'
text = '测试邮件发送!'
from_mail ='mr_mablevi@163.com'
send_mail(mail_list,subject,text,from_mail)

和第一个区别的是:红色部分
执行:

zhoujy@zhoujy:~/script$ python send_email.py

效果:
Python 发送邮件 和 发送带附件邮件_python_02

 

3,发送带附件邮件

#!/bin/env python
# -*- encoding: utf-8 -*-
#-------------------------------------------------------------------------------
# Purpose: 以附件方式发送
# Author: zhoujy
# Created: 2013-05-07
# update: 2013-05-07
#-------------------------------------------------------------------------------
import datetime
import time
import sys
import mimetypes
import smtplib
import email.MIMEMultipart
import email.MIMEText
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.Utils import COMMASPACE, formatdate
from email.mime.image import MIMEImage

reload(sys)
sys.setdefaultencoding('utf8')

month = (datetime.date.today() - datetime.timedelta(days=15)).strftime("%m")


msg=MIMEMultipart()

#下面2行#号互换,就可以用163的发邮件了。
#msg["From"] = "mr_mablevi@163.com"
msg["From"] = "Edm_Monitor@smtp.xxx.cn"

Tolist = ['xxx@xxx.com']
msg["Subject"] = '%s月统计报告' %month

text=MIMEText("附件内容为上个月发送情况的报告,详情请见附件。",_charset='utf-8')
msg.attach(text)

file_name = sys.argv[1]

ctype,encoding = mimetypes.guess_type(file_name)
if ctype is None or encoding is not None:
ctype = 'application/octet-stream'
maintype,subtype = ctype.split('/',1)

att = MIMEImage(open(file_name, 'rb').read(),subtype)
print ctype,encoding
att["Content-Disposition"] = 'attachmemt;filename="%s"' %file_name
msg.attach(att)

#下面4行#号互换,就可以用163的发邮件了。
smtp=smtplib.SMTP('192.168.111.111')
#smtp = smtplib.SMTP()
#smtp.connect("smtp.163.com")
#smtp.login("mr_mablevi@163.com","62391812")

smtp.sendmail(msg["From"],Tolist,msg.as_string())
smtp.close()

执行:

zhoujy@zhoujy:~/script$ python send_fj.py  /home/zhoujy/ABC.xls 
application/vnd.ms-excel None

效果:
Python 发送邮件 和 发送带附件邮件_sendmail_03

注意:要是发送给多个人,则需要是list格式,不能是string。如脚本里的需要改成:

Tolist = ['xxx@xxx.com','xxx@xxx.com','xxx@xxx.com','xxx@xxx.com',...]

其他:

​这里​​还有其他的一些方法,比如发送时候显示名字等。

~~~~~~~~~~~~~~~ 万物之中,希望至美 ~~~~~~~~~~~~~~~