使用PHP SDK从Amazon SES发送HTML邮件


Sending HTML Mails from Amazon SES using PHP SDK

我正在开发一项从AWS SES服务发送电子邮件的服务。我已经能够发送纯文本邮件,但我需要在一个情况下发送丰富的HTML邮件。这是我使用的代码:

header("MIME-Version: 1.0; Content-type: text/html; charset=iso-8859-1");
    require_once(dirname(__FILE__).'/AWSSDKforPHP/sdk.class.php');
// Instantiate the Amazon class
$ses = new AmazonSES();

$source = 'abc@www..com';
$dest = array('ToAddresses'=>array($to));
$message = CFComplexType::map(array('Subject.Data'=>$subject, 'Body.Html.Data'=>$message_mail));
$rSendEmail = $ses->send_email($source, $dest, $message);

message_mail是放在表中的一些HTML文本。我尝试了send_email和send_raw_email,但它们都不起作用。我需要做一些额外的或不同的事情吗?

我知道这是一个老问题,但仍然在写一个答案。并希望它能在未来帮助到别人。

$m = new SimpleEmailServiceMessage();
$m->addTo('receiver email address');
$m->setFrom('send email address');
$m->setSubject('testing!');
$body= '<b>Hello world</b>';
$plainTextBody = '';
$m->setMessageFromString($plainTextBody,$body);    
print_r($ses->sendEmail($m));

这对我有效(不使用SDK或smtp):

require_once('ses.php');
$ses = new SimpleEmailService('accessKey', 'secretKey');
$m = new SimpleEmailServiceMessage();
$m->addTo('addressee@example.com');
$m->setFrom('Name <yourmail@example.com>');
$m->setSubject('You have got Email!');
$m->setMessageFromString('Your message');
$ses->sendEmail($m);

您可以从http://www.orderingdisorder.com/aws/ses/

获取ses.php

我尝试使用SES SDK,但它根本不容易使用。我最终使用PHPMailer通过SMTP连接到SES。首先,在Amazon SES中设置SMTP访问,然后将以下行添加到PHPMailer中,使其通过TLS连接到SES:

$mail = new PHPMailer();
$mail->IsSMTP(true);
$mail->SMTPAuth = true;
$mail->Mailer = "smtp";
$mail->Host= "tls://email-smtp.us-east-1.amazonaws.com";
$mail->Port = 465;
$mail->Username = "";  // SMTP username (Amazon Access Key)
$mail->Password = "";  // SMTP Password (Amazon Secret Key)
// ... the rest of PHPMailer code here ...

PHPMailer非常擅长富电子邮件(带文本回退),嵌入图像和附件。