为什么我在发送PHP电子邮件时无法获取输入名称

时间:2014-12-02 18:57:33

标签: php html email

<?php
$name = $_POST['name'];//check here i want this both to send to my email with user input
$email = $_POST['email'];//check here i want this both to send to my email with user input
echo $name;
$email = "mymail@gmail.com";
$to = "myemail@gmail.com";
$subject = "Hi!";
$body = "Hi,How are you?";
$headers = 'From: ' .$email . "\r\n".'Reply-To: ' . $email. "\r\n".'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $body, $name, $headers)) echo("<p>Email successfully sent</p>");
else echo("<p>Email delivery failed</p>");
?>

HTML

<!DOCTYPE HTML> 
<html>
<head>
    <title>PHP form to email sample form</title>
<!-- define some style elements-->



</head>


<body>

    <form action="sendmail.php" method="post">
    <ul style="list-style-type: none;">
    <li>Name: <input type="text" name="name" size="30" maxlength="40"></li>
    <li>&nbsp;</li>
    <li>Email: <input type="text" name="email" size="30" maxlength="40"></li>
    <li>&nbsp;</li>

    <li><input type="submit" name="submit" value="Send"></li>
    </ul>
    </form>

    </body>
    </html>

我已成功收到发送到我的电子邮件帐户的邮件,但是我有一个输入nameemail我无法在邮件中发送?我做错了什么?任何人都可以帮我修复它吗?

2 个答案:

答案 0 :(得分:1)

您没有正确地将参数传递给邮件功能:

http://php.net/manual/en/function.mail.php

如果您希望名称显示在邮件标题中,请执行以下操作:

$headers = 'From: ' . $name . '<' . $email . '>' . "\r\n" . 'Reply-To: ' . $name . '<' . $email . '>' "\r\n" . 'X-Mailer: PHP/' . phpversion();

如果你想在体内取名,请执行以下操作:

$body = 'Hi ' . $name . ', How are you?';

然后调用没有$ name参数的函数:

mail($to, $subject, $body, $headers)

谢谢,

安德鲁

答案 1 :(得分:0)

名称应位于标题上。在标题上尝试此操作并从邮件功能中删除$ name

$name.` <`.$email.`>`;