Python使用smtplib模塊發送電子郵件
使用方法都寫在程序裏面的註釋中,請緩慢享用,如果您覺得不錯可以點個贊哦🙂
代碼如下:
"""電子郵件發送模塊使用方法(email,smtplib)
===============================配置郵件正文模塊===============================
email用法:
from email.mime.text import MIMEText # 文本郵件對象(構造普通文本、超文本、文本附件)
from email.mime.image import MIMEImage # 圖片對象(構造圖片附件)
from email.mime.multipart import MIMEMultipart # 容器對象(構造一個容器,可以添加文本也可以添加附件)
-------------------------------使用MIMEText對象-------------------------------
# 構造普通文本
text_info = 'Hello Evan'
text_obj = MIMEText(text_info, 'plain', 'utf-8') # 第一個參數文本信息,第二個參數文本格式,第三個參數編碼格式
text_obj["Content-Disposition"] = 'attachment;filename="hello.txt"' # 如果要添加爲附件就一定要配置這行,否則會視爲普通文本
# 構造超文本附件
html_info = '<p>Hello Evan</p>'
html_obj = MIMEText(html_info, 'html', 'utf-8') # 第一個參數超文本信息,第二個參數超文本格式,第三個參數編碼格式
html_obj["Content-Disposition"] = 'attachment;filename="hello.html"'
# 構造base64數據流附件,用於發送文件的時候使用
txt_file = open('file_path', 'rb').read()
txt_obj = MIMEText(txt_file, 'base64', 'utf-8')
txt_obj["Content-Disposition"] = 'attachment;filename="hi.txt"'
-------------------------------使用MIMEImage對象-------------------------------
# 構造圖片附件
image_file = open('image_path', 'rb').read()
image_obj = MIMEImage(image_file)
image_obj["Content-Disposition"] = 'attachment;filename="image.png"'
-------------------------------使用MIMEMultipart對象-------------------------------
MIMEMultipart實例化有三個可選類型,分別爲:
* alternative: 郵件正文中包含純文本正文(text/plain)和超文本正文(text/html)
* related: 郵件正文中包含圖片,聲音等內嵌資源
* mixed: 郵件包含附件,圖片,文本等(默認爲這個,選擇mixed類型什麼都可以發)
multipart_obj = MIMEMultipart('mixed')
multipart_obj['subject'] = Header('Hello Evan’, 'utf-8') # 配置郵件主題
multipart_obj['From'] = '[email protected]' # 配置發件方郵箱
multipart_obj['To'] = '[email protected]' # 配置發件方郵箱
multipart_obj.attach(ready_obj) # 添加構造好的文本對象或者附件對象(txt_obj、image_obj、html_obj)
===============================發送郵件模塊===============================
smtplib用法:
import smtplib
receiver_mail = '[email protected]' # 接收方的郵箱地址,可以是一個字符串,也可以是一個列表(列表包含所有郵箱地址的字符串)
sender_mail='[email protected]' # 發送方的郵箱地址
auth_code = 'abc' # 發送方郵箱提供的第三方登錄授權碼(如QQ郵箱:設置-賬戶-開啓服務-開啓POP3/SMTP服務,然後點擊生成授權碼)
sftp_obj = smtplib.SMTP('smtp.qq.com', 25) # 實例化SMTP對象,端口號爲25
sftp_obj.login(sender_mail, auth_code) # 使用發送方郵箱賬號和第三方登錄授權碼模擬登錄
send_message = ready_obj # 構造好的文本對象或者附件對象(txt_obj、image_obj、html_obj、multipart_obj)
sftp_obj.sendmail(sender_mail, receiver_mail, send_message.as_string()) # 發送郵件
sftp_obj.close() # 關閉郵件發送客戶端
"""
# -*- coding:utf-8 -*-
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
__author__ = 'Evan'
def send_email(sender_email, receive_email, auth_code):
"""
發送電子郵件
:param sender_email: 發件郵箱
:param receive_email: 收件郵箱,可以是一個字符串,也可以是一個列表(列表包含所有郵箱地址的字符串)
:param auth_code: 發件郵箱的授權登錄密碼
:return:
"""
# 實例化容器對象
send_obj = MIMEMultipart('mixed')
send_obj['From'] = 'Evan Liu'
send_obj['To'] = receive_email
send_obj['subject'] = Header('This is a test', 'utf-8')
# 構造文本內容附件
text_info = 'Hello Evan'
text_obj = MIMEText(text_info, 'plain', 'utf-8')
text_obj["Content-Disposition"] = 'attachment;filename="test.txt"'
send_obj.attach(text_obj)
# 模擬登錄郵箱
sftp_obj = smtplib.SMTP('smtp.qq.com', 25)
sftp_obj.login(sender_email, auth_code)
# 發送郵件
sftp_obj.sendmail(sender_email, receive_email, send_obj.as_string())
sftp_obj.close()
print('發送郵件到({})成功'.format(receive_email))
if __name__ == '__main__':
send_email(sender_email='[email protected]', receive_email='[email protected]', auth_code='sample')