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:

<?php
use PHPMailer\PHPMailer\PHPMailer;
require '/home/ec2-user/vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();

$mail->setFrom('address@example.com', 'Verified Email');
$mail->addAddress('Recipient@receiver.com', 'Recipient Name');

$mail->Username = 'Amazon SMPT credentials';
$mail->Password = 'Amazon SMPT secret Key';
$mail->Host = 'email-smtp.us-west-2.amazonaws.com';

$mail->Subject = 'Testing Amazon SES SMTP PHP Mail';
$mail->Body = '<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 PHPMailer.</a><br><br></p>';

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

$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 {
$mail->Send();  
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,
Visionet
visionetvAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
0
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,
Visionet
0
Steve BinkCommented:
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?
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Managing Security & Risk at the Speed of Business

Gartner Research VP, Neil McDonald & AlgoSec CTO, Prof. Avishai Wool, discuss the business-driven approach to automated security policy management, its benefits and how to align security policy management with business processes to address today's security challenges.

David FavorLinux/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 https://www.experts-exchange.com/questions/29036679/AWS-EC2-mail-server.html + 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.
0
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.

Thanks
0
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.
0
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.