- 利用python的組件進行發(fā)郵件
1.構(gòu)建郵件服務(wù)器設(shè)置舟铜、連接服務(wù)器月褥、登錄服務(wù)器
2.構(gòu)建郵件內(nèi)容
3.發(fā)送郵件,收件人收件
步驟1腮郊、2通過smtplib實現(xiàn),步驟3通過email實現(xiàn)
代碼片段:構(gòu)建郵件服務(wù)凿掂、發(fā)郵件
# 構(gòu)建smtp對象
smtp = smtplib.SMTP()
# 連接到smtp服務(wù)
smtp.connect(self.smtp_server, self.stmp_port)
# 登錄smtp服務(wù)
smtp.login(self.sender, self.password)
# 發(fā)送郵件
print('收件人為:', self.reveiver)
res = smtp.sendmail(self.sender, self.reveiver, msg.as_string())
print("郵件發(fā)送結(jié)果: ", res)
# 退出
smtp.quit()
print("send email finish")
- 構(gòu)建郵件內(nèi)容
郵件內(nèi)容的構(gòu)建大致為文本內(nèi)容伴榔、html格式纹蝴、附件庄萎、圖片四種需求
使用到的包文件為:
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
代碼片段:
def __content_of_email(self, msg_type):
"""
郵件正文為文本內(nèi)容
:param content:
:return:
"""
msg = MIMEMultipart()
msg["From"] = self.sender
msg["To"] = ";".join(self.reveiver)
msg['Subject'] = Header(self.subject, "utf-8")
# 如果msg為plain,則郵件正文為文本
if msg_type == 'plain':
msg.attach(MIMEText(self.content, "plain", "utf-8"))
# 如果msg為html,則郵件正文為html
elif msg_type == "html":
msg.attach(MIMEText(self.content, "html", "utf-8"))
else:
print("郵件內(nèi)容類型不正確")
if self.attachment is not None:
# 讀取附件內(nèi)容
with open(attachment, 'r')as f:
contents = f.read()
# 設(shè)置html格式參數(shù)
html_content = MIMEText(contents, 'base64', 'utf-8')
html_content["Content-Type"] = 'application/octet-stream'
html_content.add_header("Content-Disposition", "attachment", filename=os.path.basename(attachment))
msg.attach(html_content)
return msg
# __author__ ='wuwa'
# -*- coding: utf-8 -*-
"""
email:構(gòu)造郵件
1.發(fā)送郵件的賬號、密碼
2.連接郵件服務(wù)
3.設(shè)置郵件標題塘安、內(nèi)容
smtplib:發(fā)送郵件
4.發(fā)送郵件
"""
import os
import smtplib
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
class WriteEmails:
def __init__(self, smtp_server, stmp_port, sender, password, reveiver, subject, content, msg_type, attachment):
self.smtp_server = smtp_server
self.stmp_port = stmp_port
self.sender = sender
self.password = password
self.reveiver = reveiver
self.subject = subject
self.content = content
self.msg_type = msg_type
self.attachment = attachment
def __content_of_email(self, msg_type):
"""
郵件正文為文本內(nèi)容
:param content:
:return:
"""
msg = MIMEMultipart()
msg["From"] = self.sender
msg["To"] = ";".join(self.reveiver)
msg['Subject'] = Header(self.subject, "utf-8")
# 如果msg為plain,則郵件正文為文本
if msg_type == 'plain':
msg.attach(MIMEText(self.content, "plain", "utf-8"))
# 如果msg為html,則郵件正文為html
elif msg_type == "html":
msg.attach(MIMEText(self.content, "html", "utf-8"))
else:
print("郵件內(nèi)容類型不正確")
if self.attachment is not None:
# 讀取附件內(nèi)容
with open(attachment, 'r')as f:
contents = f.read()
# 設(shè)置html格式參數(shù)
html_content = MIMEText(contents, 'base64', 'utf-8')
html_content["Content-Type"] = 'application/octet-stream'
html_content.add_header("Content-Disposition", "attachment", filename=os.path.basename(attachment))
msg.attach(html_content)
return msg
def senf_send_email(self):
"""
發(fā)郵件
:return:
"""
msg = self.__content_of_email(self.msg_type)
try:
# 構(gòu)建smtp對象
smtp = smtplib.SMTP()
# 連接到smtp服務(wù)
smtp.connect(self.smtp_server, self.stmp_port)
# 登錄smtp服務(wù)
smtp.login(self.sender, self.password)
# 發(fā)送郵件
print('收件人為:', self.reveiver)
res = smtp.sendmail(self.sender, self.reveiver, msg.as_string())
print("郵件發(fā)送結(jié)果: ", res)
# 退出
smtp.quit()
print("send email finish")
except smtplib.SMTPException as e:
print('error', e)
if __name__ == "__main__":
# 郵箱服務(wù)地址
smtp_server = 'xx.xx.com'
# 郵箱服務(wù)的端口
stmp_port = xx
# 郵件發(fā)送者
sender = "xxxx@qq.com"
# 郵件發(fā)送者密碼
password = "password"
# 郵件接收者
receiver = ["xxx@qq@qq.com", "xxx@qq.com"]
content = "郵件正文"
subject = "郵件標題"
# 郵件正文類型
msg_type = "html"
BASE_DIR = os.path.abspath(os.path.dirname(os.getcwd()))
# 獲取ini文件的路徑
attachment = os.path.join(BASE_DIR, "report\\report.html")
# 實例化
m = WriteEmails(smtp_server, stmp_port, sender, password, receiver, subject, content, msg_type, attachment)
# 調(diào)用類方法
m.senf_send_email()