スキップしてコンテンツを表示

[Python] Unable to send emails with attachments of size greater than ~300KB via SES

0

I'm using the code below to send emails with attachments. There can be scenarios to send emails with multiple attachments (usually PDF files). Each file size will not exceed 2MB. The following code is only working if the file size is less than ~300KB. If the file size is smaller than ~300KB, email sent with attachment. if file size is larger an empty document sent with the name "no name" in the attachment.

Is there any limitation in sending files greater than 300KB via SES.

msg = MIMEMultipart("mixed")
msg["Subject"] = subject
msg["From"] = sender
msg["To"] = ", ".join(recipients)

for attachment in attachments:
       if os.path.exists(attachment):
           attachment_obj = MIMEApplication(open(attachment, "rb").read())
           attachment_obj.add_header(
                 "Content-Disposition",
                 "attachment",
                 filename=os.path.basename(attachment),
           )
           msg.attach(attachment_obj)
        
msg_body = MIMEMultipart("alternative")
email_body = MIMEText(body.encode(charset), "plain", charset)
msg_body.attach(email_body)
msg.attach(msg_body)

response = ses_client.send_raw_email(
                Source=sender,
                Destinations=recipients,
                RawMessage={
                    "Data": msg.as_string(),
                },
            )

Observed similar behaviour in Java also. but AWS docs suggests the maximum supported size is 10MB. What could be the reason for this?

1回答
0

Hello.
Message size is limited to 10 MB, including attachments.
This size is after Base64 encoding, so I thought it might be over 10 MB depending on the files used and the content of the message.
https://docs.aws.amazon.com/ses/latest/dg/quotas.html

エキスパート

回答済み 3年前

  • Hello, The size of file I tried is only 547KB (without any encoding)

  • Are messages and attachments less than 10 MB in size after encoding? Depending on the content of the attachment, it may be larger after encoding.

  • The attachment is around ~2MB after encoding. I just tried it with the below snippet: pdf_content = open('/tmp/file.pdf', 'rb').read() pdf_base64 = base64.b64encode(pdf_content).decode('utf-8') pdf_part = MIMEApplication(pdf_base64, 'pdf', Name='file.pdf') pdf_part.add_header('Content-Disposition', f'attachment; filename="file.pdf"') msg.attach(pdf_part) attachment_size = len(pdf_base64)

  • Will it not exceed 10MB even if encoded including the size of the body of the email, title, etc.?

  • yes, correct. For testing purpose just trying to send the attachment only (without body)

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

関連するコンテンツ