Python自動發(fā)郵件的10個實用代碼片段


【蜂郵EDM】:郵件群發(fā)系統(tǒng),EDM郵件營銷平臺,郵件代發(fā)服務。 查看價格
【AokSend郵件API】:觸發(fā)式郵件API,15元/萬封,99%送達率。 查看價格
【烽火郵箱】:新人領取免費域名郵箱,可用作企業(yè)郵箱公司郵箱。 查看價格
Python 自動發(fā)郵件的 10 個實用代碼片段
目錄
- 設置發(fā)送地址和密碼
- 使用SMTplib發(fā)送郵件
- 在郵件中添加附件
- 設置郵件主題
- 設置bcc和cc地址
- 批量發(fā)送多封郵件
- 批量發(fā)送郵件到多個地址
- 郵件正文和附件
- 郵件正文和附件優(yōu)化
- 郵件正文和附件模板
1. 設置發(fā)送地址和密碼
在發(fā)送郵件之前,必須先設置發(fā)送地址和密碼。以下是使用 Python 郵件庫設置發(fā)送地址和密碼的示例代碼:
?????? 【烽火郵箱】:烽火郵箱是一款簡潔高效的企業(yè)郵箱平臺,新客戶贈送免費企業(yè)郵箱,一個起賣、按月付費(低至9.9元);支持別名郵箱及群組郵箱,支持定制無限郵箱。高權(quán)重純凈IP池,系統(tǒng)自帶反垃圾機制。
立即查看 >> :企業(yè)郵箱價格
【蜂郵EDM】:郵件群發(fā)系統(tǒng),EDM郵件營銷平臺,郵件代發(fā)服務,專業(yè)研發(fā)定制郵件營銷系統(tǒng)及郵件群發(fā)解決方案!蜂郵自研產(chǎn)品線主要分為標準版、外貿(mào)版、企業(yè)版、定制版,及郵件API郵件SMTP接口服務。
立即查看 >> :郵件發(fā)送價格
【AokSend郵件API】:專注觸發(fā)式郵件API發(fā)送服務。15元/萬封,發(fā)送驗證碼郵件、忘記密碼郵件、通知告警郵件等,不限速。綜合送達率99%、進箱率98%。觸發(fā)郵件也叫事務性郵件或推送郵件,包含:驗證碼郵件、重置密碼郵件、余額提醒郵件、會員到期郵件、賬號認證郵件等!
立即查看 >> :郵件發(fā)送價格
# 設置發(fā)送地址和密碼from smtplib import *import os# 發(fā)送地址from_address = 'your_send_address@example.com'# 密碼from_pass = 'your_send_password'# 如果需要使用安全選項start_server()set_debug Send()mail_from = from_address
2. 使用SMTplib發(fā)送郵件
SMTplib 是 Python 的內(nèi)置郵件庫,用于發(fā)送電子郵件。以下是使用 SMTplib 發(fā)送郵件的示例代碼:
# 使用 SMTplib 發(fā)送郵件from email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom smtplib import *import os# 創(chuàng)建一個包含多個郵件的 MIME 多部分對象class EmailMessage: def __init__(self, to_addrs, subject, body): self.to = to_addrs self.subject = subject self.body = body def __str__(self): msg = """\ From: %s\ To: %s\ Subject: %s\ Body: %s\ """ % ( self.from_, self.to, self.subject, self.body) return msgdef sendEmail(to, subject, text): # 生成一個包含多封郵件的 MIME 多部分對象 msg = MIMEMultipart() msg['From'] = 'your_send_address@example.com' msg['To'] = to msg['Subject'] = subject msg.attach(MIMEText(text, 'plain', 'utf-8')) # 使用 SMTplib 發(fā)送郵件 try: server = SMTP('smtp.example.com', 587) server.starttls() server.sendmail('your_send_address@example.com', to, msg.as_string()) server.quit() except Exception as e: print("Error sending email:", e)# 示例調(diào)用to = 'your Recipient@example.com'subject = 'Test Email'text = 'Hello, this is a test email.'sendEmail(to, subject, text)
3. 在郵件中添加附件
要在郵件中添加附件,可以使用 MIMEFile
類來添加附件。以下是示例代碼:
# 在郵件中添加附件from email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom email附件 import MIMEPartfrom smtplib import *import osdef add_attachment(msg, filename, content): attachment = MIMEPart() attachment.add_header('Content-Disposition', 'attachment', filename=filename) attachment.add_header('Content-Type', 'application/octet-stream') attachment.set_payload(content) msg.attach(attachment)# 示例to = 'your Recipient@example.com'subject = 'Test Email with Attachment'text = 'Hello, this is a test email.'filename = 'test.pdf'content = 'content of the file'msg = MIMEMultipart()msg['From'] = 'your_send_address@example.com'msg['To'] = tomsg['Subject'] = subjectmsg.attach(MIMEText(text, 'plain', 'utf-8'))add_attachment(msg, filename, content)# 發(fā)送郵件try: server = SMTP('smtp.example.com', 587) server.starttls() server.sendmail('your_send_address@example.com', to, msg.as_string()) server.quit()except Exception as e: print("Error sending email:", e)
4. 設置郵件主題
郵件主題是用戶與收件人溝通的第一步,因此必須確保郵件主題清晰、簡潔且有吸引力。以下是設置郵件主題的示例:
# 設置郵件主題subject = '郵件主題'
5. 設置bcc和cc地址
在發(fā)送郵件時,可以設置 bcc
和 cc
地址。bcc
表示抄送地址,cc
表示Carbon Copy地址。以下是示例代碼:
# 設置bcc和cc地址bcc = ['bcc@example.com']cc = ['cc@example.com']
6. 批量發(fā)送多封郵件
要批量發(fā)送多封郵件,可以使用 smtplib
庫的 start Relay
方法。以下是示例代碼:
# 批量發(fā)送多封郵件from smtplib import *import os# 發(fā)送地址from_address = 'your_send_address@example.com'# 消息列表messages = [ 'Hello, this is a test email.', 'This is another test email.']# 發(fā)送地址列表to_addrs = ['recipient1@example.com', 'recipient2@example.com']# 設置密碼from_pass = 'your_send_password'# 如果需要使用安全選項start_server()set_debug Send()mail_from = from_addressfor i, to in enumerate(to_addrs): msg = '' msg += 'From: %s


【蜂郵EDM】:郵件群發(fā)系統(tǒng),EDM郵件營銷平臺,郵件代發(fā)服務。 查看價格
【AokSend郵件API】:觸發(fā)式郵件API,15元/萬封,99%送達率。 查看價格
【烽火郵箱】:新人領取免費域名郵箱,可用作企業(yè)郵箱公司郵箱。 查看價格
掃描二維碼推送至手機訪問。
版權(quán)聲明:本文由MailBing郵件營銷博客發(fā)布,如需轉(zhuǎn)載請注明出處。