Cannot send form responses with PHPMailer using AWS EC2 SMTP SES

Hi Experts;

I can send PHP Email from the command line connected to an Amazon Web Services EC2 instance configured with an Apache 2.4.2 server, PHP 7.0, and PHPMailer 6.01 through the AWS SES SMTP interface. I am unable however to send responses to an html form using PHPMailer to an Email address from the AWS website running on the same EC2 instance and server. Here is the code used to send PHP Email from the command line without any problems:

use PHPMailer\PHPMailer\PHPMailer;
require '/home/ec2-user/vendor/autoload.php';
$mail = new PHPMailer;

$mail->setFrom('', 'Verified Email');
$mail->addAddress('', 'Recipient Name');

$mail->Username = 'Amazon SMPT credentials';
$mail->Password = 'Amazon SMPT secret Key';
$mail->Host = '';

$mail->Subject = 'Testing Amazon SES SMTP PHP Mail';
$mail->Body = '<h1>Email Test</h1>
    <p>This email was sent through the
    <a href="">Amazon SES</a> SMTP
    interface using the PHPMailer.</a><br><br></p>';

$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->AltBody = "Email Test\r\n This email was sent through the
    Amazon SES SMTP interface using the PHPMailer class.";

if(!$mail->send()) {
    echo "Email not sent. " , $mail->ErrorInfo , PHP_EOL;
} else {
echo "Email sent!" , PHP_EOL;

I need help with the PHP code for sending the form responses from visitors to my website to my Email address and an Email response back to the user's address that the information they provided has been sent to me using the AWS EC2 SMTP interface.

Thank you,
Who is Participating?
Steve BinkConnect With a Mentor Commented:
Are you receiving any error conditions when trying to send?  Have you checked the mail logs for the mail service's side of the story?
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
The way you'd normally do this is...

1) Setup a page + form where email will be send from.

2) Call your email sending code.

3) Update your page or transition to a new page, to show status of email send.

All you've posted is the code to send your email (#2). There's much code missing (#1 + #3).

You can check GitHub for many examples of this type code you can use.

If you're asking someone to code you a working example, best to open a Gig for this.

Getting email to send is easy. Ensuring high deliverability is complex. You might consider using MailGun for you actual mail delivery.
visionetvAuthor Commented:
Thank you David for your response.

I don't have any issues in sending PHP transactional Emails from websites, just Amazon Web Services (AWS), Simple EMail Service (SES) SMTP interface. My current project requires using the AWS platform.

The code I posted was to demonstrate I am able to access and use the AWS SMTP interface successfully to send Emails from a command line but not PHP form information through the AWS SMTP interface from a website. I am hoping someone has had a similar experience with AWS, as I haven't been able to find anything on the Internet so far, including GitHub. that parallels my problem.

Thanks again,
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

David FavorConnect With a Mentor Linux/LXD/WordPress/Hosting SavantCommented:
@visionetv, If you're able to send email via the command line + no PHP, this is odd. The mail functions are baked into PHP core, so it's unlikely a missing PHP module is the problem.

As Steve said, your MTA logs are the place to look.

BTW, sending email from AWS is not the same as having your email have any level of deliverability.

The only way to have any level of deliverability is to start with my comments in + likely after a few months you're deliverability will increase to an acceptable level.

Run the PHP above on the command line + from your Web server code + compare differences of your MTA logs.

Likely this will show enough detail, where you can debug your sending problem.
visionetvAuthor Commented:
Sorry if my earlier response was misleading. I can send PHP Email from the command line (PuTTY) but not from the AWS ec2 instance that hosts the website, php and PHPMailer.

I'll take a look at the mail logs and I'll let you both know.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Best to give an exact example (commands you type in) that work + don't work.

In the case of the failure, include any diagnostic messages produced.
visionetvAuthor Commented:
My comment may not have posted: The answer was $mail->SMTPAuth = true; and $mail->SMTPSecure = 'tls'; were missing from the PHPMailer response form - should have looked closer at the log.

Thank you both - I split the points
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.