複製****
#!/usr/bin/python
#coding=utf-8
#@author:dengyike
#@date:2010-09-28
#@version:1.0
#@description: auto sending email with attachment file
import email
import mimetypes
from email.mimemultipart import mimemultipart
from email.mimetext import mimetext
from email.mimeimage import mimeimage
import smtplib
import logging
import sys
reload(sys)
sys.setdefaultencoding('utf8')
maildict = {} #郵件配置資訊
###################
#日誌輔助類
#################
class logger:
log_release= "releae"
log_release_file = "/tmp/pymail.log"
def __init__(self, log_type):
self._logger = logging.getlogger(log_type)
if log_type == logger.log_release:
self._logfile = logger.log_release_file
handler = logging.filehandler(self._logfile)
if log_type == logger.log_release:
formatter = logging.formatter('%(asctime)s ********* %(message)s')
else:
formatter = logging.formatter('%(message)s')
handler.setformatter(formatter)
self._logger.addhandler(handler)
self._logger.setlevel(logging.info)
def log(self, msg):
if self._logger is not none:
self._logger.info(msg)
mylogger = logger(logger.log_release) #logger
def initmailconf():#初始化郵件配置資訊
global maildict
maildict['server'] = "smtp.google.com"
maildict['user'] = "dengyike"
maildict['password'] = "dengyike"
maildict["from"] = "[email protected]"
maildict["cc"] = "[email protected],[email protected]"
maildict["to"] = "[email protected]"
maildict["subject"] = "python郵件指令碼測試"
maildict["text"] = "這裡是普通文字資訊"
maildict["html"] = '這裡是html文字資訊'
def sendmail(parammap):#傳送郵件
smtp = smtplib.smtp()
msgroot = mimemultipart('related')
msgalternative = mimemultipart('alternative')
if parammap.has_key("server") and parammap.has_key("user") and parammap.has_key("password"):
try:
smtp.set_debuglevel(1)
smtp.connect(parammap["server"])
smtp.login(parammap["user"], parammap["password"])
except exception, e:
mylogger.log("smtp login exception!")
return false
else:
mylogger.log("parameters incomplete!")
return false
if parammap.has_key("subject") == false or parammap.has_key("from")== false or parammap.has_key("to") == false:
mylogger.log("parameters incomplete!")
return false
msgroot['subject'] = parammap["subject"]
msgroot['from'] = parammap["from"]
if parammap.has_key("cc"):
msgroot['cc'] = parammap["cc"]
msgroot['to'] = parammap["to"]
msgroot.preamble = 'this is a multi-part message in mime format.'
msgroot.attach(msgalternative)
tempaddto = parammap["to"]
if parammap.has_key("text"):
msgtext = mimetext(parammap["text"], 'plain', 'utf-8')
msgalternative.attach(msgtext)
if parammap.has_key("html"):
msgtext = mimetext(parammap["html"], 'html', 'utf-8')
msgalternative.attach(msgtext)
if parammap.has_key("image"):
fp = open(parammap["image"], 'rb')
msgimage = mimeimage(fp.read())
fp.close()
msgimage.add_header('content-id', '' )
msgroot.attach(msgimage)
if parammap.has_key("cc"):
tempaddto = parammap["to"] + "," + parammap["cc"]
if tempaddto.find(",") != -1:
finallyadd = tempaddto.split(",")
else:
finallyadd = tempaddto
#構造附件
filename = "/tmp/test.zip"
basename = os.path.basename(filename)
if os.path.exists(filename): #資料檔案存在
data = open(filename, 'rb')
att = mimetext(data.read(), 'base64', 'gb2312')
att["content-disposition"] = 'attachment; filename="%s"' % basename
msgroot.attach(att)
smtp.sendmail(parammap["from"], finallyadd, msgroot.as_string())
smtp.quit()
return true
def process():
global maildict
initmailconf()
sendmail(maildict)
if __name__ == "__main__":
process()
複製**
最後把執行命令加入crontab中,就可以每天定時自動傳送郵件了。
Python 指令碼傳送郵件
使用python完成郵件自動傳送 usr bin python coding utf 8 import smtplib from email.header import header from email.mime.text import mimetext class email sender ob...
使用python指令碼傳送郵件
import smtplib from email.mime.text import mimetext from email.header import header 傳送方 sender 163.com 接收方 receiver 163.com 中文需要utf 8引數,不好使需要調整 在這裡輸入郵...
Python 指令碼自動傳送郵件
自動傳送郵件功能是我們經常要用到的,比如每天定時統計報表資訊,然後自動傳送給運營人員,協助運營人員進行業務資料分析。本文是用python寫的乙個自動傳送郵件的指令碼,呼叫函式時,直接把發件人郵箱位址 密碼 收件人郵箱位址 郵件標題 內容等資訊傳遞給函式,即可實現自動傳送郵件功能。指令碼 如下 pyt...