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('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 "这样的标题

4

2 回答 2

0

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

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

  1. 回复
  2. add_header 方法

该文档显示了工作代码:

AWS SES 文档

您只需要添加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()

希望这可以帮助!

于 2019-02-06T09:06:37.653 回答
-3

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

于 2021-05-24T07:47:38.253 回答