2012-09-22 49 views
2

我使用亞馬遜SES發送電子郵件,我想在它發送給用戶之前附加自定義標題,因爲我正在創建代理電子郵件系統,用於回覆線程在我的網站上,所以ID保留用於跟蹤哪個線程發送到電子郵件。附加亞馬遜SES服務的自定義電子郵件標頭

我看不出我可以從亞馬遜SES的文檔從this page這也解釋了他們接受什麼樣的信息,但是並沒有說如何綁定它,我用這SES wrapper爲PHP做附加一個自定義標題,分開。

我想用一個數字注入一個名爲X-Thread-ID的標題,我該如何繼續?


編輯:對於傑克的答案,我不能正確地發送電子郵件,它不斷給我這個錯誤:

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

我的頭也正是這樣的

To: [email protected] <YES> 
From: [email protected] <MySite> 
X-Thread-ID: 429038 

回答

6

我不知道你是如何連接到你當前的包裝,但我只是使用亞馬遜SDK的PHP附帶的,可以從亞馬遜本身下載。

$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: [email protected] <YES> 

應(逆轉):

To: YES <[email protected]> 

雙引號應該用於帶有空格的名稱。

+0

+1的官方SDK。快速,簡單,直接從亞馬遜。 – ceejayoz

+0

@Jack我有一個關於我遇到的錯誤的問題。 – MacMac

+0

你好,你能幫忙嗎? :-) – MacMac

0

快進到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 [email protected] with your "From" address. 
// This address must be verified with Amazon SES. 
$mail->setFrom('[email protected]', 'Sender Name'); 

// Replace [email protected] 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('[email protected]', '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; 
} 
?>