如何添加列表退订头亚马逊SES SEND_EMAIL功能蟒蛇?

问题描述 投票:1回答:1

这是亚马逊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('[email protected]', '[email protected]', '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邮件头?我需要附件列表退订链接从那里用户可以退订。

如果我发送正常的邮件,然后如果一部分运行有我无法添加头像消息[“列表退订”] =“http://www.xyaz.com

python boto amazon-ses email-headers
1个回答
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 <[email protected]>"
    RECIPIENT = "[email protected]"
    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 <[email protected]>"
    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()

希望这可以帮助!

© www.soinside.com 2019 - 2024. All rights reserved.