简体   繁体   English

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

[英]Sending HTML Mails from Amazon SES using PHP SDK

I am working on a service that sends emails from AWS SES service. 我正在开发一项从AWS SES服务发送电子邮件的服务。 I have been able to send plain text mails but I require to send rich HTML mails in one of the case. 我已经能够发送纯文本邮件,但我需要在其中一个案例中发送丰富的HTML邮件。 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. message_mail是放在表格中的一些HTML文本。 I have tried both send_email and send_raw_email but both of them did not work. 我已经尝试了send_email和send_raw_email,但它们都没有用。 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): 这对我有用(不使用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);

You can get ses.php from http://www.orderingdisorder.com/aws/ses/ 你可以从http://www.orderingdisorder.com/aws/ses/获得ses.php

I tried using the SES SDK and it was not easy to work with at all. 我尝试使用SES SDK,并不容易使用。 I ended up using PHPMailer to connect to SES through SMTP. 我最终使用PHPMailer通过SMTP连接到SES。 First, setup SMTP access from within Amazon SES and then add these lines to PHPMailer to have it connect to SES via TLS: 首先,从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 is very good at rich email (with text fallback), embedded images, and attachments. PHPMailer非常擅长丰富的电子邮件(带有文本回退),嵌入式图像和附件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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