Python發(fā)送Email方法實例
更新時間:2014年08月21日 11:59:51 投稿:shichen2014
這篇文章主要介紹了Python發(fā)送Email的方法,有不錯的實用價值,代碼備有一定的注釋便于讀者理解,需要的朋友可以參考下
本文以實例形式展示了Python發(fā)送Email功能的實現(xiàn)方法,有不錯的實用價值的技巧,且功能較為完善。具體實現(xiàn)方法如下:
主要功能代碼如下:
#/usr/bin/env python # -*- encoding=utf-8 -*- import base64 import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText class CCSendMail: def __init__(self,host="your.mailhost.com",username='fromuser@xxx.com',password='passwd'): self.__smtp=smtplib.SMTP(host) self.__subject=None self.__content=None self.__from=None self.__to=[] self.__style='html' self.__charset='gb2312' self.username = username self.password = password self.fromAlias='fromuser' #發(fā)件人別名 self.from2='' def close(self): try: self.__smtp.quit() except Exception ,e: pass def setFromAlias(self,alias): self.fromAlias=alias def setStyle(self,style): self.__style = style def setFrom2(self,from2): self.from2=from2 def setSubject(self,subject): self.__subject=subject def setContent(self,content): self.__content=content def setFrom(self,address): self.__from=address def addTo(self,address): self.__to.append(address) def setCharset(self,charset): self.__charset=charset def send(self): try: self.__smtp.set_debuglevel(1) #login if necessary if self.username and self.password: self.__smtp.login(self.username,self.password) msgRoot = MIMEMultipart('related') msgRoot['Subject'] = self.__subject aliasB6=base64.encodestring(self.fromAlias.encode(self.__charset)) if len(self.from2)==0: msgRoot['From'] = "=?%s?B?%s?=%s"%(self.__charset,aliasB6.strip(),self.__from) else: msgRoot['From'] = "%s"%(self.from2) msgRoot['To'] = ";".join(self.__to) msgAlternative = MIMEMultipart('alternative') msgRoot.attach(msgAlternative) msgText = MIMEText(self.__content, self.__style,self.__charset) msgAlternative.attach(msgText) self.__smtp.sendmail(self.__from,self.__to,msgRoot.as_string()) return True except Exception,e: print "Error ",e return False def clearRecipient(self): self.__to = [] #給單個人發(fā)送郵件 def sendHtml(self,address,title,content): self.setStyle('html') self.setFrom("<%s>"%self.username) if not isinstance(content,str): content = content.encode('gb18030') self.addTo(address) self.setSubject(title) self.setContent(content) ret = self.send() self.close() return ret #群發(fā)郵件 def sendMoreHtml(self,addressList,title,content): self.setStyle('html') self.setFrom("<%s>"%self.username) if not isinstance(content,str): content = content.encode('gb18030') for address in addressList: self.addTo(address) self.setSubject(title) self.setContent(content) ret = self.send() self.close() return ret #測試 def main(): send=CCSendMail() send.sendHtml('touser@xxx.com',u'郵件標(biāo)題',u'郵件內(nèi)容') #send.sendMoreHtml([touser1@xx.com,touser2@xx.com],u'郵件標(biāo)題',u'郵件內(nèi)容') if __name__=='__main__': main()
希望本文所述實例對大家的Python程序設(shè)計有一定的幫助。
相關(guān)文章
matplotlib圖例、標(biāo)簽、坐標(biāo)軸刻度的字體設(shè)置方式
這篇文章主要介紹了matplotlib圖例、標(biāo)簽、坐標(biāo)軸刻度的字體設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05Python之tkinter文字區(qū)域Text使用及說明
這篇文章主要介紹了Python之tkinter文字區(qū)域Text使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05python在linux環(huán)境下安裝skimage的示例代碼
這篇文章主要介紹了python在linux環(huán)境下安裝skimage,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10python經(jīng)典趣味24點游戲程序設(shè)計
這篇文章主要介紹了python經(jīng)典趣味24點游戲程序設(shè)計,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07詳解python 條件語句和while循環(huán)的實例代碼
這篇文章主要介紹了詳解python 條件語句和while循環(huán),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12