Amazon SES send_email 带有空格/换行符的文本正文

mor*_*tte 7 email amazon-web-services boto3

我面临一个问题,即我的所有文本电子邮件都被压缩在一起,并且在发送过程中没有新行持续存在。

这是代码:

def send_ses_message(email_to, subject, body):
    ses = init_ses_internal()
    if ses:
        ses.send_email(
            Source='no-reply@domain.com',
            Destination={
                'ToAddresses': [
                    email_to,
                ],
            },
            Message={
                'Subject': {
                    'Data': subject,
                },
                'Body': {
                    'Text': {
                        'Data': body,
                        'Charset': 'UTF-8',
                    },
                    'Html': {
                        'Data': body,
                        'Charset': 'UTF-8',
                    },
                }
            },
            ReplyToAddresses=[
                'mharris@domain.com', # just in case someone replies to a no-reply@ address I'll receive them
            ],
            ReturnPath='mharris@domain.com', # bounce backs will come to me also
        )
        return True
Run Code Online (Sandbox Code Playgroud)

我最近尝试强制使用 UTF-8,希望可以让换行符持续存在。之后,我在应该存在新行的地方添加了 \n 。

以下是电子邮件的示例:

    def send_reset_email(self, email_to, unique_id):
        subject = "Company Password Reset"
        body = """
Hello!\n\n

We have received a request to reset your password.\n

Please click the link below to proceed with resetting your password. Note: this link will expire in 1 hour.\n\n

http://staging.domain.com/password/reset/{}\n\n

If you did not request this reset you can safely ignore this e-mail.\n\n

Thank you for choosing Company!\n\n

The Company Team\n
www.company.com\n
""".format(unique_id)
        send_ses_message(email_to, subject, body)
Run Code Online (Sandbox Code Playgroud)

请让我知道我可以做些什么来确保换行符在 Amazon SES 中保持不变。谢谢!

小智 7

编辑:我在 Outlook 2013 客户端上遇到了类似的问题。在换行符之前添加一个制表符对我有用。

替换\n\t\n\t\r\n

如何格式化电子邮件中的字符串以便 Outlook 打印换行符?


小智 6

SES 中的默认内容类型似乎是 text/html,因此使用<br>而不是 \n 对我有用


小智 6

我在将日志文件的内容(存储在变量中)发送到 HTML BODY 时遇到了类似的问题。将新行替换为"<br />"以下内容有助于解决问题。以下命令将文本中的所有换行符替换为"<br />".

mytext = mytext.split("\n").join("<br />")
Run Code Online (Sandbox Code Playgroud)