PHP邮件功能不起作用

时间:2014-11-04 07:54:28

标签: php

我已经使用php联系表单为我的网站。但是它无法正常工作。当我正确填写所有字段并提交。它显示错误消息“无效数据”。

<?php 

$action=$_REQUEST['action']; 
if ($action=="")    
    { 
    ?> 
    <form  action="" method="POST" enctype="multipart/form-data"> 
    <input type="hidden" name="action" value="submit"> 
    Your name:<br> 
    <input name="name" type="text" value="" size="30" required/><br> 
    Your email:<br> 
    <input name="email" type="email" value="" size="30" required /><br> 
    Your message:<br> 
    <textarea name="message" rows="7" cols="30" required></textarea><br> <br> 
    <input type="submit" value="Send email" class="topbarbtn"/> 
    </form> 

<?php 
$subject=$_REQUEST['name']; 
$email=$_REQUEST['email']; 
 $body=$_REQUEST['message']; 
}else{
$to = "abc@abc.com";
$subject = $subject;
$from = $email;
$message = $body;


if (($from=="")||($subject=="")||($message=="")) 
        { 
       echo  '<script type="text/javascript">alert("Invalid Details");</script>' ;
        } else{

$headers = "From: " . $from . "\r\n";
$body .= $message;
mail($to, $subject, $body, $headers);
}
?>

2 个答案:

答案 0 :(得分:0)

注意到您的逻辑中存在缺陷 - 您尝试将$设置为$ email,而您没有获得变量$ email

语句中添加以下行:

$from = $_REQUEST['email'];

另外,如果邮件仍未发送,您应该考虑到某些邮件服务器(例如qmail)如果使用\ r \ n将拒绝您的邮件。 因此,您应该尝试仅使用\ n或\ n \ n作为标题中的换行符。

答案 1 :(得分:0)

像这样有效:

<?php 

$action=$_REQUEST['action']; 


if ($action=="") {?> 

    <form  action="" method="POST" enctype="multipart/form-data"> 
        <input type="hidden" name="action" value="submit"> 
        Your name:<br> 
        <input name="name" type="text" value="" size="30" required/><br> 
        Your email:<br> 
        <input name="email" type="email" value="" size="30" required /><br> 
        Your message:<br> 
        <textarea name="message" rows="7" cols="30" required></textarea><br> <br> 
        <input type="submit" value="Send email" class="topbarbtn"/> 
    </form> 

<?php 

} 
else {
    $to = "...mail address...";
    $subject = $_REQUEST['name'];
    $from = $_REQUEST['email'];
    $message = $_REQUEST['message'];


    if (($from=="")||($subject=="")||($message=="")) { 
        echo  '<script type="text/javascript">alert("Invalid Details");</script>' ;
    } 
    else{
        $headers = "From: " . $from . "\r\n";
        $body .= $message;
        mail($to, $subject, $body, $headers);
    }
}
?>