下面是Python实现发送带有pdf附件的电子邮件的完整攻略。
1. 准备工作
在开始编写代码之前,需要对电子邮件的相关知识进行了解和掌握,并且需要使用第三方库,如Python内置的smtplib库和email库。在使用这些库之前,需要先安装相应的库。
在开始编写代码之前,确定目标收件人的邮箱地址、电子邮件主题和主体内容。同时准备好要发送的pdf文档。
2. 编写代码
下面是一段Python代码,它实现了发送带有pdf附件的电子邮件:
import smtplib, email.utils
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
# 准备邮件内容
msg = MIMEMultipart()
msg['To'] = email.utils.formataddr(('Recipient', 'recipient@example.com'))
msg['From'] = email.utils.formataddr(('Author', 'author@example.com'))
msg['Subject'] = 'PDF attachment'
body = MIMEText('Please see the attached PDF document.')
msg.attach(body)
# 添加附件
attachment_filename = 'document.pdf'
with open(attachment_filename, 'rb') as f:
attachment = MIMEBase('application', 'octet-stream')
attachment.set_payload(f.read())
encoders.encode_base64(attachment)
attachment.add_header('Content-Disposition', f'attachment; filename="{attachment_filename}"')
msg.attach(attachment)
# 发送邮件
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('you@gmail.com', 'yourpassword')
server.sendmail('you@gmail.com', 'recipient@example.com', msg.as_string())
server.quit()
以上代码中,我们首先使用MIMEMultipart
类来创建一个多部分邮件,然后设置邮件的主题、发件人、收件人和主体内容,接着将准备好的pdf附件添加到多部分邮件中,最后使用smtplib库发送邮件。
3. 示例说明
示例1:发送多个附件
当需要发送多个pdf附件时,我们只需要将以上代码中添加附件的部分放到一个循环中,例如:
import smtplib, email.utils
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
# 准备邮件内容
msg = MIMEMultipart()
msg['To'] = email.utils.formataddr(('Recipient', 'recipient@example.com'))
msg['From'] = email.utils.formataddr(('Author', 'author@example.com'))
msg['Subject'] = 'PDF attachments'
body = MIMEText('Please see the attached PDF documents.')
msg.attach(body)
# 添加多个附件
attachment_filenames = ['document1.pdf', 'document2.pdf', 'document3.pdf']
for attachment_filename in attachment_filenames:
with open(attachment_filename, 'rb') as f:
attachment = MIMEBase('application', 'octet-stream')
attachment.set_payload(f.read())
encoders.encode_base64(attachment)
attachment.add_header('Content-Disposition', f'attachment; filename="{attachment_filename}"')
msg.attach(attachment)
# 发送邮件
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('you@gmail.com', 'yourpassword')
server.sendmail('you@gmail.com', 'recipient@example.com', msg.as_string())
server.quit()
示例2:使用HTML格式的电子邮件主体内容
当需要使用HTML格式的电子邮件主体内容时,我们只需要将主体内容改为MIMEText
类的一个实例,并设置subtype='html'
,例如:
import smtplib, email.utils
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
# 准备邮件内容
msg = MIMEMultipart()
msg['To'] = email.utils.formataddr(('Recipient', 'recipient@example.com'))
msg['From'] = email.utils.formataddr(('Author', 'author@example.com'))
msg['Subject'] = 'PDF attachment'
body = MIMEText('<h2>Please see the attached PDF document.</h2>', 'html')
msg.attach(body)
# 添加附件
attachment_filename = 'document.pdf'
with open(attachment_filename, 'rb') as f:
attachment = MIMEBase('application', 'octet-stream')
attachment.set_payload(f.read())
encoders.encode_base64(attachment)
attachment.add_header('Content-Disposition', f'attachment; filename="{attachment_filename}"')
msg.attach(attachment)
# 发送邮件
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('you@gmail.com', 'yourpassword')
server.sendmail('you@gmail.com', 'recipient@example.com', msg.as_string())
server.quit()
以上就是Python实现发送带有pdf附件的电子邮件的完整攻略,希望对你有帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python实现发送带有pdf附件的电子邮件 - Python技术站