php 使用 PHPMailer 和 Amazon SES
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23497876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using PHPMailer and Amazon SES
提问by BheemaSenan
I am using Amazon SES. I am trying to send an email from my PHPscript using PHPMailer.
我正在使用亚马逊 SES。我正在尝试使用PHPMailer从我的PHP脚本发送电子邮件。
I already verified twoemail ids and trying to send mail from and to this mail ids. But It throws the following error.
我已经验证了两个电子邮件 ID 并尝试从该邮件 ID 发送邮件和向该邮件 ID 发送邮件。但它会引发以下错误。
ERROR
错误
SERVER -> CLIENT: 220 email-smtp.amazonaws.com ESMTP SimpleEmailService-693939519 QEPGeLndQQq5vJ53VMXU
CLIENT -> SERVER: EHLO localhost
SERVER -> CLIENT: 250-email-smtp.amazonaws.com250-8BITMIME250-SIZE 10485760250- STARTTLS250-AUTH PLAIN LOGIN250 Ok
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 Ready to start TLS
CLIENT -> SERVER: EHLO localhost
SERVER -> CLIENT: 250-email-smtp.amazonaws.com250-8BITMIME250-SIZE 10485760250-STARTTLS250-AUTH PLAIN LOGIN250 Ok
CLIENT -> SERVER: AUTH LOGIN
SMTP NOTICE: EOF caught while checking if connected
SMTP connect() failed.
Mailer Error: SMTP connect() failed.
The following is my PHP Script:
以下是我的PHP 脚本:
<?php
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require 'phpmailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'email-smtp.us-east-1.amazonaws.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
// I tried PORT 25, 465 too
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "SES Secret ID";
//Password to use for SMTP authentication
$mail->Password = "SES Secret Key";
//Set who the message is to be sent from
$mail->setFrom('[email protected]', 'sender');
//Set who the message is to be sent to
$mail->addAddress('[email protected]', 'receiver');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->Body = 'This is a plain-text message body';
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
I tried so many solutions I found over the internet, especially stackoverflow.
我尝试了很多在互联网上找到的解决方案,尤其是stackoverflow。
Nothing works !!
什么都行不通!!
回答by Victor Gomes
I managed to make it work.
我设法让它工作。
$ mail-> Port = 587;
// Username to use for SMTP authentication - use full email address for gmail
$ mail-> Username = "Amazon SES Secret ID";
// Password to use for SMTP authentication
$ mail-> Password = "Amazon SES Secret Key";
To create credentials visit this link: https://console.aws.amazon.com/ses/home?region=us-east-1#smtp-settings:
要创建凭据,请访问此链接:https: //console.aws.amazon.com/ses/home?region=us-east-1#smtp-settings:
To send e-mail, both sending email, as the email you receive will necessarily have to be registered and checked by amazon. (https://console.aws.amazon.com/ses/home?region=us-east-1#verified-senders-mail:)
要发送电子邮件,发送电子邮件,因为您收到的电子邮件必须由亚马逊注册和检查。( https://console.aws.amazon.com/ses/home?region=us-east-1#verified-senders-mail:)
Any questions you can find me. I hope I helped
有什么问题可以找我。我希望我有所帮助
回答by leon
I had the same problem, I could only get it to work when I changed host name to start with ssl://. So this works:
我遇到了同样的问题,只有当我将主机名更改为以 ssl:// 开头时才能使其工作。所以这有效:
$mail = new PHPMailer;
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->SMTPDebug = 3; // 启用详细调试输出
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'ssl://email-smtp.us-west-2.amazonaws.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'blah'; // SMTP username
$mail->Password = 'blahblah'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 443;
回答by Fabien Snauwaert
To send email successfully with PHPMailer + AWS SES, I had to use these settings:
要使用 PHPMailer + AWS SES 成功发送电子邮件,我必须使用以下设置:
$mail->IsSMTP(true);
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host= "email-smtp.eu-west-1.amazonaws.com"; /** Edit to the region you're using **/
$mail->Port = 587;
$mail->Username = 'MYACCESSKEYID';
$mail->Password = 'MYSECRETACCESSKEY'
Taking it one step further, I extended the PHPMailer class to:
更进一步,我将 PHPMailer 类扩展为:
a. Conveniently share default values throughout my codebase; and
b. Distinguish between development and production.
一种。在我的代码库中方便地共享默认值;和
b. 区分开发和生产。
It looks like this:
它看起来像这样:
<?php
/* MYMAILER
* Our own custom mailer class. Makes it easier to use default settings for all things email.
*
* Rules of thumb:
* - Keep bounce rate under 5%.
* - Keep complaint rate under 0.1%
*
* Tips:
* - To check that the SPF and DKIM settings are correct, simply send an email (e.g.: to Gmail) and check its headers.
* - Check URIBL.com and SURBL.org to check my links are not blacklisted.
*
* Documentation:
* - Best practices: http://media.amazonwebservices.com/AWS_Amazon_SES_Best_Practices.pdf -- includes iSP postmaster pages for many ISPs on last page.
* - Header fields: http://docs.aws.amazon.com/ses/latest/DeveloperGuide/header-fields.html
* ================================================================================= */
class MyMailer extends PHPMailer {
public function __construct() {
parent::__construct();
$this->CharSet = 'UTF-8';
if ( defined('APP_DEVELOPMENT') ) {
/**
* Local development. e.g.: mhsendmail + MailHog.
**/
$this->IsMail(); /** PHPMailer's default, but let's be explicit here. **/
} else {
/**
* Production, i.e.: Amazon's SES.
* The `Username` and `Password` SMTP credentials are AWS `Access key ID`/`Secret access key` created specifically for Amazon SES.
* Cf. `AWS Console ? SES ? SMTP Settings` to create them.
* Cf. `AWS Console ? IAM ??Users` to list the access keys (passwords only available when creating credentials.)
* CAREFUL! Creating new access keys for the SMTP user via IAM is technically possible but it does NOT work!
* Instead, use the `SES ? SMTP Settings` interface.
* Remember that any FROM email has to be verified (either via domain or via email inbox.)
**/
$this->IsSMTP(true);
$this->SMTPAuth = true;
$this->SMTPSecure = "tls";
$url = parse_url( getenv('SMTP_URL') );
if ( $url == FALSE ) {
$log->error( 'Failed to parse SMTP_URL' );
}
$this->Host= $url['host'];
$this->Port = $url['port'];
$this->Username = $url['user'];
$this->Password = $url['pass'];
}
/**
* Default values common to local + SES. Constant defined in config.php
* Used defensively only.
* Those values would and should be overwritten by the code.
* Apparently, the order matters (TBC.)
**/
$this->AddReplyTo( MAIL_DEFAULT_ADDREPLYTO );
$this->FromName = MAIL_DEFAULT_FROMNAME;
$this->From = MAIL_DEFAULT_FROM;
}
}
?>
I hope this helps.
我希望这有帮助。
回答by Akash Ahire
Just increase your sending mail limit quota on AWS SES
只需增加您在 AWS SES 上的发送邮件限制配额
I had the same issue with AWS ec2 and then by searching solution, I got that AWS SES send mails only if receivers is also verified if you are in amazon sandbox.
我在使用 AWS ec2 时遇到了同样的问题,然后通过搜索解决方案,我发现 AWS SES 仅在您在亚马逊沙箱中也验证了接收者的情况下才发送邮件。
If you want to send mail to another mail id( that is not verified ) then you have to increase your sending limit quota. just follow the steps shown in URL
如果您想将邮件发送到另一个邮件 ID(未验证),那么您必须增加您的发送限制配额。只需按照 URL 中显示的步骤操作
http://docs.aws.amazon.com/ses/latest/DeveloperGuide/submit-extended-access-request.html
http://docs.aws.amazon.com/ses/latest/DeveloperGuide/submit-extended-access-request.html
回答by Anil
The below entry helped me resolve the issue I was facing with non delivery of my emails. While I had all the other configurations done but could not understand on why it was not working (not a PHP developer and hence not easy to figure out).
下面的条目帮助我解决了我面临的不发送电子邮件的问题。虽然我完成了所有其他配置,但无法理解为什么它不起作用(不是 PHP 开发人员,因此不容易弄清楚)。
$mail->SMTPSecure = 'tls';