Python3.0 我的DailyReport 脚本(二) Email发送模块
其实不会用Python,跟风装了Python3.0,看了几天自带的Manual,写个日报的脚本玩玩,不用不要紧,一用感觉还挺好用的
听说Pyhon2.X或更早的原古时代,发送中文邮件,那个叫痛苦,看来我赶上好时代了.Python3.0发点邮件的代码还是比较轻松,贴代谢产物:
Code
#!/usr/bin/env python
#coding=utf-8
#!/usr/bin/env python
#coding=utf-8
#author:haozes
#发送邮件模块
import sys
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from email import utils
class MailSender:
_from=None
to=None
cc=None
_subject=None
html=None
text=None
def __init__(self,smtpSvr,port):
self.smtp = smtplib.SMTP()
self.smtp.connect(smtpSvr,port)
def login(self,user,pwd):
self._from=user
self.smtp.login(user,pwd)
def setSubject(self,sub):
self._subject=Header(sub,'utf-8')
def send(self):
msg = MIMEMultipart('alternative')
msg['Subject'] = self._subject
msg['From'] = self._from
msg['To'] = self.to
msg['Cc']=self.cc
mailto=self.to.split(',')
if(self.cc!=None):
mailto.extend(self.cc.split(','))
print(mailto)
if self.text!=None:
part1 = MIMEText(self.text.encode(),'plain',_charset='utf-8')
msg.attach(part1)
if self.html!=None:
part2 = MIMEText(self.html.encode(),'html',_charset='utf-8')
msg.attach(part2)
self.smtp.sendmail(self._from,mailto,msg.as_string())
self.smtp.quit()
if __name__ == "__main__":
pass
#!/usr/bin/env python
#coding=utf-8
#!/usr/bin/env python
#coding=utf-8
#author:haozes
#发送邮件模块
import sys
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from email import utils
class MailSender:
_from=None
to=None
cc=None
_subject=None
html=None
text=None
def __init__(self,smtpSvr,port):
self.smtp = smtplib.SMTP()
self.smtp.connect(smtpSvr,port)
def login(self,user,pwd):
self._from=user
self.smtp.login(user,pwd)
def setSubject(self,sub):
self._subject=Header(sub,'utf-8')
def send(self):
msg = MIMEMultipart('alternative')
msg['Subject'] = self._subject
msg['From'] = self._from
msg['To'] = self.to
msg['Cc']=self.cc
mailto=self.to.split(',')
if(self.cc!=None):
mailto.extend(self.cc.split(','))
print(mailto)
if self.text!=None:
part1 = MIMEText(self.text.encode(),'plain',_charset='utf-8')
msg.attach(part1)
if self.html!=None:
part2 = MIMEText(self.html.encode(),'html',_charset='utf-8')
msg.attach(part2)
self.smtp.sendmail(self._from,mailto,msg.as_string())
self.smtp.quit()
if __name__ == "__main__":
pass