前言
本篇總結了QQ郵箱和163郵箱發送郵件,郵件包含html中文和附件,可以發給多個收件人,專治各種不行,總之看完這篇麻麻再也不用擔心我的郵件收不到了。
以下代碼兼容python2和python3,運行無異常,放心大膽食用。
一、163郵箱
1.先導入smtplib庫用來發送郵件,導入MIMEText庫用來做純文本的郵件模板
3.先准備幾個跟發郵件相關的參數,每個郵箱的發件服務器都不一樣,以163為例,百度搜到發件服務器為:smtp.163.com
4.接下來就是寫郵件的主題和正文內容,正文這里用html格式的
5.最后調用發件服務
6.參考代碼:
# coding:utf-8
import smtplib
from email.mime.text import MIMEText
# ----------1.跟發件相關的參數------
smtpserver = "smtp.163.com" # 發件服務器
port = 0 # 端口
sender = "yoyo@163.com" # 賬號
psw = "**************" # 密碼
receiver = "283340479@qq.com" # 接收人
# ----------2.編輯郵件的內容------
subject = "這個是主題163"
body = '<p>這個是發送的163郵件</p>' # 定義郵件正文為html格式
msg = MIMEText(body, "html", "utf-8")
msg['from'] = sender
msg['to'] = "283340479@qq.com"
msg['subject'] = subject
# ----------3.發送郵件------
smtp = smtplib.SMTP()
smtp.connect(smtpserver) # 連服務器
smtp.login(sender, psw) # 登錄
smtp.sendmail(sender, receiver, msg.as_string()) # 發送
smtp.quit() # 關閉
二、QQ郵件
1.QQ郵箱是需要SSL認證的,這種郵箱跟上面的就有點不一樣了
2.找到QQ郵箱授權碼,打開QQ郵箱-設置-賬號-POP3開啟服務-開啟
(如果已經開啟了,不知道授權碼,就點溫馨提示里面的‘生成授權碼’)
3.發驗證短信獲取授權碼,照着提示發個短信,如何點我已發送,就會收到授權碼了
4.收到授權碼后復制,保存下來,這個就可以當QQ郵箱的密碼了
5.QQ郵箱發送郵件代碼,跟163有點不一樣,如下圖紅色框框:
6.參考代碼:
# coding:utf-8
import smtplib
from email.mime.text import MIMEText
# ----------1.跟發件相關的參數------
# smtpserver = "smtp.163.com" # 發件服務器
smtpserver = "smtp.qq.com"
port = 465 # 端口
sender = "283340479@qq.com" # 賬號
psw = "**************" # 密碼
receiver = "283340479@qq.com" # 接收人
# ----------2.編輯郵件的內容------
subject = "這個是主題QQ"
body = '<p>這個是發送的QQ郵件</p>' # 定義郵件正文為html格式
msg = MIMEText(body, "html", "utf-8")
msg['from'] = sender
msg['to'] = "283340479@qq.com"
msg['subject'] = subject
# ----------3.發送郵件------
# smtp = smtplib.SMTP()
# smtp.connect(smtpserver) # 連服務器
smtp = smtplib.SMTP_SSL(smtpserver, port)
smtp.login(sender, psw) # 登錄
smtp.sendmail(sender, receiver, msg.as_string()) # 發送
smtp.quit() # 關閉
三、兼容163和QQ郵箱
1.如果想兼容上面兩種方式發送郵件,只需把第三塊內容稍微改下,如下所示
四、發送帶附件
1.上面的MIMEText只能發送正文,無法帶附件,發送帶附件的需要導入另外一個模塊MIMEMultipart
2.先讀取要發送文件的內容,file_path是路徑的參數名
3.下圖紅色框框file_name參數是發送的附件重新命名
4.參考代碼:
# coding:utf-8
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# ----------1.跟發件相關的參數------
smtpserver = "smtp.163.com" # 發件服務器
port = 465 # 端口
sender = "yoyo@163.com" # 賬號
psw = "***********" # 密碼
receiver = "283340479@qq.com" # 接收人
# ----------2.編輯郵件的內容------
# 讀文件
file_path = "result.html"
with open(file_path, "rb") as fp:
mail_body = fp.read()
msg = MIMEMultipart()
msg["from"] = sender # 發件人
msg["to"] = receiver # 收件人
msg["subject"] = "這個我的主題" # 主題
# 正文
body = MIMEText(mail_body, "html", "utf-8")
msg.attach(body)
# 附件
att = MIMEText(mail_body, "base64", "utf-8")
att["Content-Type"] = "application/octet-stream"
att["Content-Disposition"] = 'attachment; filename="test_report.html"'
msg.attach(att)
# ----------3.發送郵件------
try:
smtp = smtplib.SMTP()
smtp.connect(smtpserver) # 連服務器
smtp.login(sender, psw)
except:
smtp = smtplib.SMTP_SSL(smtpserver, port)
smtp.login(sender, psw) # 登錄
smtp.sendmail(sender, receiver, msg.as_string()) # 發送
smtp.quit()
5.最后結果,有圖有真相
五、發給多個收件人
1.上面都是發給一個收件人,那么如何一次發給多個收件人呢?只需改兩個小地方
2.把receiver參數改成list對象,單個多個都是可以收到的
3.msg["to"]這個參數不能用list了,得先把receiver參數轉化成字符串,如下圖所示
4.參考代碼:
# coding:utf-8
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# ----------1.跟發件相關的參數------
smtpserver = "smtp.163.com" # 發件服務器
port = 465 # 端口
sender = "yoyo@163.com" # 賬號
psw = "*********" # 密碼
# receiver = ["xxxx@qq.com"] # 單個接收人也可以是list
receiver = ["xxxx@qq.com", "yoyo@qq.com"] # 多個收件人list對象
# ----------2.編輯郵件的內容------
# 讀文件
file_path = "result.html"
with open(file_path, "rb") as fp:
mail_body = fp.read()
msg = MIMEMultipart()
msg["from"] = sender # 發件人
msg["to"] = ";".join(receiver) # 多個收件人list轉str
msg["subject"] = "這個我的主題999" # 主題
# 正文
body = MIMEText(mail_body, "html", "utf-8")
msg.attach(body)
# 附件
att = MIMEText(mail_body, "base64", "utf-8")
att["Content-Type"] = "application/octet-stream"
att["Content-Disposition"] = 'attachment; filename="test_report.html"'
msg.attach(att)
# ----------3.發送郵件------
try:
smtp = smtplib.SMTP()
smtp.connect(smtpserver) # 連服務器
smtp.login(sender, psw)
except:
smtp = smtplib.SMTP_SSL(smtpserver, port)
smtp.login(sender, psw) # 登錄
smtp.sendmail(sender, receiver, msg.as_string()) # 發送
smtp.quit() # 關閉
六:郵件收不到的幾種原因:
1.Subject和正文內容不要用hello、hehe、test等單詞
2.from(發件人)和to(收件人)不要為空,
(要不然會被認為是垃圾郵件)
3.找不到的話,先看下垃圾信箱,是不是跑到垃圾箱了
4.如果前幾次可以收到,后來收不到了,需改下subject內容
(因為每次都是一個subject,系統也會拒收的,把subject內容設置為動態的是最好的)
5.部分郵箱是ssl加密了的,所以無法發送,如:qq郵箱
(用授權碼去登錄)
6.要是按照上面的步驟來報錯了,說明代碼抄錯了,多檢查幾次。
(以上代碼在python2和python3上都測試通過了)
