【问题标题】:Attach custom email header for Amazon SES service附加 Amazon SES 服务的自定义电子邮件标头
【发布时间】:2012-09-14 20:58:36
【问题描述】:

我正在使用 Amazon SES 发送电子邮件,我想在它发送给用户之前附加一个自定义标题,因为我正在制作代理电子邮件系统,用于回复我网站上的线程,所以 ID保留用于跟踪与电子邮件一起发布到哪个线程。

我看不到如何从 Amazon SES 的文档中附加自定义标头,除了 this page 解释了他们接受哪些标头但没有说明如何绑定它,我正在使用这个 SES wrapper 为PHP。

我希望能够注入一个名为X-Thread-ID 的标头并带有一个数字,我将如何处理?


编辑:对于杰克的回答,我无法正确发送电子邮件,它一直给我这个错误:

CFSimpleXML Object
(
    [Type] => Sender
    [Code] => InvalidParameterValue
    [Message] => Missing final '@domain'
)

我的标题就是这样的

To: myemail@hotmail.co.uk <YES>
From: developer@mysite.com <MySite>
X-Thread-ID: 429038

【问题讨论】:

    标签: php email-headers amazon-ses


    【解决方案1】:

    我不确定您对当前包装器的依恋程度,但我只使用 Amazon SDK for PHP 附带的一个,可以从 Amazon 本身下载。

    $ses = new AmazonSES(AWS_ACCESS_KEY, AWS_ACCESS_SECRET);
    
    $headers = join("\r\n", array(
        "To: $recipient",
        "X-Thread-ID: 123test",
    ));
    $body = "<html><body> ... </body></html>";
    
    $res = $ses->send_raw_email(array(
        'Data' => chunk_split(base64_encode("$headers\r\n\r\n$body"))
    ), array());
    
    // check API result
    if (!$res->isOK()) {
        throw new Exception(print_r($res->body->Error, true));
    }
    // inspect message id
    $messageId = (string)$res->body->SendRawEmailResult->MessageId
    

    编辑

    此电子邮件标题:

    To: myemail@hotmail.co.uk <YES>
    

    应该(反转):

    To: YES <myemail@hotmail.co.uk>
    

    双引号应该用于带有空格的名称。

    【讨论】:

    • 官方 SDK +1。快速、简单、直接来自亚马逊。
    • @Jack 我有一个关于我遇到的错误的问题。
    • 哦!抱歉,我忘了说我已经编辑了我的帖子 - 您可以从那里阅读,抱歉!
    • @BurningtheCodeigniter 这是一个简单的错误,更新答案:)
    • @Jack Aha,我的错。谢谢你:-)
    【解决方案2】:

    快进到 2017 年,现在的答案是(根据亚马逊):

    (参考:Send an Email by Accessing the Amazon SES SMTP Interface Programmatically

    <?php
    
    // Modify the path in the require statement below to refer to the 
    // location of your Composer autoload.php file.
    require 'path_to_sdk_inclusion';
    
    // Instantiate a new PHPMailer 
    $mail = new PHPMailer;
    
    // Tell PHPMailer to use SMTP
    $mail->isSMTP();
    
    // Replace sender@example.com with your "From" address. 
    // This address must be verified with Amazon SES.
    $mail->setFrom('sender@example.com', 'Sender Name');
    
    // Replace recipient@example.com with a "To" address. If your account 
    // is still in the sandbox, this address must be verified.
    // Also note that you can include several addAddress() lines to send
    // email to multiple recipients.
    $mail->addAddress('recipient@example.com', 'Recipient Name');
    
    // Replace smtp_username with your Amazon SES SMTP user name.
    $mail->Username = 'smtp_username';
    
    // Replace smtp_password with your Amazon SES SMTP password.
    $mail->Password = 'smtp_password';
    
    // Specify a configuration set. If you do not want to use a configuration
    // set, comment or remove the next line.
    $mail->addCustomHeader('X-SES-CONFIGURATION-SET', 'ConfigSet');
    
    // If you're using Amazon SES in a region other than US West (Oregon), 
    // replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP  
    // endpoint in the appropriate region.
    $mail->Host = 'email-smtp.us-west-2.amazonaws.com';
    
    // The port you will connect to on the Amazon SES SMTP endpoint.
    $mail->Port = 465;
    
    // The subject line of the email
    $mail->Subject = 'Amazon SES test (SMTP interface accessed using PHP)';
    
    // The HTML-formatted body of the email
    $mail->Body = '<h1>Email Test</h1>
        <p>This email was sent through the 
        <a href="https://aws.amazon.com/ses">Amazon SES</a> SMTP
        interface using the <a href="https://github.com/PHPMailer/PHPMailer">
        PHPMailer</a> class.</p>';
    
    // Tells PHPMailer to use SMTP authentication
    $mail->SMTPAuth = true;
    
    // Enable SSL encryption
    $mail->SMTPSecure = 'ssl';
    
    // Tells PHPMailer to send HTML-formatted email
    $mail->isHTML(true);
    
    // The alternative email body; this is only displayed when a recipient
    // opens the email in a non-HTML email client. The \r\n represents a 
    // line break.
    $mail->AltBody = "Email Test\r\nThis email was sent through the 
        Amazon SES SMTP interface using the PHPMailer class.";
    
    if(!$mail->send()) {
        echo "Email not sent. " , $mail->ErrorInfo , PHP_EOL;
    } else {
        echo "Email sent!" , PHP_EOL;
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2012-06-22
      • 1970-01-01
      • 2016-05-14
      • 2013-10-31
      • 1970-01-01
      • 1970-01-01
      • 2012-09-13
      • 1970-01-01
      • 2020-08-28
      相关资源
      最近更新 更多