简体   繁体   中英

Outlook and Thunderbird do not receive attachment when I send from Python using Mime

I was trying to send an email with attachment using MimeBase in python. I can even send the email and receive the pdf attachment when I'm not using Thunderbird or Outlook and open the mailbox in the browser.

mensagem = MIMEMultipart('alternative')
mensagem['Subject'] = Header(sTituloEmail.encode('utf-8'), 'UTF-8').encode()
mensagem['To'] = Header(sEmailTo.encode('utf-8'), 'UTF-8').encode()
mensagem['CC'] = Header(sEmailCC.encode('utf-8'), 'UTF-8').encode()
mensagem['From'] = Header(sEmailFrom.encode('utf-8'), 'UTF-8').encode()

# Corpo da mensagem
mensagem.attach(MIMEText(sTextEmail.encode('utf-8'), 'html', 'UTF-8'))

## Arquivos anexos.
mime = MIMEBase('application', 'x-pdf') #I've used pdf too and did the same thing
mime.set_payload(open('out.pdf', 'rb').read())
encoders.encode_base64(mime)
mime.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename('out.pdf'))
mensagem.attach(mime)

Make sure your email is mensagem = MIMEMultipart('mixed')
and try mime = MIMEBase('application', "octet-stream") .

If that will not help, share you complete code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM