PHP mail()执行但它不发送邮件

时间:2015-12-25 20:41:22

标签: php html email

我的PHP脚本不发送邮件。怎么了?我需要标题还是其他东西?邮件不受支持吗?

PHP代码:

<?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>

我无法看到我在这里做错了什么,也许我的主题,身体和标题都在错误的地方,但除此之外,我不确定。 如果有人能解释这里的错误,我将非常感激。

4 个答案:

答案 0 :(得分:0)

PHP mail()函数无法发送邮件。因为此功能使用外部应用程序发送电子邮件--MTA(邮件传输代理)。

如果您真的想通过PHP mail()函数发送电子邮件,则必须安装MTA应用程序(sendmail,postfix,exim等)并配置PHP(php.ini文件,sendmail_path选项)以发送此应用程序的电子邮件。

答案 1 :(得分:0)

我总是添加另一个标题。您可以像这样使用它:

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

当然,您总是可以使用不同的内容类型和字符集。

您还可以通过添加

来检查邮件功能是否出错
$send = mail(...)
if ($send) {
echo "Success";
} else { 
print_r(error_get_last());
}

试一试:)

答案 2 :(得分:0)

您可以使用phpmailer类发送电子邮件。在这个假设下,我将你的重新编码如下。我工作......我测试过。 注意:你尝试发送两封邮件(命运和所有者(我认为)。我曾经两次运行此代码或发送CC代替。 我希望它适合你。

试试这个......

<?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;
        }

   }

?>

答案 3 :(得分:0)

首先,您应该检查mail()的返回值,它可能会失败。

function.mail.php

  

如果邮件成功接受传递,则返回TRUE,否则返回

其次,在引擎mail()下使用sendmail_path变量php.ini中定义的命令(默认/usr/sbin/sendmail -t -i),最终您可以使用phpinfo()来检查当前值。

mail.configuration.php

sendmail命令可能在您的系统中丢失或配置错误,或者配置为仅针对某些特定域接受From值(我在此猜测,因为您的问题并非如此)包括运行php的环境的详细信息,但这些是mail()失败时的常见问题。

如果您在* nix环境中运行php并且可以访问shell,那么您可以手动检查该命令是否正常工作:

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

如果无法正常工作,您可能需要配置当前的 MTA (检查日志/队列/假脱机,如果没有失败)或安装一个只是为了处理php邮件,接受sendmail语法的任何 MTA 应该有效(有时我会使用msmtp并为其设置sendmail_path /usr/bin/msmtp -C /usr/htdocs/msmtprc --from=default -t

如果您在托管服务器上运行,请检查控制面板中的错误日志,mail()失败时应该显示一条消息。

如前所述, php only 解决方案( phpmailer 可能是最常用的)并没有使用 sendmail 命令。