Python SMTP發(fā)送郵件的13個(gè)實(shí)例與常見錯(cuò)誤解決辦法


【蜂郵EDM】:郵件群發(fā)系統(tǒng),EDM郵件營銷平臺(tái),郵件代發(fā)服務(wù)。 查看價(jià)格
【AokSend郵件API】:觸發(fā)式郵件API,15元/萬封,99%送達(dá)率。 查看價(jià)格
【烽火郵箱】:新人領(lǐng)取免費(fèi)域名郵箱,可用作企業(yè)郵箱公司郵箱。 查看價(jià)格
Python SMTP發(fā)送郵件的13個(gè)實(shí)例與常見錯(cuò)誤解決辦法
Python SMTP發(fā)送郵件的基本介紹
Python SMTP發(fā)送郵件是通過Python的smtplib庫實(shí)現(xiàn)的,該庫提供了與SMTP服務(wù)器的通信接口,支持郵件的發(fā)送。通過SMTP協(xié)議,Python能夠幫助開發(fā)者輕松實(shí)現(xiàn)郵件的自動(dòng)化發(fā)送。
Python SMTP發(fā)送郵件的13個(gè)實(shí)例
下面將通過13個(gè)實(shí)例,展示如何使用Python的smtplib庫發(fā)送郵件。
1. 發(fā)送簡單的文本郵件
最簡單的郵件發(fā)送方式是發(fā)送純文本郵件,代碼如下:
import smtplibfrom email.mime.text import MIMETextmsg = MIMEText("這是一封測試郵件")msg['From'] = "your_email@example.com"msg['To'] = "recipient_email@example.com"msg['Subject'] = "測試郵件"server = smtplib.SMTP('smtp.example.com')server.login("your_email@example.com", "your_password")server.sendmail("your_email@example.com", "recipient_email@example.com", msg.as_string())server.quit()
2. 發(fā)送帶附件的郵件
要發(fā)送帶附件的郵件,可以使用email庫中的MIME模塊,下面是代碼示例:
from email.mime.multipart import MIMEMultipartfrom email.mime.base import MIMEBasefrom email import encodersmsg = MIMEMultipart()msg['From'] = "your_email@example.com"msg['To'] = "recipient_email@example.com"msg['Subject'] = "帶附件的郵件"# 添加附件filename = "file.txt"attachment = open("file.txt", "rb")part = MIMEBase('application', 'octet-stream')part.set_payload(attachment.read())encoders.encode_base64(part)part.add_header('Content-Disposition', f"attachment; filename={filename}")msg.attach(part)server = smtplib.SMTP('smtp.example.com')server.login("your_email@example.com", "your_password")server.sendmail("your_email@example.com", "recipient_email@example.com", msg.as_string())server.quit()
3. 發(fā)送HTML郵件
發(fā)送HTML郵件可以使用MIMEText并指定郵件內(nèi)容的類型為HTML:
Powered By 蜂.郵.EDMfrom email.mime.text import MIMETextmsg = MIMEText("HTML郵件
這是一封HTML格式的郵件
", "html")msg['From'] = "your_email@example.com"msg['To'] = "recipient_email@example.com"msg['Subject'] = "HTML郵件"server = smtplib.SMTP('smtp.example.com')server.login("your_email@example.com", "your_password")server.sendmail("your_email@example.com", "recipient_email@example.com", msg.as_string())server.quit()
4. 發(fā)送帶多個(gè)收件人的郵件
如果您想一次發(fā)送郵件給多個(gè)收件人,可以在“To”字段中指定多個(gè)郵箱地址:
msg['To'] = "recipient1@example.com, recipient2@example.com"
5. 發(fā)送郵件并抄送給其他人
除了主要收件人,還可以使用“CC”字段抄送給其他人:
msg['Cc'] = "cc@example.com"
6. 發(fā)送帶有主題的郵件
郵件的主題通過設(shè)置“Subject”字段來指定:
msg['Subject'] = "郵件主題"
7. 發(fā)送郵件并加密
如果需要加密郵件內(nèi)容,可以使用SSL連接到SMTP服務(wù)器:
server = smtplib.SMTP_SSL('smtp.example.com', 465)
8. 發(fā)送批量郵件
通過循環(huán),可以批量發(fā)送郵件給多個(gè)用戶:
for recipient in recipient_list:server.sendmail("your_email@example.com", recipient, msg.as_string())
9. 發(fā)送郵件時(shí)處理錯(cuò)誤
在發(fā)送郵件時(shí),我們可以使用try-except塊來處理SMTP錯(cuò)誤:
try:server.sendmail("your_email@example.com", "recipient_email@example.com", msg.as_string())except smtplib.SMTPException as e:print(f"Error sending mail: {e}")
10. 發(fā)送帶有圖片的郵件
如果郵件中需要包含圖片,可以將圖片嵌入HTML郵件中:
?????? 【烽火郵箱】:烽火郵箱是一款簡潔高效的企業(yè)郵箱平臺(tái),新客戶贈(zèng)送免費(fèi)企業(yè)郵箱,一個(gè)起賣、按月付費(fèi)(低至9.9元);支持別名郵箱及群組郵箱,支持定制無限郵箱。高權(quán)重純凈IP池,系統(tǒng)自帶反垃圾機(jī)制。
立即查看 >> :企業(yè)郵箱價(jià)格
【蜂郵EDM】:郵件群發(fā)系統(tǒng),EDM郵件營銷平臺(tái),郵件代發(fā)服務(wù),專業(yè)研發(fā)定制郵件營銷系統(tǒng)及郵件群發(fā)解決方案!蜂郵自研產(chǎn)品線主要分為標(biāo)準(zhǔn)版、外貿(mào)版、企業(yè)版、定制版,及郵件API郵件SMTP接口服務(wù)。
立即查看 >> :郵件發(fā)送價(jià)格
【AokSend郵件API】:專注觸發(fā)式郵件API發(fā)送服務(wù)。15元/萬封,發(fā)送驗(yàn)證碼郵件、忘記密碼郵件、通知告警郵件等,不限速。綜合送達(dá)率99%、進(jìn)箱率98%。觸發(fā)郵件也叫事務(wù)性郵件或推送郵件,包含:驗(yàn)證碼郵件、重置密碼郵件、余額提醒郵件、會(huì)員到期郵件、賬號認(rèn)證郵件等!
立即查看 >> :郵件發(fā)送價(jià)格
html = "
"msg = MIMEText(html, 'html')msg.add_header('Content-ID', '')
11. 發(fā)送帶有鏈接的郵件
HTML郵件可以包含鏈接:
from email.mime.multipart import MIMEMultipartfrom email.mime.base import MIMEBasefrom email import encodersmsg = MIMEMultipart()msg['From'] = "your_email@example.com"msg['To'] = "recipient_email@example.com"msg['Subject'] = "帶附件的郵件"# 添加附件filename = "file.txt"attachment = open("file.txt", "rb")part = MIMEBase('application', 'octet-stream')part.set_payload(attachment.read())encoders.encode_base64(part)part.add_header('Content-Disposition', f"attachment; filename={filename}")msg.attach(part)server = smtplib.SMTP('smtp.example.com')server.login("your_email@example.com", "your_password")server.sendmail("your_email@example.com", "recipient_email@example.com", msg.as_string())server.quit()
0
12. 使用代理發(fā)送郵件
如果您的網(wǎng)絡(luò)需要使用代理,您可以通過設(shè)置代理來發(fā)送郵件:
from email.mime.multipart import MIMEMultipartfrom email.mime.base import MIMEBasefrom email import encodersmsg = MIMEMultipart()msg['From'] = "your_email@example.com"msg['To'] = "recipient_email@example.com"msg['Subject'] = "帶附件的郵件"# 添加附件filename = "file.txt"attachment = open("file.txt", "rb")part = MIMEBase('application', 'octet-stream')part.set_payload(attachment.read())encoders.encode_base64(part)part.add_header('Content-Disposition', f"attachment; filename={filename}")msg.attach(part)server = smtplib.SMTP('smtp.example.com')server.login("your_email@example.com", "your_password")server.sendmail("your_email@example.com", "recipient_email@example.com", msg.as_string())server.quit()
1
13. 發(fā)送郵件時(shí)指定發(fā)送時(shí)間
郵件發(fā)送的時(shí)間可以通過任務(wù)調(diào)度來控制,使用定時(shí)任務(wù)工具(如Cron)來自動(dòng)化郵件發(fā)送。
常見錯(cuò)誤與解決辦法
在使用Python的SMTP功能時(shí),您可能會(huì)遇到一些常見的錯(cuò)誤。下面是幾種常見錯(cuò)誤及其解決方法:
1. SMTPAuthenticationError
這個(gè)錯(cuò)誤通常表示您的郵箱用戶名或密碼錯(cuò)誤,檢查用戶名和密碼是否正確,并確保SMTP服務(wù)器支持身份驗(yàn)證。
2. SMTPConnectError
如果您無法連接到SMTP服務(wù)器,可能是因?yàn)榉?wù)器地址或端口號錯(cuò)誤,或網(wǎng)絡(luò)問題。
3. SMTPRecipientsRefused
這個(gè)錯(cuò)誤通常是由于收件人郵箱地址錯(cuò)誤導(dǎo)致的,檢查收件人地址的格式是否正確。
總結(jié)
通過以上的13個(gè)實(shí)例,您可以輕松使用Python的smtplib庫發(fā)送不同類型的郵件。若遇到問題,檢查SMTP服務(wù)器設(shè)置、郵件內(nèi)容格式或使用合適的錯(cuò)誤處理機(jī)制。對于需要批量發(fā)送郵件的用戶,可以選擇MailBing(https://www.mailbing.com/)等工具來提升郵件發(fā)送效率。