0

I have some Python script that sends emails with an attachment. This attachment is passed as a bytes object and is always displayed on Outlook and Gmail web apps on my computer, and on Gmail and Outlook apps on iPhone and Android. However, it is never displayed on iPhone native Email app, which is called "Mail".

Here is my script, self._attachment is the PNG as bytes.

# Create a multipart message and set headers
# Create a multipart message and set headers
message = MIMEMultipart('related')
message["From"] = self.prepare_from_field(self._sender_name, self._sender_account)
message["Subject"] = self.prepare_email_subject(self._subject_type)
message["To"] = self.prepare_recipients(self._list_of_recipients)
message["Cc"] = self.prepare_recipients(self._list_of_cc)
message["Bcc"] = self.prepare_recipients(self._list_of_bcc)

# Encapsulate the plain and HTML versions of the message body in an
# 'alternative' part, so message agents can decide which one they want to display.
msgAlternative = MIMEMultipart('alternative')

body = self.prepare_body(self._list_of_texts, self._list_of_disclaimers, self._extra_code, None)

msgText = MIMEText(body, 'html')
msgAlternative.attach(msgText)
message.attach(msgAlternative)

# message.attach(MIMEText(body, "html"))

# Add attachment if required
if self._attachment is not None:
    message.attach(MIMEImage(self._attachment, name='Your png.png'))
# Prepare the entire email as string
text = message.as_string()

all_recipients = self.absolute_list_of_recipients(self._list_of_recipients, self._list_of_cc, self._list_of_bcc)

with smtplib.SMTP(at.smtp_config['smtp_server'], at.smtp_config['smtp_non_sll_port']) as server:

    context = ssl.create_default_context()
    server.starttls(context=context)
    server.ehlo()

    # Log in with credentials
    server.login(at.user_credentials['username'], at.user_credentials['password'])

    # Send the emailu
    server.sendmail(senders_account, recipient_address, message)
    return True

Would you know why iPhone's Mail app does not display the png file attached and how to solve it without compromising the display on other apps and devices?

Thank you in advance!

MartinHN
  • 124
  • 2
  • 9
  • 1
    If it works in one mailer, but not another, examining the mail's source is a good next step. – benc Mar 14 '23 at 22:55
  • I was examining different combinations with MIMEMultipart('alternative'), MIMEMultipart('mixed') and MIMEMultipart('related') and none of them worked, unfortunately... – MartinHN Apr 04 '23 at 16:50
  • The standard is pretty functional but it's the kind to topic where reading the spec isn't always helpful. You should check superuser and serverfault to see if anyone has discussed similar problems there. Sadly, this was a topic where I never got into the internals. If you want to take two emails that work + don't-work and put the **mail source** (not the API code) here, I could look at it. – benc May 27 '23 at 05:10

0 Answers0