Python 发送邮件 和 发送带附件邮件
生活随笔
收集整理的這篇文章主要介紹了
Python 发送邮件 和 发送带附件邮件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
摘要:
???? 平時運行一些腳本,需要把結果發送到郵箱,可以用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, formatdatereload(sys) sys.setdefaultencoding('utf8')def send_mail(to, subject, text, from_mail, server="localhost"): #發送郵件的函數message = MIMEMultipart()message['From'] = from_mailmessage['To'] = COMMASPACE.join(to)message['Date'] = formatdate(localtime=True)message['Subject'] = subjectmessage.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效果:
?
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, formatdatereload(sys) sys.setdefaultencoding('utf8')def send_mail(to, subject, text, from_mail, server="smtp.163.com"):message = MIMEMultipart()message['From'] = from_mailmessage['To'] = COMMASPACE.join(to)message['Date'] = formatdate(localtime=True)message['Subject'] = subjectmessage.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)和第一個區別的是:紅色部分
執行:
效果:
?
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 MIMEImagereload(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月統計報告' %monthtext=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效果:
注意:要是發送給多個人,則需要是list格式,不能是string。如腳本里的需要改成:
Tolist = ['xxx@xxx.com','xxx@xxx.com','xxx@xxx.com','xxx@xxx.com',...]其他:
這里還有其他的一些方法,比如發送時候顯示名字等。
總結
以上是生活随笔為你收集整理的Python 发送邮件 和 发送带附件邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我使用过的Linux命令之curl -
- 下一篇: 简单字符串过滤练习