您可以使用以下代码发送电子邮件,我使用的是SMTP:
message = MIMEMultipart('alternative')
message['From'] = 'test@gmail.com'
message['To'] = 'test@gmail.com'
email_body = '<html><body>Hello!</body></html>'
message.attach(MIMEText(email_body, 'html'))
text = message.as_string()
然后发送为:
smtpserver.sendmail(from_address, to_address, message_body)
当我从网址添加附件时,例如此网址:http://www.pdf995.com/samples/pdf.pdf
我添加了这段代码:
from email.MIMEBase import MIMEBase
from email import encoders
pdf = 'http://www.pdf995.com/samples/pdf.pdf'
filename = 'test_pdf'
import requests
webf = requests.get(pdf)
attachment = webf.content
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment)
encoders.encode_base64(part)
part.add_header('Content-Disposition',
"attachment", filename=filename)
它发送但不附加pdf。
我尝试在线搜索似乎无法找到解决方案。