代碼如下:
author = 'damao'
"""發(fā)送郵件封裝"""
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
class SendMail(object):
def __init__(self,smtp_server,
smtp_port,
smtp_sender,
smtp_senderpassword,
smtp_receiver,
smtp_subject,
smtp_body,
smtp_file=None):
"""
to init parameter
:param smtp_server: 郵件服務(wù)器
:param smtp_port:端口號
:param smtp_sender:發(fā)件人
:param smtp_senderpassword:密碼
:param smtp_receiver:收件人
:param smtp_subject:郵件主題
:param smtp_body:郵件內(nèi)容
:param smtp_file_path:文件路徑
"""
self.smtp_server = smtp_server
self.smtp_port = smtp_port
self.smtp_sender = smtp_sender
self.smtp_senderpassword = smtp_senderpassword
self.smtp_receiver = smtp_receiver
self.smtp_subject = smtp_subject
self.smtp_body = smtp_body
self.smtp_file = smtp_file
def mail_content(self):
"""
to edit mail content
:param subject: 郵件主題
:param body:郵件內(nèi)容
:return:msg
"""
if self.smtp_file != None :
msg = MIMEMultipart()
with open(self.smtp_file,'rb') as fp:
mail_body =fp.read()
att = MIMEText(mail_body, "base64", "utf-8")
att['Conten-Type'] = "application/octet-stream"
att["Content-Disposition"] = 'attachment; filename="%s"' % self.smtp_file
msg.attach(att)
msg.attach(MIMEText(self.smtp_body, "html", "utf-8"))
msg['from'] = self.smtp_sender
msg['to'] = ";".join(self.smtp_receiver)
msg['subject'] = self.smtp_subject
return msg
else:
msg = MIMEText(self.smtp_body, "html", "utf-8")
msg['from'] = self.smtp_sender
msg['to'] = ";".join(self.smtp_receiver)
msg['subject'] = self.smtp_subject
return msg
def send_mail(self):
try:
smtp = smtplib.SMTP()
smtp.connect(self.smtp_server)
smtp.login(user=self.smtp_sender,password=self.smtp_senderpassword)
except:
smtp = smtplib.SMTP_SSL()
smtp.login(user=self.smtp_sender,password=self.smtp_senderpassword)
aaa = self.mail_content()
try:
smtp.sendmail(self.smtp_sender,self.smtp_receiver,aaa.as_string())
print("發(fā)送成功----")
except Exception as e:
print("發(fā)送失敗...",e)
smtp.quit()
if __name__ =='__main__':
# 公司郵箱測試
a = SendMail(smtp_server="xxxx.com",
smtp_port=993,
smtp_sender="xxxx.com",
smtp_senderpassword="xxxx",
smtp_receiver="306936010@qq.com",# ,"czk176@zcsmart.com"
smtp_subject="大毛的郵件!场仲!",
smtp_body="<p>我的簡書地址:http://www.reibang.com/u/1f9e71a85238</p>")
a.send_mail()
# QQ郵箱測試
b = SendMail(smtp_server="smtp.qq.com",
smtp_port=465,
smtp_sender=""xxxx.com.com",
smtp_senderpassword=""xxxx.com",
smtp_receiver="xxxx.com",# ,""xxxx.comt.com"
smtp_subject="小毛的郵件B稀刁赦!",
smtp_body="<p>我的簡書地址:http://www.reibang.com/u/1f9e71a85238</p>")
b.send_mail()
# 發(fā)送多人郵件
c = SendMail(smtp_server="mail.zcsmart.com",
smtp_port=993,
smtp_sender="xxxx.com",
smtp_senderpassword=""xxxx..",
smtp_receiver=[""xxxx.com","xxxx.com","xxxxt.com"],
smtp_subject="大毛的郵件F牙摺蛤克!",
smtp_body="<p>我的簡書地址:http://www.reibang.com/u/1f9e71a85238</p>")
c.send_mail()
# 帶附件的郵件發(fā)送
d = SendMail(smtp_server=""xxxx.com",
smtp_port=993,
smtp_sender="xxxxt.com",
smtp_senderpassword=""xxxx",
smtp_receiver=""xxxx.com",
smtp_subject="大毛的郵件9藓奋早!",
smtp_body="<p>我的簡書地址:http://www.reibang.com/u/1f9e71a85238</p>",
smtp_file='.\\aaa.txt')
d.send_mail()