Python自动发邮件的10个实用代码片段


【蜂邮EDM】:邮件群发系统,EDM邮件营销平台,邮件代发服务。 查看价格
【AokSend邮件API】:触发式邮件API,15元/万封,99%送达率。 查看价格
【烽火邮箱】:新人领取免费域名邮箱,可用作企业邮箱公司邮箱。 查看价格
Python 自动发邮件的 10 个实用代码片段
目录
- 设置发送地址和密码
- 使用SMTplib发送邮件
- 在邮件中添加附件
- 设置邮件主题
- 设置bcc和cc地址
- 批量发送多封邮件
- 批量发送邮件到多个地址
- 邮件正文和附件
- 邮件正文和附件优化
- 邮件正文和附件模板
1. 设置发送地址和密码
在发送邮件之前,必须先设置发送地址和密码。以下是使用 Python 邮件库设置发送地址和密码的示例代码:
# 设置发送地址和密码
from smtplib import *
import os
# 发送地址
from_address = 'your_send_address@example.com'
# 密码
from_pass = 'your_send_password'
# 如果需要使用安全选项
start_server()
set_debug Send()
mail_from = from_address
2. 使用SMTplib发送邮件
SMTplib 是 Python 的内置邮件库,用于发送电子邮件。以下是使用 SMTplib 发送邮件的示例代码:
# 使用 SMTplib 发送邮件
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from smtplib import *
import os
# 创建一个包含多个邮件的 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 msg
def 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 发送邮件
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)
# 示例调用
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 MIMEMultipart
from email.mime.text import MIMEText
from email附件 import MIMEPart
from smtplib import *
import os
def 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'] = to
msg['Subject'] = subject
msg.attach(MIMEText(text, 'plain', 'utf-8'))
add_attachment(msg, filename, content)
# 发送邮件
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地址
在发送邮件时,可以设置 bcc 和 cc 地址。bcc 表示抄送地址,cc 表示Carbon Copy地址。以下是示例代码:
【烽火邮箱】:烽火邮箱是一款简洁高效的企业邮箱平台,新客户赠送免费企业邮箱,一个起卖、按月付费(低至9.9元);支持别名邮箱及群组邮箱,支持定制无限邮箱。高权重纯净IP池,系统自带反垃圾机制。
立即查看 >> :企业邮箱价格
【蜂邮EDM】:邮件群发系统,EDM邮件营销平台,邮件代发服务,专业研发定制邮件营销系统及邮件群发解决方案!蜂邮自研产品线主要分为标准版、外贸版、企业版、定制版,及邮件API邮件SMTP接口服务。
立即查看 >> :邮件发送价格
【AokSend邮件API】:专注触发式邮件API发送服务。15元/万封,发送验证码邮件、忘记密码邮件、通知告警邮件等,不限速。综合送达率99%、进箱率98%。触发邮件也叫事务性邮件或推送邮件,包含:验证码邮件、重置密码邮件、余额提醒邮件、会员到期邮件、账号认证邮件等!
立即查看 >> :邮件发送价格
# 设置bcc和cc地址
bcc = ['bcc@example.com']
cc = ['cc@example.com']
6. 批量发送多封邮件
要批量发送多封邮件,可以使用 smtplib 库的 start Relay 方法。以下是示例代码:
# 批量发送多封邮件
from smtplib import *
import os
# 发送地址
from_address = 'your_send_address@example.com'
# 消息列表
messages = [
'Hello, this is a test email.',
'This is another test email.'
]
# 发送地址列表
to_addrs = ['recipient1@example.com', 'recipient2@example.com']
# 设置密码
from_pass = 'your_send_password'
# 如果需要使用安全选项
start_server()
set_debug Send()
mail_from = from_address
for i, to in enumerate(to_addrs):
msg = ''
msg += 'From: %s

【蜂邮EDM】:邮件群发系统,EDM邮件营销平台,邮件代发服务。 查看价格
【AokSend邮件API】:触发式邮件API,15元/万封,99%送达率。 查看价格
【烽火邮箱】:新人领取免费域名邮箱,可用作企业邮箱公司邮箱。 查看价格
