发送带有pdf附件的电子邮件

时间:2018-12-19 19:27:04

标签: python email-attachments

尝试发送带有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

1 个答案:

答案 0 :(得分:0)

代替

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

尝试以下

 msg.set_content(body)