0

I've set up the Amazon SES on my ec2 instance as in this link

The test file is working fine and sending the email when I use the command php teste.php on the terminal on my ec2 instance, the problem is when I try to make a post request to access this file and send the email, it's throwing an error 500 and I don't know how to fix it.

here is the error message on chrome:

enter image description here

and on Insomnia:

enter image description here

here is my test file:

<?php

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

// location of your Composer autoload.php file.
require '../../../home/ec2-user/vendor/autoload.php';

$sender = 'email@email.com';
$senderName = 'Teste';
$recipient = 'email@email.com';
$usernameSmtp = '[removed for security]';
$passwordSmtp = '[removed for security]';
$host = 'email-smtp.sa-east-1.amazonaws.com';
$port = 587;
$subject = 'Amazon SES test (SMTP interface accessed using PHP)';
$bodyText =  "Email Test\r\nThis email was sent through the
Amazon SES SMTP interface using the PHPMailer class.";

$bodyHtml = '<h1>Email Test</h1>
<p>This email was sent through the
<a href="https://aws.amazon.com/ses">Amazon SES</a> SMTP
interface using the <a href="https://github.com/PHPMailer/PHPMailer">
PHPMailer</a> class.</p>';

$mail = new PHPMailer(true);

try {
  $mail->isSMTP();
  $mail->setFrom($sender, $senderName);
  $mail->Username   = $usernameSmtp;
  $mail->Password   = $passwordSmtp;
  $mail->Host       = $host;
  $mail->Port       = $port;
  $mail->SMTPAuth   = true;
  $mail->SMTPSecure = 'tls';
  $mail->addCustomHeader('X-SES-CONFIGURATION-SET');
  $mail->addAddress($recipient);
  $mail->isHTML(true);
  $mail->Subject    = $subject;
  $mail->Body       = $bodyHtml;
  $mail->AltBody    = $bodyText;
  $mail->Send();
echo "Email sent!", PHP_EOL;
} catch (phpmailerException $e) {
  echo "An error occurred. {$e->errorMessage()}", PHP_EOL;
} catch (Exception $e) {
  echo "Email not sent. {$mail->ErrorInfo}", PHP_EOL;
}
ronate
  • 51
  • 5
  • Can you share logs? – x43 Jan 03 '21 at 18:26
  • I don't know where to find logs, the only message that comes back is 500 internal server error, and if I hoover this message on Insomnia it shows: The server has encountered a situation it doesn't know how to handle. – ronate Jan 03 '21 at 18:58

1 Answers1

1

You need to open port 587 on your instance's security group to make it work.

A similar question asked on aws developer form

EDIT

using SSL

    $mail->Host = 'ssl://email-smtp.sa-east-1.amazonaws.com';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 443;
samtoddler
  • 8,463
  • 2
  • 26
  • 21
  • you need to share the error message what you are getting , run the script in the foreground, and share the error here. – samtoddler Jan 04 '21 at 15:29
  • @ronate can you try with ssl settings as I've updated. There are couple of other options [using-phpmailer-and-amazon-ses](https://stackoverflow.com/questions/23497876/using-phpmailer-and-amazon-ses) – samtoddler Jan 04 '21 at 16:10