【问题标题】:How to add a list-unsubscribe header in amazon SES send_email function in python?如何在 python 的亚马逊 SES send_email 函数中添加列表取消订阅标头?
【发布时间】:2013-10-12 05:38:12
【问题描述】:

这是我的亚马逊 SES 的 Python 代码:

import mimetypes
from email import encoders
from email.utils import COMMASPACE
from email.mime.multipart import MIMEMultipart
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from boto.ses import SESConnection
class SESMessage(object):
    """
    Usage:

    msg = SESMessage('from@example.com', 'to@example.com', 'The subject')
    msg.text = 'Text body'
    msg.html = 'HTML body'
    msg.send()

    """

    def __init__(self, source, to_addresses, subject, **kw):
        self.ses = connection

        self._source = source
        self._to_addresses = to_addresses
        self._cc_addresses = None
        self._bcc_addresses = None

        self.subject = subject
        self.text = None
        self.html = None
        self.attachments = []

    def send(self):
        if not self.ses:
            raise Exception, 'No connection found'

        if (self.text and not self.html and not self.attachments) or \
           (self.html and not self.text and not self.attachments):
            return self.ses.send_email(self._source, self.subject,
                                       self.text or self.html,
                                       self._to_addresses, self._cc_addresses,
                                       self._bcc_addresses,
                                       format='text' if self.text else 'html')
        else:
            message = MIMEMultipart('alternative')

            message['Subject'] = self.subject
            message['From'] = self._source
            if isinstance(self._to_addresses, (list, tuple)):
                message['To'] = COMMASPACE.join(self._to_addresses)
            else:
                message['To'] = self._to_addresses

            message.attach(MIMEText(self.text, 'plain'))
            message.attach(MIMEText(self.html, 'html'))

根据amazon ses boto library,我可以通过 MIME 标头发送 html 或文本或带有附件的电子邮件,但我如何提及普通文本或 html 邮件的标头?我需要从用户可以取消订阅的附加列表取消订阅链接。

如果我发送普通邮件,那么如果部分在那里运行,我将无法添加像 message['list-unsubscribe'] = "http://www.xyaz.com" 这样的标题

【问题讨论】:

  • 您是否尝试过使用您正在创建的 MIMEMultipart 消息对象执行“message['list-unsubscribe'] = ...”?
  • 没关系,我看到您在询问非 MIME 消息。对不起。
  • 我认为您可以使用send-email-raw 并自己构建整个消息,包括您想要包含的其他标头。
  • @garnaat:这就是我的想法,根据 boto 库,我们可以使用 send_raw_email(raw_message, source=None,destinations=None) 发送消息,我应该在正文中附加所有标题?我正在使用 html 正文。
  • 如果您使用send_email_raw,您必须使用ToFrom 等编写完整的电子邮件消息、标题和正文。

标签: python boto amazon-ses email-headers


【解决方案1】:

$mail->AddReplyTo('mail@exmaple.com', '回复姓名'); $mail->AddCustomHeader("List-Unsubscribe: mailto:mail@example.com, http://example.com/unsubscribe/");

【讨论】:

    【解决方案2】:

    虽然这是一个老问题,但我曾经遇到过同样的问题,但无法得到答案。我通过分析原始邮件找到了正确工作的代码。

    我遗漏了两件重要的事情。

    1. 回复
    2. add_header 方法

    文档显示了工作代码:

    AWS SES documentation

    您只需添加 Reply-To 参数和 List-Unsubscribe 标头。

    这是工作代码。

    import os
    import boto3
    from botocore.exceptions import ClientError
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from email.mime.application import MIMEApplication
    
    
    def send_r_email():
        region_name = 'us-west-2'
        SENDER = "Google <no-reply@google.com>"
        RECIPIENT = "your-email@google.com"
        CONFIGURATION_SET = "your configuration set"
        SUBJECT = "Customer new subject contact info"
        BODY_TEXT = "Hello,\r\nPlease see the attached file for a list of customers to contact."
        BODY_HTML = """\
            <html>
            <head></head>
            <body>
            <h1>Hello!</h1>
            <p>Please see the attached file for a list of customers to contact.</p>
            </body>
            </html>
        """
        CHARSET = "utf-8"
        client = boto3.client('ses',region_name=region_name)
        msg = MIMEMultipart('mixed')
        msg['Subject'] = SUBJECT
        msg['From'] = SENDER
        msg['To'] = RECIPIENT
        msg['Reply-To'] = "Google <abc@google.com>"
        msg_body = MIMEMultipart('alternative')
        textpart = MIMEText(BODY_TEXT.encode(CHARSET), 'plain', CHARSET)
        htmlpart = MIMEText(BODY_HTML.encode(CHARSET), 'html', CHARSET)
        msg_body.attach(textpart)
        msg_body.attach(htmlpart)
        msg.attach(msg_body)
        msg.add_header('List-Unsubscribe', '<http://somelink.com>')
        try:
            #Provide the contents of the email.
            response = client.send_raw_email(
                Source=SENDER,
                Destinations=[
                    RECIPIENT
                ],
                RawMessage={
                    'Data':msg.as_string(),
                },
                ConfigurationSetName=CONFIGURATION_SET
            )
        except ClientError as e:
            print(e.response['Error']['Message'])
        else:
            print("Email sent! Message ID:")
            print(response['MessageId'])
    
    
    send_r_email()
    

    希望这会有所帮助!

    【讨论】:

    • 如何从 .msg 文件中获取 'List-Unsubscribe' 标头?
    猜你喜欢
    • 1970-01-01
    • 2011-01-25
    • 2017-09-15
    • 1970-01-01
    • 2019-05-08
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    相关资源
    最近更新 更多