Recently I built a website, Everything is working smooth but contact form is not sending email. I am doing it using a combination of jquery, php and PHPMailer php library. Please help me out. Here is the code:
php in a separate file named contact.php in a folder PHP:
<?php
require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->Mailer = "smtp";
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '<myemail>';
$mail->Password = '<secretpassword>';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$to = "<myemail>";
$from = $_POST['email'];
$first_name = $_POST['name'];
$subject = "Contact form";
$message = "Message: " . $_POST['textans'];
$mail->SetFrom($from, $first_name);
$mail->addAddress('<myemail>', 'Diode');
$mail->Subject = $subject;
$mail->Body=$message;
$mail->send();
?>
According to the digging I have done the data is being passed to the PHP but is not being sent. I am using the website on AWS EC2. I checked telnet for connection, but telnet works so I am sure the host is reachable. I have also turned off the two-factor verification and turned on less secure apps.
I tried: 1) changing port to 465. 2) turning off autotls. 3) Checked that openssl is uncommented. 4) Checked inbound and outbound rules both in windows firewall and aws security
It give a post error (500 Internal server error) on the browser debugger. Here is the PHP log:
[<dateandtime> Asia/Kolkata] PHP Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not connect to SMTP host. in <somelocation>\PHP\src\PHPMailer.php:2129
Stack trace:
#0 <somelocation>\PHP\src\PHPMailer.php(1953): PHPMailer\PHPMailer\PHPMailer->smtpConnect(Array)
#1 <somelocation>\PHP\src\PHPMailer.php(1635): PHPMailer\PHPMailer\PHPMailer->smtpSend('Date: Th...', ' Name: N...')
#2 <somelocation>\PHP\src\PHPMailer.php(1468): PHPMailer\PHPMailer\PHPMailer->postSend()
#3 <somelocation>\PHP\contact.php(37): PHPMailer\PHPMailer\PHPMailer->send()
#4 {main}
thrown in <somelocation>\PHP\src\PHPMailer.php on line 2129
Also it works on localhost on XAMPP! Thanks for your help in advance.