【问题标题】:Amazon SES send_email Text body with spacing/newlinesAmazon SES send_email 带有空格/换行符的文本正文
【发布时间】:2016-05-26 18:59:20
【问题描述】:

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

代码如下:

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

我最近尝试强制使用 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)

请告诉我如何确保换行符在 Amazon SES 中保持不变。谢谢!

【问题讨论】:

  • 我不明白你的问题。SES 不会删除新行。无需强制使用 UTF-8。你在发送什么,你在接收什么?将样本集限制为 2 或 3 行。
  • 我的意思是,当我发送在 send_reset_email 中看到的正文时,电子邮件的内容如下:“您好!我们收到了重置密码的请求。请单击下面的链接继续重置密码。注意:此链接将在 1 小时后过期。staging.company.com/password/reset/… 如果您没有请求此重置,您可以放心地忽略此电子邮件。感谢您选择公司!公司团队 www.company.com" - 没有格式化
  • 我也尝试了 \r\n 但没有成功 - 目前我为我发送的每封电子邮件实现了一个带有 .html 和 .text 文件的电子邮件模板系统,并且完整的 HTML 文件按预期工作。

标签: email amazon-web-services boto3


【解决方案1】:

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

【讨论】:

  • 这也适用于我,尝试其他转义字符无效。
【解决方案2】:

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

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

How do I format a String in an email so Outlook will print the line breaks?

【讨论】:

  • 完美!谢谢!我使用 SNS 主题进行一些简单的警报,SNS 不使用 text/html 以及我发现的所有其他示例都假设我是!
【解决方案3】:

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

mytext = mytext.split("\n").join("<br />")

【讨论】:

    猜你喜欢
    • 2021-09-16
    • 1970-01-01
    • 2012-09-28
    • 2019-09-02
    • 2018-04-19
    • 2013-04-12
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    相关资源
    最近更新 更多