无法在php中的邮件标题中发送电子邮件ID

时间:2014-07-21 11:39:49

标签: php email

这是我的PHP代码:

$Name = isset($_POST['Name']) ? $_POST['Name'] : "";
$email = isset($_POST['email']) ? $_POST['email'] : "";
$mobileno = isset($_POST['mobileno']) ? $_POST['mobileno'] : "";
$city = isset($_POST['city']) ? $_POST['city'] : "";
$promo_code = isset($_POST['promo_code']) ? $_POST['promo_code'] : "";

if (isset($_POST['Submit'])) {
    if ($Name == "") {
        $Message = "Please Fill Your Name";
    } elseif ($email == "") {
        $Message = "Please Fill in the Email";
    } elseif ($mobileno == "") {
        $Message = "Please Fill in the Mobile No";
    } elseif ($city == "") {
        $Message = "Please Fill in the city";
    } elseif ($promo_code == "") {
        $Message = "Please Fill in the promo code";
    } else {

        $message = '<body>
                <table width="100%" border="2" cellspacing="2" cellpadding="0">
                <tr>
                <td width="600">Name:</td>
                <td>' . $Name . '</td>
                </tr>
                <tr>
                <td>Email:</td>
                <td>' . $email . '</td>
                </tr>
                <tr>
                <td>Mobile No:</td>
                <td>' . $mobileno . '</td>
                </tr>
                <tr>
                <td>City:</td>
                <td>' . $city . '</td>
                </tr>
                <tr>
                <td>promo_code:</td>
                <td>' . $promo_code . '</td>
                </tr>
                </table>
              </body>';
        $to = "pradeepmehta23@gmail.com";
        $headers = 'From:' . $_POST['email'] . '>\r\n';
        $headers .= "Content-type: text/html\r\n";
        $mailSent = false;
        $mailSent = mail($to, $subject, $message, $headers); // to the client

        if ($mailSent) {
            echo "<script type='text/javascript'>";
            echo "alert('Thank you for contacting us.  We will get back to you soon     ');";
            echo "</script>";
        }
    }
}

this is my html code
<html>
<head>
<title></title>
</head>
<body>
 <div class="main_wrap">
            <?php include('top_band.php'); ?>

            <div class="mid_wrap">
                <h1 style="padding: 10px 0px; font-size: 16px;">KhauGaliDeals Quiz Contest</h1>
                <!--<img src="images/cont_img/bakrapur_image.jpg" alt=""/> width="703" height="301" -->

                <div align="left">


                    <table width="499" border="0" cellpadding="4" cellspacing="4">
                        <td colspan="2"><p><strong>Fill Your Information</strong> 
                        </td>
                        <tr>
                            <td width="213" class="field_input">* Your Name :</td>
                            <td width="263" class="input"><input type="text" name="Name" value="<?php echo $Name; ?>" id="Name">
                            </td>
                        </tr>
                        <tr>
                            <td width="213" class="field_input">* Email Id :</td>
                            <td width="263" class="input"><input type="text" name="email" value="<?php echo $email; ?>" id="email">
                            </td>
                        </tr>
                        <tr>
                            <td height="35" class="field_input">* Mobile No. :</td>
                            <td class="input"><input type="text" name="mobileno" id="mobileno" value="<?php echo $mobileno; ?>"></td>
                        </tr>
                        <tr>
                            <td height="35" class="field_input">* City.:</td>
                            <td class="input"><input type="text" name="city" id="city" value="<?php echo $city; ?>"></td>
                        </tr>
                        <tr>
                            <td height="35" class="field_input">* Enter Order Id:</td>
                            <td class="input"><input type="text" name="promo_code" id="city" value="<?php echo $promo_code; ?>"></td>
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td><input name="Submit" id="Submit" value="Submit" type="submit" style="background:#e3a20b; color:#FFF; padding:4px 10px; font-size:11px; font-weight:bold; cursor:pointer;" /></td>
                        </tr>
                    </table>
                    </form>
                    </td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                    </tr>
                    </table>
                    <br> 
                    </td>
                    </tr>
                    </table>
                </div>
            </div>
        </div> 
        <div class="deals_wrap"></div>
    </div>
    <?php include('footer.php'); ?>
</body>
</html>

我的代码工作正常我唯一的问题是,当我包含此代码时

$headers = 'From:' . $_POST['email'] . '>\r\n'; 

邮件未来,但我收到了JavaScript消息,感谢您与我们联系以及删除代码时

$headers = 'From:' . $_POST['email'] . '>\r\n'; 

我收到了邮件,但它向未知的发件人显示了如何获取电子邮件ID以代替未知发件人的任何帮助。

3 个答案:

答案 0 :(得分:0)

尝试使用此 -

$to = 'mail@gmail.com';
$from = $_POST['email'] // sender
$subject = "your subject";
$message = '<body>
            <table width="100%" border="2" cellspacing="2" cellpadding="0">
            <tr>
            <td width="600">Name:</td>
            <td>' . $Name . '</td>
            </tr>
            <tr>
            <td>Email:</td>
            <td>' . $email . '</td>
            </tr>
            <tr>
            <td>Mobile No:</td>
            <td>' . $mobileno . '</td>
            </tr>
            <tr>
            <td>City:</td>
            <td>' . $city . '</td>
            </tr>
            <tr>
            <td>promo_code:</td>
            <td>' . $promo_code . '</td>
            </tr>
            </table>
          </body>';

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset: utf8\r\n";
// Additional headers
$headers .= "From: <$from>" . "\r\n";

// Mail it
if (mail($to, $subject, $message, $headers)){
    echo "email sent successfully";
}

答案 1 :(得分:0)

你可以试试这个。

$headers = "From: $Name <$from>" . PHP_EOL;
$headers .= "To: $to" . PHP_EOL;
$headers .= "Content-Type: text/html" . PHP_EOL;
$mailSent = mail($to, $subject, $message, $headers); 

使用PHP_EOL时最好使用\r\n。因为它取决于您的代码运行的操作系统如何识别&#34; End Of Line&#34;字符。 因此,PHP_EOL将始终引导您找到正确的方向。

注意:通过更改 参数发送邮件通常会向收件人的邮箱显示警告,例如&#34;这可能不是由原始发件人发送&#34;

答案 2 :(得分:0)

您没有插入换行符,因为换行符\r\n只能用双引号解析而您不会在代码中关闭>。试试这样的事情

$headers = 'From: ' . $_POST['email'] . "\r\n";
$headers .= 'Content-type: text/html'. "\r\n";