PHP mail()执行,但不执行';不要发邮件

PHP mail()执行,但不执行';不要发邮件,php,html,email,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'];

我的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.";
}
?>
PHP mail()函数无法发送邮件。因为此函数使用外部应用程序发送电子邮件--MTA(邮件传输代理)


如果您确实希望通过PHP mail()函数发送电子邮件,则必须安装MTA应用程序(sendmail、postfix、exim等),并配置PHP(PHP.ini文件、sendmail_路径选项),以便使用此应用程序发送电子邮件。

我始终会添加另一个标题。您可以这样使用它:

$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());
}

试试看:)

您可以使用phpmailer类发送电子邮件。在此假设下,我将您的代码重新编码如下。我工作。。。我做了测试。 注意:您尝试发送两封邮件(我想是destiny和owner)。我曾经两次运行此代码,或者发送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;
        }

   }

?>

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

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

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

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

如果您在*nix环境中运行php,并且有权访问shell,则可以手动检查命令是否有效:

echo 'mail message' | sendmail -t -i example@email.cc
如果不工作,您可能需要配置当前MTA(如果没有失败,请检查日志/队列/假脱机)或安装一个MTA来处理php邮件,任何接受
sendmail
语法的MTA都应该工作(有时我使用并为其设置
sendmail\u路径
/usr/bin/msmtp-C/usr/htdocs/msmtrc--from=default-t

如果您在托管服务器上运行,请检查控制面板中的错误日志,应该会有一条关于
mail()
failure的消息


如前所述,有一些仅使用php的解决方案(phpmailer可能是最常用的)不使用sendmail命令。

您是在本地主机上运行此解决方案,还是在配置了SMTP的服务器上运行此解决方案?我也会检查您的垃圾邮件过滤器,有时根据我的经验,它会被抛出其中。
echo 'mail message' | sendmail -t -i example@email.cc