PHP表单 - 不接收电子邮件

时间:2014-02-14 18:02:04

标签: php

我不明白为什么我在填写简单表格后没有收到表格中的电子邮件。点击提交按钮后,它会重定向到感谢页面,没有任何问题,但没有电子邮件。

HTML

<form class="action" name="form1" method="POST" action="_sendmail2.php" onSubmit="return CheckAll(this);">
    <label class="nick-2">Full Name:</label><br>
    <input type="text" class="name" name="full_name">
    <label class="nick">Email Address:</label><br>
    <input type="text" class="email" name="email"><br>
    <div class="radio-toolbar">  
        <input type="radio" id="radio1" name="agent_type" value="Buyer" checked>
        <label for="radio1">Buyer</label>
        <input type="radio" id="radio2" name="agent_type" value="Seller">
        <label for="radio2">Seller</label>
        <input type="radio" id="radio3" name="agent_type" value="Investor">
        <label for="radio3">Investor</label>
    </div><br>
    <input type="submit" class="btn" value="SUBMIT" name="Submit">
</form> 

PHP(

<?php 
$to = "cluneborg@hotmail.com"; 
$subject = "New Agent Inquries"; 
$full_name = $_POST['full_name']; 
$email = $_POST['email']; 
$agent_type = $_POST['agent_type'];

if($_SERVER['REQUEST_METHOD']=="POST") {    

$full_name=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['full_name']));  
$email=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['email']));  
$agent_type=str_replace ( array("\n"), array("   <br>"),trim($_REQUEST['agent_type']));   

$contentmsg=stripslashes("<br><b><font style=color:#CC3300>$subject</font></b><br>
<table width=708 border=0 cellpadding=2 cellspacing=1 bgcolor=#CCCCCC>

 <tr>
  <td width=165 align=right valign=top bgcolor=#FFFFFF><B>Full Name: </b> </td>
  <td width=565 align=left valign=top bgcolor=#FFFFFF> $full_name</td>
</tr>

<tr>
  <td width=165 align=right valign=top bgcolor=#FFFFFF><B>Email Address: </b> </td>
  <td width=565 align=left valign=top bgcolor=#FFFFFF> $email</td>
</tr>

<tr>
  <td width=165 align=right valign=top bgcolor=#FFFFFF><B>Type of Agent:</b> </td>
  <td width=565 align=left valign=top bgcolor=#FFFFFF> $agent_type</td>
</tr>

</table>
");

$headers  = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= 'To: Eric <eluneborg@gmail.com>' . "\r\n";
$headers .= 'From: Texas Real Estate Agent Website' . "\r\n";

if(mail($to,$subject,$contentmsg,$headers)){
header("Location: http://www.magnixsolutions.com/clients/tas/thanks.html"); 
} 
else
{ 
echo "Mail was not sent!"; 
} 
}
?>

有时它会向我的hotmail发送电子邮件,大部分时间都会收到此邮件(在cpanel上检查)

This message was created automatically by mail delivery software.

A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

cluneborg@hotmail.com
Domain magnixsolutions.com has exceeded the max defers and failures per hour (5/5      (55%)) allowed. Message discarded.

3 个答案:

答案 0 :(得分:1)

(已测试) - 您的代码存在一些问题。

  1. 最重要的是@中的@mail符号 - 这不会执行,需要删除。
  2. 现在,这一行:(在PHP中)

    $_REQUEST['type_agent']
    

    应该是:

    $_REQUEST['agent_type']
    

    按照:(以HTML格式)

    <input type="radio" id="radio3" name="agent_type" value="Investor">
    

    然后您的标题不正确,我添加了一些\r\n

    你的一个标题(在PHP中)

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

    已更改为:

    $headers .= "From: $full_name <$email>\r\n";
    

    旁注:可以替换为

    $headers .= "From: $fromemail <$email>\r\n";
    

    如果您希望名称在邮件中显示为“新代理”,而不是发送电子邮件的人名。


    将此$fromemail="New Agent";$from=$fromemail;$headers .= "From: ".$from."";

    结合使用

    会导致邮件进入垃圾邮件,因为它不是真正的电子邮件地址。

    另外,在测试您的原始代码时,它没有作为正确的HTML出现,但代码本身出现在电子邮件中;已被纠正。


    如果您需要电子邮件和名称,则需要使用两个不同的变量。

    I.e。:

    $headers .= 'From: YourName <YourName@domain.com>' . "\r\n";
    

    ,在你的情况下:

    $headers .= "From: $full_name <$email>\r\n";
    

    重写:(PHP)

    <?php ob_start();
    // commented out - is not needed for the time being
    // $fromemail="New Agent"; // change here if you want
    
    $toemail="email@example.com";   // change here if you want
    
    $sub="Agent Inquiries";          // change here if you want
    $success_page_name="thanks.html";
    
    ////// do not change in following
    if($_SERVER['REQUEST_METHOD']=="POST")
    {
        $full_name=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['full_name']));  
        $email=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['email']));  
        $type_agent=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['agent_type']));  
    
        $contentmsg=stripslashes("<br><b><font style=color:#CC3300>$sub</font></b><br>
        <table width=708 border=0 cellpadding=2 cellspacing=1 bgcolor=#CCCCCC>
    
        <tr>
          <td width=165 align=right valign=top bgcolor=#FFFFFF><B>Full Name: </b> </td>
          <td width=565 align=left valign=top bgcolor=#FFFFFF>$full_name</td>
        </tr>
    
        <tr>
          <td width=165 align=right valign=top bgcolor=#FFFFFF><B>Email Address: </b> </td>
          <td width=565 align=left valign=top bgcolor=#FFFFFF>$email</td>
        </tr>
    
        <tr>
          <td width=165 align=right valign=top bgcolor=#FFFFFF><B>Type of Agent:</b> </td>
          <td width=565 align=left valign=top bgcolor=#FFFFFF>$type_agent</td>
        </tr>
    
        </table>
        ");
    
        $headers  = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
        $headers .= "From: $full_name <$email>\r\n";
    
        @mail($toemail,$sub,$contentmsg,$headers);
    
        header("Location:$success_page_name");
    }
    ?>
    

    <强>脚注:

    1. @中包含@mail符号会抑制错误并且不会执行该功能,因此您需要将其删除..

    2. 在我的测试中,我删除了onSubmit="return CheckAll(this);,因为您的完整代码不包含该功能。如果它失败了,那么你也可能需要删除它。

答案 1 :(得分:0)

您需要在标题中添加EOL字符\n才能分开。不知道这是否是 解决方案,但至少需要注意一个问题。

$headers  = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";

$from=$fromemail;

$headers .= "From: ".$from."\n";

除了错误抑制答案/评论之外,您还可以确保mail()返回true,表示您的服务器已接受并将尝试投放。

$success = mail($toemail,$sub,$contentmsg,$headers);
var_dump( $success ); // should be true

答案 2 :(得分:0)

从@mail命令中删除@符号,它可能会给您一个有用的错误。 @符号有抑制错误: http://www.php.net/manual/en/language.operators.errorcontrol.php