0

I got the server from my client and this is the first time I using Amazon EC2 server I install Apache, MySQL and PHP inside the server and the client install the Amazon SES mail server and they send me the access-key and secret-key to me. For the email configuration, I install composer, aws-sdk-php and PHPMailer using that I include the key's what they send to me to my code. I try to send an email with my site contact-us form but I didn't receive the emails. Please attached my code below. Please check and advise me where I made a mistake in my code.

Note : I install the plugin inside my var/www/html folder.

require '../vendor/autoload.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',
    'region'  => 'us-west-2',
    'key' => 'xxxxxxxx',
    'secret' => 'xxxxxxxxx'
]);

// Replace sender@example.com with your "From" address.
// This address must be verified with Amazon SES.
$sender_email = 'no-reply@demo.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 = ['demo@gmail.com','demo123@gmail.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";
}
Paulie-C
  • 1,674
  • 1
  • 13
  • 29
Jerad
  • 213
  • 5
  • 25

1 Answers1

1

Please check the following:

1) Verify your email address with AWS SES. You have to verify that you own the email address.

2) Confirm your SMTP credentials, these are not the same as your AWS credentials. -- SMTP credentials are found on this page (after you have logged in to your AWS account): https://console.aws.amazon.com/ses/home?#smtp-settings

3) To confirm that PHP is working, please run php -v in your EC2 terminal to confirm that you get the PHP version printed on-screen.

4) When all of this is working, confirm that you have PHPMailer correctly installed (require './vendor/autoload.php';, replace the path with the one you have on your system)

5) If your AWS EC2 instance is in a region other than US West, declare and assign values to the $host and $port values.

6) Still not working? What error message are you getting from PHPMailer? Please see the following guide for error handling with PHPMailer: Error handling with PHPMailer

Paulie-C
  • 1,674
  • 1
  • 13
  • 29
  • I submitting the form with ajax but I'm getting any message from this I don't know why to help me out of these issues. – Jerad Dec 23 '19 at 15:59