简体   繁体   中英

Sending HTML Mails from Amazon SES using PHP SDK

I am working on a service that sends emails from AWS SES service. I have been able to send plain text mails but I require to send rich HTML mails in one of the case. This is the code that I use:

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 is some HTML text put inside tables. I have tried both send_email and send_raw_email but both of them did not work. Do I need to do something extra or different?

I know it's old question but still writing an answer. And hoping it will help someone in future.

$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));

this worked for me (without using the sdk or 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);

You can get ses.php from http://www.orderingdisorder.com/aws/ses/

I tried using the SES SDK and it was not easy to work with at all. I ended up using PHPMailer to connect to SES through SMTP. First, setup SMTP access from within Amazon SES and then add these lines to PHPMailer to have it connect to SES via TLS:

$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 is very good at rich email (with text fallback), embedded images, and attachments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM