如何在python中的amazon SES send_email函数中添加list-unsubscribe标头?

时间:2013-10-05 05:49:58

标签: python boto amazon-ses email-headers

这是我对亚马逊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邮件的标题?我需要附加list-unsubscribe链接,用户可以取消订阅。

如果我发送普通邮件,那么如果部分在那里运行我无法添加标题,如消息['list-unsubscribe'] =“http://www.xyaz.com

2 个答案:

答案 0 :(得分:0)

尽管这是一个古老的问题,但我有一段时间遇到相同的问题,但无法获得答案。我通过分析原始邮件找到了正确的工作代码。

我缺少两件重要的事情。

  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()

希望这会有所帮助!

答案 1 :(得分:-3)

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