Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python smtplib.sendmail Mime Multipart body doesn't shown on iPhone

Here is my code:

FROM = ''
TO = ''
SMTPSERVER = ''
MYEMAILPASSWORD = ""

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.mime.base import MIMEBase
from email import encoders


def physicallySend(listOfAttachments):
    msg = MIMEMultipart('alternative')
    msg['Subject'] = "Testing"
    msg['From'] = FROM
    msg['To'] = TO

    textPart = MIMEText("Hello. I should be able to see this body.", 'plain')
    msg.attach(textPart)
    for attachmentFilename in listOfAttachments:
        fp = open(attachmentFilename, 'rb')
        file1 = MIMEBase('application','csv')
        file1.set_payload(fp.read())
        fp.close()
        encoders.encode_base64(file1)
        file1.add_header('Content-Disposition','attachment;filename=%s' % attachmentFilename)
        msg.attach(file1)

    server = smtplib.SMTP(SMTPSERVER)
    server.ehlo()
    server.starttls()
    server.login(FROM, MYEMAILPASSWORD)
    server.sendmail(FROM, to, msg.as_string())
    server.quit()
    return True


physicallySend(["testfile.csv"])

While I can see the text body fine on Gmail and Outlook, however on my iPhone (6.1.3) I only see the attachment, and not the body.

like image 306
Chris Avatar asked Apr 30 '14 20:04

Chris


1 Answers

I found my solution in this comment: How do I send attachments using SMTP?

My first line should have been

msg = MIMEMultipart('mixed')

rather than 'alternative'.

like image 141
Chris Avatar answered Oct 05 '22 23:10

Chris