【问题标题】:Send E-mail with Attachment which is a pdf发送带有pdf附件的电子邮件
【发布时间】:2018-12-20 08:14:17
【问题描述】:

尝试发送带有 PDF 附件的电子邮件。我可以发送电子邮件,但它只会附加 pdf 文件。没有随电子邮件发送的正文。

import smtplib
from email.mime.text import MIMEText

from email.message import EmailMessage
#from email import encoders

email_user = 'my.email@email.com'
email_send = input('Enter address to Send e-mail to: ')
subject = 'Python!'

msg = EmailMessage()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject

body = 'Hi there, This is a test E-mail from Python!' #This part is missing
msg.attach(MIMEText(body, 'plain')) 


filename = 'my_file'


with open(filename, 'rb') as content_file:
    content = content_file.read()
    msg.add_attachment(content, maintype='application/pdf', subtype='pdf', filename=filename)

text = msg.as_string()






server = smtplib.SMTP('server',port)
server.sendmail(email_user, email_send, text)
server.quit

【问题讨论】:

    标签: python email-attachments


    【解决方案1】:

    代替

       msg.attach(MIMEText(body, 'plain')) 
    

    下面试试

     msg.set_content(body)
    

    【讨论】:

    • 欢迎,享受编码! !!
    猜你喜欢
    • 2011-10-09
    • 2020-11-20
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 2014-05-29
    • 1970-01-01
    相关资源
    最近更新 更多