0

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.

Diode
  • 68
  • 10
  • What have you tried to debug the problem? Also, please remove the irrelevant parts and tags from your question: neither Javascript nor jQuery are involved if your PHP code throws such an error – Nico Haase Apr 15 '21 at 06:46
  • There are many duplicates here with accepted and highly upvoted answers (ie they work for many ppl). Since you didn't mention trying or even seeing those, they are the first thing to check, eg https://stackoverflow.com/questions/16048347/unable-to-send-email-using-gmail-smtp-server-through-phpmailer-getting-error-s, https://stackoverflow.com/questions/3477766/phpmailer-smtp-error-could-not-connect-to-smtp-host?rq=1, ... Minor observations - `$mail->isSMTP();` appears 2x, with different capitalisation. What is `SMTP::DEBUG_SERVER`, or rather, have you already set it for debugging? – Don't Panic Apr 15 '21 at 07:24
  • 1
    AWS mostly blocks outbound SMTP, so that's the most likely explanation. You're getting a 500 error because you're asking PHPMailer to throw exceptions, but then not catching them! I suggest you base your code on the gmail example provided with PHPMailer, which avoids issues like that. I suspect you have not shown us your entire log output, as it's missing important info. – Synchro Apr 15 '21 at 07:57

1 Answers1

2

First of all, I tried running your code which gave me SMTP Class not found error, so I added another use statement use PHPMailer\PHPMailer\SMTP; to avoid future errors.

Then, I got the same error and solved it by generating an App Password, which was required for 2-Step Verified accounts.

I didn't really change your code, but I ran it in localhost.

If nothing works, you can set the debug mode $mail->SMTPDebug = 3; in order to get more details, and also I recommend checking out the SSL/TLS protocols, which I saw it from this thread: PHPMailer: SMTP Error: Could not connect to SMTP host

I hope that I didn't waste your time...

Roni
  • 76
  • 4
  • Thanks a lot for the prompt response! I really appreciate it. I tried it but didn't work sadly. You can checkout the updated code. It still showed the same error. As soon as I added '$mail->SMTPOptions = array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ) );' it worked thanks! – Diode Apr 15 '21 at 07:51
  • 1
    I'm really sorry to not clearly explain my solution. I guess that the problem was the configuration of SSL. Because of I ran the code in localhost, I might not get any ssl errors, but as you setup your code in external server, you needed to configure your encryption protocol. Anyway, I'm happy that it worked! And also, don't forget to upvote the solution in the other thread. :) – Roni Apr 15 '21 at 08:06