0

My PHP script doesn't send mail. What's wrong? Do I need headers or other things? Is mail not supported?

PHP code:

<?php 
if(isset($_POST['submit'])){
    $to = "mymail@gmail.com"; // this is your Email address
    $from = $_POST['mail']; // this is the sender's Email address
    $voornaam = $_POST['voornaam'];
    $achternaam = $_POST['achternaam'];
    $telefoon = $_POST['telefoon'];
    $personen = $_POST['personen'];
    $datum = $_POST['datum'];
    $tijd = $_POST['tijd'];
    $opmerking = $_POST['opmerking'];
    $subject = "Reservering via Website";
    $subject2 = "KOPIE: Reservering bij .....";
    $message = "Voornaam: " . $voornaam . "\n\n" . "Achternaam: " . $achternaam . "\n\n" . "Telefoon: " . $telefoon . "\n\n" . "E-Mail: " . $from . "\n\n" . "Aantal Personen: " . $personen . "\n\n" . "Datum: " . $datum . "\n\n" . "Tijd: " . $tijd . "\n\n" . "\n\n" . "Opmerking:" . "\n\n" . $opmerking;
    $message2 = "Hartelijk dank voor uw reservering." . "\n\n" . "Voornaam: " . $voornaam . "\n\n" . "Achternaam: " . $achternaam . "\n\n" . "Telefoon: " . $telefoon . "\n\n" . "E-Mail: " . $from . "\n\n" . "Aantal Personen: " . $personen . "\n\n" . "Datum: " . $datum . "\n\n" . "Tijd: " . $tijd . "\n\n" . "\n\n" . "Opmerking:" . "\n\n" . $opmerking;

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $voornaam . ", we will contact you shortly.";
}
?>

HTML code:

<form action="" method="post">
      <table class="tg">
          <tr>
              <td class="tg-yw4l">Voornaam:</td>
              <td class="tg-yw4l">
                  <input type="text" name="voornaam">
              </td>
          </tr>
          <tr>
              <td class="tg-yw4l">Achternaam:*</td>
              <td class="tg-yw4l">
                   <input type="text" name="achternaam" required>
              </td>
          </tr>
          <tr>
              <td class="tg-yw4l">Telefoon:</td>
              <td class="tg-yw4l">
                   <input type="text" name="telefoon">
              </td>
          </tr>
          <tr>
              <td class="tg-yw4l">E-Mail:*</td>
              <td class="tg-yw4l">
                   <input type="text" name="mail" required>
              </td>
          </tr>
          <tr>
              <td class="tg-yw4l"></td>
              <td class="tg-yw4l"></td>
          </tr>
          <tr>
              <td class="tg-yw4l">Aantal Personen:*</td>
              <td class="tg-yw4l">
                  <input type="text" name="personen" required>
              </td>
          </tr>
          <tr>
              <td class="tg-yw4l">Datum:*</td>
              <td class="tg-yw4l">
                  <input type="date" name="datum" required>
              </td>
          </tr>
          <tr>
              <td class="tg-yw4l">Tijd:*</td>
              <td class="tg-yw4l">
                  <input type="time" name="tijd" required>
              </td>
          </tr>
          <tr>
              <td class="tg-yw4l"></td>
              <td class="tg-yw4l"></td>
          </tr>
          <tr>
              <td class="tg-yw4l">Vraag/Opmerking:</td>
              <td class="tg-yw4l">
                  <textarea rows="10" cols="40" name="opmerking"></textarea>
              </td>
          </tr>
          <tr>
              <td class="tg-yw4l"></td>
              <td class="tg-yw4l">
                   <input type="submit" name="submit" value="Reserveren">
              </td>
          </tr>
      </table>
</form>

I can't see what I have done wrong here, maybe that my subject, body, and headers are in the wrong places, but other than that I'm not sure really. If anyone could explain what's wrong here I'd be very grateful.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42

4 Answers4

0

PHP mail() function cant send mail. Because this function use external application for send email -- MTA (Mail Transfer Agent).

If you really want send email via PHP mail() function, you must install MTA application (sendmail, postfix, exim, etc.) and configure PHP (php.ini file, sendmail_path option) for send email with this application.

Deep
  • 2,472
  • 2
  • 15
  • 25
0

I always add another header. You can use it like this:

$headers .= "MIME-version: 1.0\nContent-Type: text/html; charset=utf-8\n";

You can always use a different content-type and charset of course.

You can also check if the mail function errored by adding

$send = mail(...)
if ($send) {
echo "Success";
} else { 
print_r(error_get_last());
}

Give it a try :)

Chris G
  • 787
  • 6
  • 20
0

You could used phpmailer class to send emails. Under this assumption, I re-coded yours as following. I works... I tested. Note: you try to send two mails (destiny and owner (i think). I used to run this code twice or send CC instead. I hope it fits for you.

Try this...

<?php 
   require('class.phpmailer.php');

   $port = '587'; // set the SMTP server port
   $host = 'mail.yourhost.com.ar';   // SMTP server
   $username   = 'info@yourhost.com.ar';   // SMTP server username
   $password   = 'yourpass';   // SMTP server password


   if(isset($_POST['submit'])){
           $to = "mymail@gmail.com"; // this is your Email address
       $from = $_POST['mail']; // this is the sender's Email address
       $voornaam = $_POST['voornaam'];
       $achternaam = $_POST['achternaam'];
       $telefoon = $_POST['telefoon'];
       $personen = $_POST['personen'];
       $datum = $_POST['datum'];
       $tijd = $_POST['tijd'];
       $opmerking = $_POST['opmerking'];
       $subject = "Reservering via Website";
       $subject2 = "KOPIE: Reservering bij .....";
       $message = "Voornaam: " . $voornaam . "\n\n" . "Achternaam: " .     $achternaam . "\n\n" . "Telefoon: " . $telefoon . "\n\n" . "E-Mail: " . $from . "\n\n" . "Aantal Personen: " . $personen . "\n\n" . "Datum: " . $datum . "\n\n" . "Tijd: " . $tijd . "\n\n" . "\n\n" . "Opmerking:" . "\n\n" . $opmerking;
       $message2 = "Hartelijk dank voor uw reservering." . "\n\n" . "Voornaam: " . $voornaam . "\n\n" . "Achternaam: " . $achternaam . "\n\n" . "Telefoon: " . $telefoon . "\n\n" . "E-Mail: " . $from . "\n\n" . "Aantal Personen: " . $personen . "\n\n" . "Datum: " . $datum . "\n\n" . "Tijd: " . $tijd . "\n\n" . "\n\n" . "Opmerking:" . "\n\n" . $opmerking;

       $headers = "From:" . $from;
       $headers2 = "From:" . $to;

       $mail = new PHPMailer(); // defaults to using php "mail()"

         $mail->SMTPDebug = false;  // Disable debug  

        $mail->IsSMTP();                 // Con la propiedad Mailer le indicamos que vamos a usar un servidor smtp
        $mail->SMTPAuth   = true;        // enable SMTP authentication
        $mail->Port       = $port;
        $mail->Host       = $host;
        $mail->Username   = $username;
        $mail->Password   = $password;

        $mail->SetFrom ($from,$voornaam);     // @mail from
        $mail->AddAddress($to, $to);    // @destiny

         $mail->Subject    = $subject;    // subject

         $mail->MsgHTML($message);

         $intentos = 1;
         //se envia el mensaje, si no ha habido problemas la variable $exito tendra el valor true
        $exito = $mail->Send();

        //Si el mensaje no ha podido ser enviado se realizaran 4 intentos mas como mucho 
        //para intentar enviar el mensaje, cada intento se hara 5 segundos despues 
        //del anterior, para ello se usa la funcion sleep   
    while ((!$exito) && ($intentos < $reintentos)) {
            sleep(1);
            echo "<br/>".$mail->ErrorInfo;
            $exito = $mail->Send();
            $intentos = $intentos+1;    
        }

         $respuesta = false;  // devolucion a aplicaicon principal
        if(!$exito){
            echo "error. ". $mail->ErrorInfo;
            $respuesta = $mail->ErrorInfo;  
        } else {
            echo "Mail Sent. Thank you " . $voornaam . ", we will contact you shortly.";
            $respuesta = true;
        }

   }

?>
Claudio
  • 296
  • 2
  • 6
0

First, you should check the return value of mail(), it may fail.

function.mail.php

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise

Second, under the hood mail() use the command defined in the variable sendmail_path of php.ini (default /usr/sbin/sendmail -t -i), eventually you can use phpinfo() to check the current value.

mail.configuration.php

The sendmail command may be missing or misconfigured in your system, or configured to accept From values only for some specific domain (I'm guessing here because your question doesn't include details on the environment where php is running, but those are common problems when mail() fail).

If you're running php on a *nix environment and have access to a shell then you can check manually if the command is working:

echo 'mail message' | sendmail -t -i example@email.cc

If isn't working you may need to configure your current MTA (check the logs/queue/spool if doesn't fail) or install one just to handle php mail, any MTA which accept sendmail syntax should work (sometime I use msmtp and set sendmail_path for it /usr/bin/msmtp -C /usr/htdocs/msmtprc --from=default -t)

If you're running on a hosting server check the error logs in the control panel, there should be a message on mail() failure.

As already mentioned there are php only solutions (phpmailer probably the most used) which aren't using the sendmail command.

Alex
  • 3,264
  • 1
  • 25
  • 40