PHPMailer 和 SES 的电子邮件身份验证问题

问题描述 投票:0回答:1

我正在尝试使用 SES 从 EC2 容器中的 PHP 代码发送邮件。我想添加基于角色的权限来发送邮件(我不想使用用户名和密码)。

对于 IAM 用户,我已附加以下策略:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ses:*"
            ],
            "Resource": "*"
        }
    ]
}

我使用以下代码发送测试电子邮件:

require 'vendor/autoload.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->isSMTP();
    $mail->SMTPDebug  = 3;
    $mail->Host       = $strSesHostName;
    $mail->SMTPAuth   = false;
    $mail->SMTPSecure = 'ssl';
    $mail->SMTPAutoTLS = false;
    $mail->Port       = 465;

    //Recipients
    $mail->setFrom($strFromMail, 'Prifulnath');
    $mail->addAddress($strToMail);

    //Content
    $mail->isHTML(true);
    $mail->Subject = 'SES Test Email';
    $mail->Body    = 'Test email body';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

但是当运行此代码时,我收到 SMTP 错误,如下所示:

2024-08-28 11:17:48 Connection: opening to ssl://email-smtp.us-east-1.amazonaws.com:465, timeout=300, options=array()
2024-08-28 11:17:48 Connection: opened
2024-08-28 11:17:48 SERVER -> CLIENT: 220 email-smtp.amazonaws.com ESMTP SimpleEmailService-d-UF5383B18 BKsi60z05CMePrzvSC8r
2024-08-28 11:17:48 CLIENT -> SERVER: EHLO test.mydomain.org
2024-08-28 11:17:48 SERVER -> CLIENT: 250-email-smtp.amazonaws.com250-8BITMIME250-AUTH PLAIN LOGIN250 Ok
2024-08-28 11:17:48 CLIENT -> SERVER: MAIL FROM:<[email protected]>
2024-08-28 11:17:48 SERVER -> CLIENT: 530 Authentication required
2024-08-28 11:17:48 SMTP ERROR: MAIL FROM command failed: 530 Authentication required
The following From address failed: [email protected] : MAIL FROM command failed,Authentication required,530,SMTP server error: MAIL FROM command failed Detail: Authentication required SMTP code: 530
Message could not be sent. Mailer Error: The following From address failed: [email protected] : MAIL FROM command failed,Authentication required ,530,SMTP server error: MAIL FROM command failed Detail: Authentication required SMTP code: 530SMTP server error: MAIL FROM command failed Detail: Authentication required SMTP code: 5302024-08-28 11:17:48 CLIENT -> SERVER: QUIT
2024-08-28 11:17:48 SERVER -> CLIENT: 221 Bye
2024-08-28 11:17:48 Connection: closed

有人可以帮我解决这个问题吗?

php email smtp phpmailer amazon-ses
1个回答
0
投票

您遇到的问题与您尝试使用 PHPMailer 通过 AWS SES 进行身份验证的方式有关。即使您已将 IAM 策略附加到 EC2 实例,PHPMailer 仍会尝试使用标准 SMTP 身份验证,该身份验证需要用户名和密码。

使用适用于 PHP 的 AWS 开发工具包而不是 PHPMailer 发送电子邮件:由于您使用的是 AWS 服务,因此最好使用适用于 PHP 的 AWS 开发工具包通过 SES 发送电子邮件。此方法允许您利用 IAM 角色,而无需提供 SMTP 凭证。

require 'vendor/autoload.php';

use Aws\Ses\SesClient;
use Aws\Exception\AwsException;

// Create an SES client
$SesClient = new SesClient([
    'version' => 'latest',
    'region'  => 'us-east-1', // Change to your region
]);

$sender_email = '[email protected]';
$recipient_emails = ['[email protected]'];

$subject = 'SES Test Email';
$body_text = 'Test email body';
$body_html = '<p>Test email body</p>';

try {
    $result = $SesClient->sendEmail([
        'Source' => $sender_email,
        'Destination' => [
            'ToAddresses' => $recipient_emails,
        ],
        'Message' => [
            'Subject' => [
                'Data' => $subject,
                'Charset' => 'UTF-8',
            ],
            'Body' => [
                'Text' => [
                    'Data' => $body_text,
                    'Charset' => 'UTF-8',
                ],
                'Html' => [
                    'Data' => $body_html,
                    'Charset' => 'UTF-8',
                ],
            ],
        ],
    ]);

    echo 'Email sent! Message ID: '.$result->get('MessageId')."\n";

} catch (AwsException $e) {
    echo $e->getMessage();
    echo "\n";
}
© www.soinside.com 2019 - 2024. All rights reserved.