简体   繁体   中英

AWS SES SMTP email works. AWS SES API does not send email

I am using AWS SES on a website with shared hosting platform (not on AWS)

When I send emails using the SMTP it works perfectly. When I attempt to call and use the API, no email is sent, no error message is received.

There are two reasons I want to use the API instead of SMTP 1) According to Amazon, the API is faster. 2) One of my transactional emails is a reminder to clients to log in, and/or pay their account. SMTP cannot send email to more than 50 (if I am not mistaken). For that reason, I need to call the SendRawEmail.

Please help me with my code to send the email and to implement SendRawEmail

Here is my code: 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";
}

?>

Check your error log. I had the same issue:

require 'AWS/aws-autoloader.php';

For other SDK uses (such as API calls) you may need to create a .aws folder to include credentials

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM