0

I got actually a problem to send a mail with my VPS server. My server have PHP and sendmail installed.

I try to send an email from an HTML form which is checked in JS then sent in HTML, the request launches well but nothing is sent.

My JS code :

submitMail() {
      const emailValue = document.getElementById('mail').value;
      const subjectValue = document.getElementById('subject').value;
      const messageValue = document.getElementById('text').value;
      const xhr = new XMLHttpRequest();
      xhr.open('POST', 'https://ag-dev.fr/mailform.php', true);
      xhr.setRequestHeader('Content-Type', 'application/json');
      xhr.onreadystatechange = () => {
        if (xhr.readyState === 4 && xhr.status === 200) {
          this.sendComplet();
        }
      };
      xhr.send(JSON.stringify({
        email: emailValue,
        subject: subjectValue,
        message: messageValue,
      }));
    },

My PHP code :

<?php
  if (!empty($_POST['message']))
  {
      $to = 'contact@ag-dev.fr';
      $from = $_POST['email'];
      $subject = $_POST['subject'];
      $message = $_POST['message'] . "\n\n\n\n Ceci est un message envoyé depuis le formulaire de contact. Pour répondre à ce mail, envoyez un mail à l'adresse suivante : " . $_POST['email'];
      $headers  = 'MIME-Version: 1.0\r\n';
      $headers .= 'Content-type: text/html; charset=iso-8859-1\r\n';
      $headers .= 'To: Guyomar Alexis <contact@ag-dev.fr>\r\n';
      $headers .= 'From: ' . ' <' . $from . '>\r\n';
      mail($to,$subject,$message,$headers);
  }
?>

I run a test on my command line server with sendmail and I receive the mail with no problem. I think my code or my configuration server got a problem.

If anyone has any idea where this may have come from ...

ag-dev
  • 213
  • 2
  • 12
  • Here's a good general guide to debugging email problems in PHP: https://stackoverflow.com/a/24644450/5947043 – ADyson Jun 05 '20 at 08:12
  • The only things I notice here is 1) `mail` has a specific `$to` field. So why are you defining "to" again in the $headers? It's not necessary certainly, and maybe it's even causing a problem. P.S. you're not even checking if `mail` returns true or false, so you haven't got very far in debugging this. – ADyson Jun 05 '20 at 08:13
  • Also, 2) you are allowing the user to specify the "from" address in the HTML form. This is a bad idea - many mailservers will reject such a message as spam if they receive it, and maybe even the server you're sending via won't even accept the message for delivery at all. This is because it's very likely that the "from" address will have a different domain than the server which is generating the email. Of course this is quite a common thing that email spoofers use for phishing attempts, spam, impersonation etc, so it's usually disallowed or flagged as suspicious. – ADyson Jun 05 '20 at 08:16
  • Instead, use a fixed "no-reply@yourdomain.com" type of address as the from field. If you want to include the provided email address in the message, you could simply put it in the message body, or try adding it as a reply-to field. – ADyson Jun 05 '20 at 08:17
  • Oh actually though, even before you get to all that, I just noticed: `JSON.stringify({ email: emailValue, subject: subjectValue, message: messageValue,})` is a problem too - you're sending JSON here, but PHP is expecting regular form-url-encoded POST values. So it won't be able to detect the `POST["message"]` variable from what you've sent. Read this: [Receive JSON POST with PHP](https://stackoverflow.com/questions/18866571/receive-json-post-with-php) and adjust your PHP code accordingly. Fix that first, and maybe you won't have any problems with the email itself....we'll see. – ADyson Jun 05 '20 at 08:20
  • Thank you for yours answers. I will try to follow this article to solve my problem. It is the first time that I code in PhP. – ag-dev Jun 05 '20 at 08:21
  • Another thing I would make as a general recommendation as well though is to use a well-known library such as [PHPMailer](https://github.com/PHPMailer/PHPMailer) to help you generate your email in PHP - it is more structured than mail() and can help you avoid some basic and hard-to-solve errors (like problems with your header format, and that kind of thing). They have done the hard work of making the PHP generate valid email data, so there's no point in struggling with it yourself. – ADyson Jun 05 '20 at 08:24

1 Answers1

0

Thank you @ADyson for your JSON POST with PHP link. I solve my problem like this :

<?php
  $data = json_decode(file_get_contents('php://input'), true);
  if (!empty($data))
  {
      $to = 'contact@ag-dev.fr';
      $from = $data['email'];
      $subject = utf8_decode($data['subject']);
      $message = utf8_decode($data['message']) . utf8_decode("\n\n\n\n Ceci est un message envoyé depuis le formulaire de contact. Pour répondre à ce mail, envoyez un mail à l'adresse suivante : " . $from);
      $headers  = 'MIME-Version: 1.0\r\n';
      $headers .= 'Content-type: text/html; charset=utf-8\r\n';
      $headers .= 'To: Guyomar Alexis <contact@ag-dev.fr>\r\n';
      $headers .= 'From: ' . ' <' . $from . '>\r\n';
      mail($to,$subject,$message,$headers);
  }
?>

now all run good.

ag-dev
  • 213
  • 2
  • 12