I'm getting a weird behaviour from gmail that I can't figure: I'm using the GMail API as described and I can send and receive messages. The Problem is, when I create a multipart message with a file attachment, that file is stripped from the message after sending. I.e. the attachment shows up fine in the Sent folder, but the receiver gets the message without attachment. I also checked the original message on both sides and on the sent message I have all 3 parts (1 text 2 html 3 png) and on the receiving side I only have parts 1 and 2. Fun twist: when I forward the message from my Sent folder to the same recipient, the attachment is sent.
The code-snippet for the message creation is the following:
# processing incoming email
for part in email_msg.walk():
if part.get_content_maintype() == 'multipart':
continue
if 'text/plain' in part.get_content_type():
payload += part.get_payload(decode=True)
elif 'text/html' in part.get_content_type():
html_payload += part.get_payload(decode=True)
elif part.get('Content-Disposition') is not None and part.get_filename() != 'smime.p7s':
attachments.append(
{'data_stream': part.get_payload(decode=True),
'filename': part.get_filename(),
'content_type': part.get_content_type()
}
)
#part creating the message
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = 'my_gsuite_address'
msg['To'] = user_mail
msg.attach(MIMEText(body_msg_plain, 'plain', "utf-8"))
if html_message != '':
body_msg_html = ISSUE_UPDATED_MESSAGE_HTML.format(
message=html_message,
footer=MM_FOOTER_PLAIN
)
msg.attach(MIMEText(body_msg_html, 'html', "utf-8"))
for attachment in attachments:
main_type, sub_type = attachment['content_type'].split('/')
if main_type == 'text':
mime_class = MIMEText
elif main_type == 'image':
mime_class = MIMEImage
elif main_type == 'audio':
mime_class = MIMEAudio
else:
mime_class = MIMEBase
attachment_part = mime_class(attachment['data_stream'], _subtype=sub_type)
attachment_part.add_header('Content-Disposition', 'attachment', filename=attachment["filename"])
msg.attach(attachment_part)
#part sending the message
self.service.users().messages().send(
userId=self.account,
body={'raw': base64.urlsafe_b64encode(msg.as_bytes()).decode('utf-8')}
).execute()
Any help would be greatly appreciated