9951 explained code solutions for 126 technologies


php-awsHow do I set up an AWS PHP mailer?


  1. First, install the AWS SDK for PHP using Composer by running the following command:
composer require aws/aws-sdk-php
  1. Next, create an SES client object and pass your AWS credentials to it:
$sesClient = new \Aws\Ses\SesClient([
    'version'     => 'latest',
    'region'      => 'us-east-1',
    'credentials' => [
        'key'    => 'your_aws_key',
        'secret' => 'your_aws_secret',
    ],
]);
  1. Then, create the message body with the desired parameters:
$messageBody = [
    'Source' => '[email protected]',
    'Destination' => [
        'ToAddresses' => [
            '[email protected]',
        ],
    ],
    'Message' => [
        'Subject' => [
            'Data' => 'Test Email',
            'Charset' => 'utf-8',
        ],
        'Body' => [
            'Text' => [
                'Data' => 'This is a test email sent from AWS SES.',
                'Charset' => 'utf-8',
            ],
        ],
    ],
];
  1. Finally, send the email using the SES client's sendEmail method:
$result = $sesClient->sendEmail($messageBody);

This will return an object with the MessageId of the sent email.

  1. To verify that your email has been sent, you can use the getSendQuota method to view your current sending limits and the getSendStatistics method to view your sending activity:
$quota = $sesClient->getSendQuota();
$stats = $sesClient->getSendStatistics();
  1. To complete the setup, you must also verify your email address and set up the necessary DNS records in order for SES to send emails from your domain.

  2. You can find more information and examples on using the AWS SDK for PHP to send emails with SES in the official documentation.

Edit this code on GitHub