Search code examples
javascriptphpphpmailer

Can't POST data jquery to phpmailer


Html form:

<form id="contacts-form">
  <input name="email" type="text">
  <textarea name="contacts-form-textarea"></textarea>
  <button type="submit">Send</button>
</form>

JS:

var $form = $("#contacts-form");
$form.on("submit", submitHandler);

function submitHandler(e) {
  e.preventDefault();
  $.ajax({
    url: '/sendmail2.php',
    type: 'POST',
    data: $form.serialize()
  }).done(response => {
    if (JSON.parse(response).success) {
      console.log('success');
      $form.trigger('reset');
    } else {
      console.log('fail');
    }
  });
}

sendmail2.php (basically the official phpmailer example for gmail with minor modifications):

<?php
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;

// require '../vendor/autoload.php';
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';

//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "myemail@gmail.com";

//Password to use for SMTP authentication
$mail->Password = "mypass";

//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');

//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');

//Set who the message is to be sent to
$mail->addAddress('anotherone@gmail.com', 'My Name');

//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
// $mail->msgHTML(file_get_contents('contentss.html'), __DIR__);
$mail->msgHTML('<h1>' . $_POST["email"] . '</h1>');

//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';

//Attach an image file
// $mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors
if (!$mail->send()) {
    // echo "Mailer Error: " . $mail->ErrorInfo;
    $results = array('success' => false);
    echo json_encode($results);
} else {
    // echo "Message sent!";
    $results = array('success' => true);
    echo json_encode($results);
    //Section 2: IMAP
    //Uncomment these to save your message in the 'Sent Mail' folder.
    #if (save_mail($mail)) {
    #    echo "Message saved!";
    #}
}

//Section 2: IMAP
//IMAP commands requires the PHP IMAP Extension, found at: https://php.net/manual/en/imap.setup.php
//Function to call which uses the PHP imap_*() functions to save messages: https://php.net/manual/en/book.imap.php
//You can use imap_getmailboxes($imapStream, '/imap/ssl') to get a list of available folders or labels, this can
//be useful if you are trying to get this working on a non-Gmail IMAP server.
function save_mail($mail)
{
    //You can change 'Sent Mail' to any other folder or tag
    $path = "{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail";

    //Tell your server to open an IMAP connection using the same username and password as you used for SMTP
    $imapStream = imap_open($path, $mail->Username, $mail->Password);

    $result = imap_append($imapStream, $path, $mail->getSentMIMEMessage());
    imap_close($imapStream);

    return $result;
}

Mail sending works as expected, I receive the message on my email. But for some reason the data is not getting sent to the sendmail2.php.

When I console.log the data in the JS file it works fine, but $_POST["email"] on the backend returns null. I tried

var data = {
  email: $("#contacts-form input").val()
};

and in my JS, didn't work either.


Solution

  • Turns out the issue was caused by these .htaccess rules used to hide the .php extension in the urls:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
    RewriteRule ^ /%1 [R=301,L,NE]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+?)/?$ $1.php [QSA,NC,L]
    

    Other than that, the code from the initial post works as expected.

    ADDED:

    The rules above can be replaced by the following (with no affect on mail sending):

    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L]