AWS SES SMTP 邮件可以正常发送,但是 AWS SES API 无法发送邮件。

3

我在一个共享托管平台的网站上使用AWS SES。当我使用SMTP发送电子邮件时,它可以完美地工作。但是,如果我尝试调用并使用API,则无法发送电子邮件,也收不到任何错误消息。

我想使用API而不是SMTP的原因有两个: 1)根据亚马逊的说法,API更快。 2)我的某些业务邮件是提醒客户登录和/或支付其账户的邮件。SMTP无法向超过50个人发送电子邮件(如果我没有记错的话)。出于这个原因,我需要调用SendRawEmail。

请帮助我编写发送电子邮件的代码并实现SendRawEmail。

以下是我的代码:

require('PHPMailer/PHPMailerAutoload.php');

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


// Create an SesClient. Change the value of the region parameter if you're 
// using an AWS Region other than US West (Oregon). Change the value of the
// profile parameter if you want to use a profile in your credentials file
// other than the default.
$SesClient = new SesClient([
'profile' => 'default',
'version' => '2010-12-01',
'region'  => 'eu-central-1'
]);

// Replace sender@example.com with your "From" address.
// This address must be verified with Amazon SES.
$sender_email = 'info@mydomain.com';

// Replace these sample addresses with the addresses of your recipients. If
// your account is still in the sandbox, these addresses must be verified.
$recipient_emails = ['info@example.com'];

// Specify a configuration set. If you do not want to use a configuration
// set, comment the following variable, and the
// 'ConfigurationSetName' => $configuration_set argument below.
//$configuration_set = 'ConfigSet';

$subject = 'Amazon SES test (AWS SDK for PHP)';
$plaintext_body = 'This email was sent with Amazon SES using the AWS SDK for PHP.' ;
$html_body =  '<h1>AWS Amazon Simple Email Service Test Email</h1>'.
          '<p>This email was sent with <a href="https://aws.amazon.com/ses/">'.
          'Amazon SES</a> using the <a href="https://aws.amazon.com/sdk-for-php/">'.
          'AWS SDK for PHP</a>.</p>';
$char_set = 'UTF-8';

try {
$result = $SesClient->sendEmail([
    'Destination' => [
        'ToAddresses' => $recipient_emails,
    ],
    'ReplyToAddresses' => [$sender_email],
    'Source' => $sender_email,
    'Message' => [
      'Body' => [
          'Html' => [
              'Charset' => $char_set,
              'Data' => $html_body,
          ],
          'Text' => [
              'Charset' => $char_set,
              'Data' => $plaintext_body,
          ],
      ],
      'Subject' => [
          'Charset' => $char_set,
          'Data' => $subject,
      ],
    ],
    // If you aren't using a configuration set, comment or delete the
    // following line
    'ConfigurationSetName' => $configuration_set,
]);
$messageId = $result['MessageId'];
echo("Email sent! Message ID: $messageId"."\n");
} catch (AwsException $e) {
// output error message if fails
echo $e->getMessage();
echo("The email was not sent. Error message: ".$e->getAwsErrorMessage()."\n");
echo "\n";
}

?>

“没有发送电子邮件,也没有收到错误消息。”但是 echo("电子邮件已发送!消息ID:$messageId"."\n"); 记录了什么? - Michael - sqlbot
没有任何记录。空白屏幕。 - CharlieT
那么,就是您需要解决的实际问题。我并不是一个PHP专家,但根据您的代码,除非可能触发了整体PHP执行超时以在到达该点之前停止执行并掩盖实际错误,否则似乎不可能什么都没有显示。也许您的服务器上有错误日志条目? - Michael - sqlbot
您确定正在使用正确的凭据吗?用于SMTP和SES API的凭据是不同的。 - Ajris
1个回答

1

请检查您的错误日志。我遇到了同样的问题:

require 'AWS/aws-autoloader.php';

对于其他SDK用途(例如API调用),您可能需要创建一个 .aws 文件夹来包含 credentials


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接