通过php发送电子邮件,为什么不起作用


Sending an email through php, why is not working?

我正试图从我的网站发送电子邮件,我输入了正确的名称和电子邮件,但它没有发送。。你能帮我吗?

URL正在被解析,html与php通信良好。我把这行

echo $_POST["userName"].' y '; 
//(to know if the name is getting to the php)
echo $_POST["userEmail"];
//(to know if the email is getting to the php)

它工作正常。。。可能是邮件线路出了问题。

这是我的PHP代码

elseif ($page_name=='contact-post.html') {
    if($_POST["userName"]!='' && $_POST["userEmail"]!=''){
        echo $_POST["userName"].' y '; 
        //(to know if the name is getting to the php
        echo $_POST["userEmail"];
        //(to know if the email is getting to the php
        $nombre = $_POST["userName"];
        $correo = $_POST["userEmail"];
        $asunto = "Mensaje de ".$nombre;
        $mensaje = $_POST["userMsg"];
        $correoMarsur = "mail@mail.com";
            if(mail($correoMarsur,$asunto,$mensaje,$correo)){
            echo ("<SCRIPT LANGUAGE='JavaScript'>
            window.alert('Mensaje enviado')
            window.location.href='http://page.cl';
            </SCRIPT>");
            }
            else{
            echo ("<SCRIPT LANGUAGE='JavaScript'>
            window.alert('Imposible enviar su mensaje')
            window.location.href='http://page.cl';
            </SCRIPT>"); 
            }
    }
    else{
        echo ("<SCRIPT LANGUAGE='JavaScript'>
            window.alert('Ingrese Nombre y Correo')
        window.location.href='http://page.cl';
            </SCRIPT>");     
    }
}

这是我的HTML代码

<div class="form">              
        <!--<form method="post" action="/index.php/contact-post.html"> -->
            <form method="post" action="../index.php/contact-post.html">
                <input name="userName" id="userName" type="text" class="textbox" placeholder="Nombre*" />
                <input name="userEmail" id="userEmail" type="text" class="textbox" placeholder="Email*" />
                <div class="clear"> </div>
                <div>
                    <textarea name="userMsg" id="userMsg" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Mensaje* ';}">Mensaje*</textarea>
                </div>  
                <div class="submit"> 
                    <input type="submit" value="Enviar " />
                </div>
            </form>
        </div>

邮件布尔值返回false,我该如何修复它?

您已将用户电子邮件放入mail()函数中的additional_headers参数槽中,这就是它返回false的原因。

这是您必须填写的参数:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

例如:

mail($correoMarsur,$asunto,$mensaje);

这应该有效,除非您的变量中有奇怪的数据。

//SEND MAIL FUNCTION
function sendMail($to, $subject, $body, $headers)
{
if (mail($to, $subject, $body, $headers)) {
$returnMessage = "Success";
}
else {
$returnMessage = "Failed";
}
return $returnMessage;
}    
//SET YOUR POST VARIABLES HERE
$name =  ($_POST['userName']); 
$email =  ($_POST['userEmail']);
//SET YOUR EMAIL DETAILS HERE, HOWEVER THIS CAN BE PASSED IN FROM ELSEWHERE, HENSE THE FUNCTION
$to = "mail@mail.com";
$subject = "Some subject";
$body = ("Name: " . $name . "'nEmail Address: " . $email . "'nSome other text you may want in your subject etc");
$headers = 'From: mail@mail.com' . "'r'n" .
    'Reply-To: mail@mail.com';

// CALL FUNCTION
sendMail($to, $subject, $body, $headers);

使用函数来提高代码的可重用性是一种很好的做法。这个邮件脚本现在可以用于不仅仅是这个联系人表单,它可以节省您重新编写整个内容的时间。

如果这不起作用(我已经测试过了,它起作用了(,可能是你的服务器SendMail,要检查是否启用了它,你可以创建一个PHPInfo并将其上传到你的FTP,以检查SendMail是否启用。

PHP信息:

<? phpinfo() ?>

我希望这能有所帮助,Dan