如何在 PHP 中使用 Amazon SES 发送 html 电子邮件
How to send html email using Amazon SES in PHP
我完全是 AWS 菜鸟。我今天配置了 AWS SES,现在我可以使用此代码向收件人发送电子邮件。
<?php
// Replace sender@example.com with your "From" address.
// This address must be verified with Amazon SES.
define('SENDER', 'sender email');
// Replace recipient@example.com with a "To" address. If your account
// is still in the sandbox, this address must be verified.
define('RECIPIENT', 'recipient email');
// Replace smtp_username with your Amazon SES SMTP user name.
define('USERNAME','my username');
// Replace smtp_password with your Amazon SES SMTP password.
define('PASSWORD','my password');
// 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.
define('HOST', 'email-smtp.us-west-2.amazonaws.com');
// The port you will connect to on the Amazon SES SMTP endpoint.
define('PORT', '587');
// Other message information
define('SUBJECT','Hello from Driffle!');
define('BODY','Test Email');
require_once 'Mail.php';
$headers = array (
'From' => SENDER,
'To' => RECIPIENT,
'Subject' => SUBJECT);
$smtpParams = array (
'host' => HOST,
'port' => PORT,
'auth' => true,
'username' => USERNAME,
'password' => PASSWORD
);
// Create an SMTP client.
$mail = Mail::factory('smtp', $smtpParams);
// Send the email.
$result = $mail->send(RECIPIENT, $headers, BODY);
if (PEAR::isError($result)) {
echo("Email not sent. " .$result->getMessage() ."\n");
} else {
echo("Email sent!"."\n");
}
?>
但是当我尝试发送 html 格式的电子邮件时。输出电子邮件 returns 纯文本。寻找通过 Amazon SES 发送 html 电子邮件的解决方案。
找到了。我做的有点不对,但这很有效...
添加这些 headers:
'Mime-Version' => '1.0',
'Content-Type' => 'text/html; charset="ISO-8859-1"',
然后将 body 作为 HTML 标记提交(开始时尽可能保持简单 - 它不一定是 "page")。
bh
在 $headers 中添加了内容类型。它对我有用
require_once 'Mail.php';
$headers = array (
'Content-Type' => "text/html; charset=UTF-8", // <- add this line
'From' => SENDER,
'To' => RECIPIENT,
'Subject' => SUBJECT);
我完全是 AWS 菜鸟。我今天配置了 AWS SES,现在我可以使用此代码向收件人发送电子邮件。
<?php
// Replace sender@example.com with your "From" address.
// This address must be verified with Amazon SES.
define('SENDER', 'sender email');
// Replace recipient@example.com with a "To" address. If your account
// is still in the sandbox, this address must be verified.
define('RECIPIENT', 'recipient email');
// Replace smtp_username with your Amazon SES SMTP user name.
define('USERNAME','my username');
// Replace smtp_password with your Amazon SES SMTP password.
define('PASSWORD','my password');
// 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.
define('HOST', 'email-smtp.us-west-2.amazonaws.com');
// The port you will connect to on the Amazon SES SMTP endpoint.
define('PORT', '587');
// Other message information
define('SUBJECT','Hello from Driffle!');
define('BODY','Test Email');
require_once 'Mail.php';
$headers = array (
'From' => SENDER,
'To' => RECIPIENT,
'Subject' => SUBJECT);
$smtpParams = array (
'host' => HOST,
'port' => PORT,
'auth' => true,
'username' => USERNAME,
'password' => PASSWORD
);
// Create an SMTP client.
$mail = Mail::factory('smtp', $smtpParams);
// Send the email.
$result = $mail->send(RECIPIENT, $headers, BODY);
if (PEAR::isError($result)) {
echo("Email not sent. " .$result->getMessage() ."\n");
} else {
echo("Email sent!"."\n");
}
?>
但是当我尝试发送 html 格式的电子邮件时。输出电子邮件 returns 纯文本。寻找通过 Amazon SES 发送 html 电子邮件的解决方案。
找到了。我做的有点不对,但这很有效...
添加这些 headers:
'Mime-Version' => '1.0', 'Content-Type' => 'text/html; charset="ISO-8859-1"',
然后将 body 作为 HTML 标记提交(开始时尽可能保持简单 - 它不一定是 "page")。
bh
在 $headers 中添加了内容类型。它对我有用
require_once 'Mail.php';
$headers = array (
'Content-Type' => "text/html; charset=UTF-8", // <- add this line
'From' => SENDER,
'To' => RECIPIENT,
'Subject' => SUBJECT);