AWS SES with PHPMailer using SMTP, SMTP Error: Could not authenticate?
AWS SES with PHPMailer using SMTP, SMTP Error: Could not authenticate?
我收到以下错误:身份验证凭据无效。
我已经仔细检查了用户名(访问密钥 ID)和密码(秘密令牌)一百万次。我仔细检查了发送到服务器的 base64,它是正确的。用户具有正确的访问权限。允许来自 EC2 服务器的所有出站流量。 SELinux 被禁用。我已经转义了特殊字符,尝试了不同的凭据。尝试使用具有更多访问权限的用户。
我将 PHPMailer 与 AWS SES 结合使用。
代码如下:
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->SMTPDebug = 4;
$mail->setFrom('braydenrhancock@gmail.com', 'Sender Name');
$mail->addAddress('braydenrhancock@gmail.com', 'Recipient Name');
$mail->Username = 'AKIAINH6PZ2UQKDK2BTA';
$mail->Password = 'GvMMa7R3fAXZGacl3gyfA86J0RxJLO7FQte9vrof';
$mail->Host = 'email-smtp.us-east-1.amazonaws.com';
$mail->Subject = 'Amazon SES test (SMTP interface accessed using PHP)';
$mail->Body = '<h1>Email Test</h1>';
$mail->Port = 587;
$mail->isHTML(true);
$mail->AltBody = "Email Test\r\nThis email was sent through the
Amazon SES SMTP interface using the PHPMailer class.";
if(!$mail->send()) {
echo "Email not sent. " , $mail->ErrorInfo , PHP_EOL;
} else {
echo "Email sent!" , PHP_EOL;
}
这是完整的输出:
2017-11-06 01:09:49 Connection: opening to email-smtp.us-east-1.amazonaws.com:587, timeout=300, options=array ()
2017-11-06 01:09:49 Connection: opened
2017-11-06 01:09:49 SMTP -> get_lines(): $data is ""
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "220 email-smtp.amazonaws.com ESMTP SimpleEmailService-2367521455 ZxpFMwcwQB6LGlJ2noyc"
2017-11-06 01:09:49 SERVER -> CLIENT: 220 email-smtp.amazonaws.com ESMTP SimpleEmailService-2367521455 ZxpFMwcwQB6LGlJ2noyc
2017-11-06 01:09:49 CLIENT -> SERVER: EHLO 34.200.216.237
2017-11-06 01:09:49 SMTP -> get_lines(): $data is ""
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250-email-smtp.amazonaws.com"
2017-11-06 01:09:49 SMTP -> get_lines(): $data is "250-email-smtp.amazonaws.com"
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250-8BITMIME"
2017-11-06 01:09:49 SMTP -> get_lines(): $data is "250-email-smtp.amazonaws.com250-8BITMIME"
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250-SIZE 10485760"
2017-11-06 01:09:49 SMTP -> get_lines(): $data is "250-email-smtp.amazonaws.com250-8BITMIME250-SIZE 10485760"
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250-STARTTLS"
2017-11-06 01:09:49 SMTP -> get_lines(): $data is "250-email-smtp.amazonaws.com250-8BITMIME250-SIZE 10485760250-STARTTLS"
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250-AUTH PLAIN LOGIN"
2017-11-06 01:09:49 SMTP -> get_lines(): $data is "250-email-smtp.amazonaws.com250-8BITMIME250-SIZE 10485760250-STARTTLS250-AUTH PLAIN LOGIN"
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250 Ok"
2017-11-06 01:09:49 SERVER -> CLIENT: 250-email-smtp.amazonaws.com250-8BITMIME250-SIZE 10485760250-STARTTLS250-AUTH PLAIN LOGIN250 Ok
2017-11-06 01:09:49 CLIENT -> SERVER: STARTTLS
2017-11-06 01:09:49 SMTP -> get_lines(): $data is ""
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "220 Ready to start TLS"
2017-11-06 01:09:49 SERVER -> CLIENT: 220 Ready to start TLS
2017-11-06 01:09:49 CLIENT -> SERVER: EHLO 34.200.216.237
2017-11-06 01:09:49 SMTP -> get_lines(): $data is ""
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250-email-smtp.amazonaws.com"
2017-11-06 01:09:49 SMTP -> get_lines(): $data is "250-email-smtp.amazonaws.com"
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250-8BITMIME"
2017-11-06 01:09:49 SMTP -> get_lines(): $data is "250-email-smtp.amazonaws.com250-8BITMIME"
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250-SIZE 10485760"
2017-11-06 01:09:49 SMTP -> get_lines(): $data is "250-email-smtp.amazonaws.com250-8BITMIME250-SIZE 10485760"
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250-STARTTLS"
2017-11-06 01:09:49 SMTP -> get_lines(): $data is "250-email-smtp.amazonaws.com250-8BITMIME250-SIZE 10485760250-STARTTLS"
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250-AUTH PLAIN LOGIN"
2017-11-06 01:09:49 SMTP -> get_lines(): $data is "250-email-smtp.amazonaws.com250-8BITMIME250-SIZE 10485760250-STARTTLS250-AUTH PLAIN LOGIN"
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250 Ok"
2017-11-06 01:09:49 SERVER -> CLIENT: 250-email-smtp.amazonaws.com250-8BITMIME250-SIZE 10485760250-STARTTLS250-AUTH PLAIN LOGIN250 Ok
2017-11-06 01:09:49 Auth method requested: UNKNOWN
2017-11-06 01:09:49 Auth methods available on the server: PLAIN,LOGIN
2017-11-06 01:09:49 Auth method selected: LOGIN
2017-11-06 01:09:49 CLIENT -> SERVER: AUTH LOGIN
2017-11-06 01:09:49 SMTP -> get_lines(): $data is ""
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "334 VXNlcm5hbWU6"
2017-11-06 01:09:49 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2017-11-06 01:09:49 CLIENT -> SERVER: QUtJQUlOSDZQWjJVUUtESzJCVEE=
2017-11-06 01:09:49 SMTP -> get_lines(): $data is ""
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "334 UGFzc3dvcmQ6"
2017-11-06 01:09:49 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2017-11-06 01:09:49 CLIENT -> SERVER: R3ZNTWE3UjNmQVhaR2FjbDNneWZBODZKMFJ4SkxPN0ZRdGU5dnJvZg==
2017-11-06 01:09:50 SMTP -> get_lines(): $data is ""
2017-11-06 01:09:50 SMTP -> get_lines(): $str is "535 Authentication Credentials Invalid"
2017-11-06 01:09:50 SERVER -> CLIENT: 535 Authentication Credentials Invalid
2017-11-06 01:09:50 SMTP ERROR: Password command failed: 535 Authentication Credentials Invalid
SMTP Error: Could not authenticate.
2017-11-06 01:09:50 CLIENT -> SERVER: QUIT
2017-11-06 01:09:50 SMTP -> get_lines(): $data is ""
2017-11-06 01:09:50 SMTP -> get_lines(): $str is "221 Bye"
2017-11-06 01:09:50 SERVER -> CLIENT: 221 Bye
2017-11-06 01:09:50 Connection: closed
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Email not sent. SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
这些是您的 AWS 凭证还是 SMTP 凭证?
如果这些是 AWS 凭据,则生成 SMTP 凭据,然后尝试。
See it here
或者你也可以试试这个
Similar issue
SES 不支持使用 IAM 用户访问密钥发送电子邮件。 SES 使用 Amazon SES SMTP 凭据。
Sign in to the AWS Management Console and open the Amazon SES console at https://console.aws.amazon.com/ses/.
In the navigation pane, choose SMTP Settings.
In the content pane, choose Create My SMTP Credentials.
In the Create User for SMTP dialog box, you will see that an SMTP user name has been filled in for you. You can accept this suggested user name or enter a different one. To proceed, choose Create.
Choose Show User SMTP Credentials. Your SMTP credentials will be displayed on the screen; copy them and store them in a safe place. You can also choose Download Credentials to download a file that contains your credentials.
这是一份 link 文档,其中解释了不同类型的凭据以及它们如何与 SES 一起使用。
Using Credentials With Amazon SES
这里是 link 获取凭据的方法:
假设您将 phpmailer 5.2(或左右)解压缩到与我相同的位置,此示例函数将与安装在 Windows 2012 的 PHP 7.1 的 Amazon SES 一起使用。我没有理会作曲家。请注意,在下面的函数中,我注释掉了该行
$邮件->SMTPDebug = 2;
...但是您希望在测试时取消对这一行的注释,以便您可以看到发生了什么。您需要添加的值在括号内。我的 SES 主机是 "email-smtp.us-east-1.amazonaws.com" ,只要确保无论你使用哪个,除了主机 fqdn 之外别无其他(不要用 tls://....[= 引导它) 12=]
将以下内容另存为Send_Amazon_Mail.php
<?php
function Send_Mail($to,$subject,$body)
{
require 'C:\Program Files\PHP\v7.1\phpmailer\PHPMailerAutoload.php';
$from = "[SES validated email address here]";
$mail = new PHPMailer();
// $mail->SMTPDebug = 2;
$mail->IsSMTP(true); // SMTP
$mail->SMTPAuth = true; // SMTP authentication
$mail->SMTPSecure = 'ssl';
$mail->Mailer = "smtp";
$mail->Host= "[your SES host]";
$mail->Port = 465; // SMTP Port
$mail->Username = "[SES SMTP USERNAME]"; // SMTP Username
$mail->Password = "[SES SMTP PASSWORD]"; // SMTP Password
$mail->SetFrom($from, 'Test Do Not Reply');
$mail->AddReplyTo($from,'[SES validated email address here]');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, $to);
if(!$mail->Send())
return false;
else
return true;
}
?>
你可以用测试程序调用这个函数
<?php
require 'Send_Amazon_Mail.php';
$to = "[someemailaddresshere]";
$subject = "Test Mail Subject";
$body = "Hi<br/>Test Mail<br/>Amazon SES"; // HTML tags
Send_Mail($to,$subject,$body);
?>
我在这里添加了我的答案,因为上面没有帮助我。
此问题与 WHM/cPanel 中的安全功能有关。仅当您的服务器上有 WHM 时才阅读更多内容。
选项名为:
Restrict outgoing SMTP to root, exim, and mailman (FKA SMTP Tweak)
如果禁用它,问题就解决了。请记住,在某些版本的 cPanel 上缺少此选项,它会出现在 WHM 中,因为它会影响所有帐户。对我来说,禁用它是可以的,因为我拥有服务器并且那里没有未知 domains/users。
我遇到了同样的问题,直到我发现 ConfigServer 安全和防火墙 (csf) 出于某种原因阻止了它。
解决方案是在 WHM 调整设置中关闭“将传出 SMTP 限制为 root、exim 和邮递员(FKA SMTP 调整)”。
此选项与 CSF 不兼容。
我收到以下错误:身份验证凭据无效。
我已经仔细检查了用户名(访问密钥 ID)和密码(秘密令牌)一百万次。我仔细检查了发送到服务器的 base64,它是正确的。用户具有正确的访问权限。允许来自 EC2 服务器的所有出站流量。 SELinux 被禁用。我已经转义了特殊字符,尝试了不同的凭据。尝试使用具有更多访问权限的用户。
我将 PHPMailer 与 AWS SES 结合使用。
代码如下:
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->SMTPDebug = 4;
$mail->setFrom('braydenrhancock@gmail.com', 'Sender Name');
$mail->addAddress('braydenrhancock@gmail.com', 'Recipient Name');
$mail->Username = 'AKIAINH6PZ2UQKDK2BTA';
$mail->Password = 'GvMMa7R3fAXZGacl3gyfA86J0RxJLO7FQte9vrof';
$mail->Host = 'email-smtp.us-east-1.amazonaws.com';
$mail->Subject = 'Amazon SES test (SMTP interface accessed using PHP)';
$mail->Body = '<h1>Email Test</h1>';
$mail->Port = 587;
$mail->isHTML(true);
$mail->AltBody = "Email Test\r\nThis email was sent through the
Amazon SES SMTP interface using the PHPMailer class.";
if(!$mail->send()) {
echo "Email not sent. " , $mail->ErrorInfo , PHP_EOL;
} else {
echo "Email sent!" , PHP_EOL;
}
这是完整的输出:
2017-11-06 01:09:49 Connection: opening to email-smtp.us-east-1.amazonaws.com:587, timeout=300, options=array ()
2017-11-06 01:09:49 Connection: opened
2017-11-06 01:09:49 SMTP -> get_lines(): $data is ""
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "220 email-smtp.amazonaws.com ESMTP SimpleEmailService-2367521455 ZxpFMwcwQB6LGlJ2noyc"
2017-11-06 01:09:49 SERVER -> CLIENT: 220 email-smtp.amazonaws.com ESMTP SimpleEmailService-2367521455 ZxpFMwcwQB6LGlJ2noyc
2017-11-06 01:09:49 CLIENT -> SERVER: EHLO 34.200.216.237
2017-11-06 01:09:49 SMTP -> get_lines(): $data is ""
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250-email-smtp.amazonaws.com"
2017-11-06 01:09:49 SMTP -> get_lines(): $data is "250-email-smtp.amazonaws.com"
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250-8BITMIME"
2017-11-06 01:09:49 SMTP -> get_lines(): $data is "250-email-smtp.amazonaws.com250-8BITMIME"
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250-SIZE 10485760"
2017-11-06 01:09:49 SMTP -> get_lines(): $data is "250-email-smtp.amazonaws.com250-8BITMIME250-SIZE 10485760"
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250-STARTTLS"
2017-11-06 01:09:49 SMTP -> get_lines(): $data is "250-email-smtp.amazonaws.com250-8BITMIME250-SIZE 10485760250-STARTTLS"
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250-AUTH PLAIN LOGIN"
2017-11-06 01:09:49 SMTP -> get_lines(): $data is "250-email-smtp.amazonaws.com250-8BITMIME250-SIZE 10485760250-STARTTLS250-AUTH PLAIN LOGIN"
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250 Ok"
2017-11-06 01:09:49 SERVER -> CLIENT: 250-email-smtp.amazonaws.com250-8BITMIME250-SIZE 10485760250-STARTTLS250-AUTH PLAIN LOGIN250 Ok
2017-11-06 01:09:49 CLIENT -> SERVER: STARTTLS
2017-11-06 01:09:49 SMTP -> get_lines(): $data is ""
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "220 Ready to start TLS"
2017-11-06 01:09:49 SERVER -> CLIENT: 220 Ready to start TLS
2017-11-06 01:09:49 CLIENT -> SERVER: EHLO 34.200.216.237
2017-11-06 01:09:49 SMTP -> get_lines(): $data is ""
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250-email-smtp.amazonaws.com"
2017-11-06 01:09:49 SMTP -> get_lines(): $data is "250-email-smtp.amazonaws.com"
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250-8BITMIME"
2017-11-06 01:09:49 SMTP -> get_lines(): $data is "250-email-smtp.amazonaws.com250-8BITMIME"
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250-SIZE 10485760"
2017-11-06 01:09:49 SMTP -> get_lines(): $data is "250-email-smtp.amazonaws.com250-8BITMIME250-SIZE 10485760"
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250-STARTTLS"
2017-11-06 01:09:49 SMTP -> get_lines(): $data is "250-email-smtp.amazonaws.com250-8BITMIME250-SIZE 10485760250-STARTTLS"
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250-AUTH PLAIN LOGIN"
2017-11-06 01:09:49 SMTP -> get_lines(): $data is "250-email-smtp.amazonaws.com250-8BITMIME250-SIZE 10485760250-STARTTLS250-AUTH PLAIN LOGIN"
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "250 Ok"
2017-11-06 01:09:49 SERVER -> CLIENT: 250-email-smtp.amazonaws.com250-8BITMIME250-SIZE 10485760250-STARTTLS250-AUTH PLAIN LOGIN250 Ok
2017-11-06 01:09:49 Auth method requested: UNKNOWN
2017-11-06 01:09:49 Auth methods available on the server: PLAIN,LOGIN
2017-11-06 01:09:49 Auth method selected: LOGIN
2017-11-06 01:09:49 CLIENT -> SERVER: AUTH LOGIN
2017-11-06 01:09:49 SMTP -> get_lines(): $data is ""
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "334 VXNlcm5hbWU6"
2017-11-06 01:09:49 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2017-11-06 01:09:49 CLIENT -> SERVER: QUtJQUlOSDZQWjJVUUtESzJCVEE=
2017-11-06 01:09:49 SMTP -> get_lines(): $data is ""
2017-11-06 01:09:49 SMTP -> get_lines(): $str is "334 UGFzc3dvcmQ6"
2017-11-06 01:09:49 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2017-11-06 01:09:49 CLIENT -> SERVER: R3ZNTWE3UjNmQVhaR2FjbDNneWZBODZKMFJ4SkxPN0ZRdGU5dnJvZg==
2017-11-06 01:09:50 SMTP -> get_lines(): $data is ""
2017-11-06 01:09:50 SMTP -> get_lines(): $str is "535 Authentication Credentials Invalid"
2017-11-06 01:09:50 SERVER -> CLIENT: 535 Authentication Credentials Invalid
2017-11-06 01:09:50 SMTP ERROR: Password command failed: 535 Authentication Credentials Invalid
SMTP Error: Could not authenticate.
2017-11-06 01:09:50 CLIENT -> SERVER: QUIT
2017-11-06 01:09:50 SMTP -> get_lines(): $data is ""
2017-11-06 01:09:50 SMTP -> get_lines(): $str is "221 Bye"
2017-11-06 01:09:50 SERVER -> CLIENT: 221 Bye
2017-11-06 01:09:50 Connection: closed
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Email not sent. SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
这些是您的 AWS 凭证还是 SMTP 凭证? 如果这些是 AWS 凭据,则生成 SMTP 凭据,然后尝试。 See it here
或者你也可以试试这个 Similar issue
SES 不支持使用 IAM 用户访问密钥发送电子邮件。 SES 使用 Amazon SES SMTP 凭据。
Sign in to the AWS Management Console and open the Amazon SES console at https://console.aws.amazon.com/ses/.
In the navigation pane, choose SMTP Settings.
In the content pane, choose Create My SMTP Credentials.
In the Create User for SMTP dialog box, you will see that an SMTP user name has been filled in for you. You can accept this suggested user name or enter a different one. To proceed, choose Create.
Choose Show User SMTP Credentials. Your SMTP credentials will be displayed on the screen; copy them and store them in a safe place. You can also choose Download Credentials to download a file that contains your credentials.
这是一份 link 文档,其中解释了不同类型的凭据以及它们如何与 SES 一起使用。
Using Credentials With Amazon SES
这里是 link 获取凭据的方法:
假设您将 phpmailer 5.2(或左右)解压缩到与我相同的位置,此示例函数将与安装在 Windows 2012 的 PHP 7.1 的 Amazon SES 一起使用。我没有理会作曲家。请注意,在下面的函数中,我注释掉了该行 $邮件->SMTPDebug = 2; ...但是您希望在测试时取消对这一行的注释,以便您可以看到发生了什么。您需要添加的值在括号内。我的 SES 主机是 "email-smtp.us-east-1.amazonaws.com" ,只要确保无论你使用哪个,除了主机 fqdn 之外别无其他(不要用 tls://....[= 引导它) 12=]
将以下内容另存为Send_Amazon_Mail.php
<?php
function Send_Mail($to,$subject,$body)
{
require 'C:\Program Files\PHP\v7.1\phpmailer\PHPMailerAutoload.php';
$from = "[SES validated email address here]";
$mail = new PHPMailer();
// $mail->SMTPDebug = 2;
$mail->IsSMTP(true); // SMTP
$mail->SMTPAuth = true; // SMTP authentication
$mail->SMTPSecure = 'ssl';
$mail->Mailer = "smtp";
$mail->Host= "[your SES host]";
$mail->Port = 465; // SMTP Port
$mail->Username = "[SES SMTP USERNAME]"; // SMTP Username
$mail->Password = "[SES SMTP PASSWORD]"; // SMTP Password
$mail->SetFrom($from, 'Test Do Not Reply');
$mail->AddReplyTo($from,'[SES validated email address here]');
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $to;
$mail->AddAddress($address, $to);
if(!$mail->Send())
return false;
else
return true;
}
?>
你可以用测试程序调用这个函数
<?php
require 'Send_Amazon_Mail.php';
$to = "[someemailaddresshere]";
$subject = "Test Mail Subject";
$body = "Hi<br/>Test Mail<br/>Amazon SES"; // HTML tags
Send_Mail($to,$subject,$body);
?>
我在这里添加了我的答案,因为上面没有帮助我。 此问题与 WHM/cPanel 中的安全功能有关。仅当您的服务器上有 WHM 时才阅读更多内容。
选项名为:
Restrict outgoing SMTP to root, exim, and mailman (FKA SMTP Tweak)
如果禁用它,问题就解决了。请记住,在某些版本的 cPanel 上缺少此选项,它会出现在 WHM 中,因为它会影响所有帐户。对我来说,禁用它是可以的,因为我拥有服务器并且那里没有未知 domains/users。
我遇到了同样的问题,直到我发现 ConfigServer 安全和防火墙 (csf) 出于某种原因阻止了它。
解决方案是在 WHM 调整设置中关闭“将传出 SMTP 限制为 root、exim 和邮递员(FKA SMTP 调整)”。 此选项与 CSF 不兼容。