以下是一个使用Python的示例代码,可以将PDF文件保存到本地,并通过电子邮件发送出去。请确保您安装了Python的smtplib和email库。您需要提供自己的发件人和收件人的电子邮件地址,以及发件人的电子邮件密码。
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
def send_email_with_pdf(pdf_file_path, sender_email, sender_password, receiver_email):
# 创建一个带附件的邮件实例
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = 'PDF文件'
# 添加邮件正文
body = MIMEText('请查收附件中的PDF文件。')
msg.attach(body)
# 添加附件
with open(pdf_file_path, 'rb') as f:
attach = MIMEApplication(f.read(), _subtype="pdf")
attach.add_header('Content-Disposition', 'attachment', filename=pdf_file_path)
msg.attach(attach)
# 连接到SMTP服务器并发送邮件
with smtplib.SMTP("smtp.gmail.com", 587) as server:
server.starttls()
server.login(sender_email, sender_password)
server.sendmail(sender_email, receiver_email, msg.as_string())
# 示例用法
pdf_file_path = 'path/to/your/pdf/file.pdf'
sender_email = 'sender@gmail.com'
sender_password = 'password'
receiver_email = 'receiver@gmail.com'
send_email_with_pdf(pdf_file_path, sender_email, sender_password, receiver_email)
请注意,此示例使用了Gmail的SMTP服务器。如果您使用其他电子邮件提供商,请相应地更改SMTP服务器和端口。