Python 使用 smtp ssl 模式 发送邮件与附件
生活随笔
收集整理的這篇文章主要介紹了
Python 使用 smtp ssl 模式 发送邮件与附件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
參考 :
? ? ? ? 發送郵件簡單入門 ( 以qq郵箱,163郵箱為例 ) :https://blog.csdn.net/qq_38661599/article/details/81013834
? ? ? ? smtp ssl 模式 發送郵件 與 附件:https://www.cnblogs.com/SunshineKimi/p/10629342.html
? ? ? ? Python3 使用 SMTP 發送帶附件郵件:https://www.jb51.net/article/142231.htm
還可以使用?scrapy.mail 模塊發送郵件:https://blog.csdn.net/you_are_my_dream/article/details/60868329
?
把代碼中這個幾設置成你自己的參數:
self._smtp_host = "smtp.mxhichina.com" # 設置 服務器 self._smtp_port = 465 # 設置 端口 self._email_address = "xxxxxxxx@xxx.com" # 用戶名 self._email_password = "xxxxxxxxxxxxxxx" # 口令完整代碼( send_email_attach.py )
# -*- coding: utf-8 -*- # @Author : # @File : temp.py # @Software: PyCharm # @description : XXXimport smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header import optparseclass SendEmail(object):def __init__(self):super(SendEmail, self).__init__()# 第三方 SMTP 服務self._smtp_host = "smtp.mxhichina.com" # 設置 服務器self._smtp_port = 465 # 設置 端口# 默認的發送郵件的郵箱地址和密碼。# 當沒有傳遞發送者的郵箱地址和密碼時,使用默認的郵箱地址和密碼發送self._email_address = "xxxxxxxx@xxx.com" # 用戶名self._email_password = "xxxxxxxxxxxxxxx" # 口令self.frm = Noneself.pwd = Noneself.to = Noneself.email_title = Noneself.email_content = Noneself.attach_path = Noneself.attach_path_list = Nonepassdef set_args(self, frm=None, pwd=None, to=None, email_title=None, email_content=None, attach_path=None):"""設置參數:param frm: 發送者郵箱地址:param pwd: 發送者郵箱密碼:param to: 接收者郵箱地址,多個接收者時以逗號','分割:param email_title: 郵件標題:param email_content:郵件內容:param attach_path: 附件路徑,多個附件時以逗號','分割:return:"""if frm:self.frm = frmif not pwd:raise Exception('設置郵箱密碼')else:self.pwd = pwdelse:self.frm = self._email_addressself.pwd = self._email_passwordself.to = toself.email_title = email_titleself.email_content = email_contentself.attach_path = attach_path# 把逗號分割的附件路徑變成 listif self.attach_path is not None:self.attach_path_list = self.attach_path if ',' not in self.attach_path else self.attach_path.split(',')def send_email(self):multi_part = MIMEMultipart()multi_part['From'] = self.frmmulti_part['To'] = self.tomulti_part['Subject'] = Header(self.email_title, "utf-8")# 添加 郵件 內容msg = self.email_contentemail_body = MIMEText(msg, 'plain', 'utf-8')multi_part.attach(email_body)# 添加附件if isinstance(self.attach_path_list, str):# 只有一個附件attach = MIMEText(open(self.attach_path, 'rb').read(), 'base64', 'utf-8')attach["Content-Type"] = 'application/octet-stream'# filename not strictattach_file_name = self.attach_path_list.split('/')[-1]attach["Content-Disposition"] = 'attachment; filename="{0}"'.format(attach_file_name)multi_part.attach(attach)elif isinstance(self.attach_path_list, list):# 多個附件for item in self.attach_path_list:attach = MIMEText(open(item, 'rb').read(), 'base64', 'utf-8')attach["Content-Type"] = 'application/octet-stream'# filename not strictattach_file_name = item.split('/')[-1]attach["Content-Disposition"] = 'attachment; filename="{0}"'.format(attach_file_name)multi_part.attach(attach)# ssl 協議安全發送smtp_server = smtplib.SMTP_SSL(host=self._smtp_host, port=self._smtp_port)try:smtp_server.login(self.frm, self.pwd)smtp_server.sendmail(self.frm, self.to, multi_part.as_string())except smtplib.SMTPException as e:print("send fail", e)else:print("send success")finally:try:smtp_server.quit()except smtplib.SMTPException:print("quit fail")else:print("quit success")if __name__ == '__main__':parse = optparse.OptionParser(usage='"usage : %prog [options] arg1,arg2"', version="%prog 1.2")parse.add_option('-t', '--to', dest='to', action='store', type=str, metavar='to',help='接收者的郵箱地址, 多個接收者時以逗號 "," 分隔')parse.add_option('-f', '--from', dest='frm', type=str, metavar='from',help='發送者的郵箱地址')parse.add_option('-p', '--pwd', dest='pwd', type=str, metavar='pwd',help='發送者的郵箱密碼')parse.add_option('-T', '--title', dest='email_title', type=str, metavar='title',help='郵件標題')parse.add_option('-C', '--content', dest='email_content', type=str, metavar='content',help='郵件內容')parse.add_option('-A', '--attach', dest='attach_path', type=str, metavar='attach',help='郵件的附件路徑, 多個附件時以逗號 "," 分隔')parse.add_option('-v', help='help')options, args = parse.parse_args()temp_send = SendEmail()temp_send.set_args(frm=options.frm, pwd=options.pwd, to=options.to,email_title=options.email_title,email_content=options.email_content,attach_path=options.attach_path)temp_send.send_email()可以執行 :python3 send_email_attach.py -h 查看幫助
發送郵件:
郵箱截圖
?
?
?
總結
以上是生活随笔為你收集整理的Python 使用 smtp ssl 模式 发送邮件与附件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 模拟实现string其中的一些知识点
- 下一篇: 安卓逆向_22( 一 ) --- Xpo