与发件人的PHP邮件

时间:2016-10-04 08:14:33

标签: php email post

在这个邮件形式中,除了没有发件人外,一切都有效,我的邮件直接进入垃圾箱!是因为这个邮件表上没有发件人吗?我想从发件人那里获取信息,以便在哪里填写发件人的代码?

<?php
/* Set e-mail recipient */
$myemail = "xxxxx@live.com";

/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "Your Name");
$email = check_input($_POST['inputEmail'], "Your E-mail Address");
$subject = check_input($_POST['inputSubject'], "Message Subject");
$message = check_input($_POST['inputMessage'], "Your Message");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */


$message = "

Someone has sent you a message from xxxxxxx.com:

Name: $name
Email: $email
Subject: $subject

Message:
$message

";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: http://www.xxxxxxx.com/confirmation.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>

  <body>

    <p>Please correct the following error:</p>
    <strong><?php echo $myError; ?></strong>
    <p>Hit the back button and try again</p>

  </body>
</html>
<?php
exit();
}
?>

1 个答案:

答案 0 :(得分:2)

您可以使用“附加标题”参数将发件人的电子邮件地址添加到邮件:

mail($myemail, $subject, $message, "From: sender@example.com");

编辑:在您的情况下,我认为您需要传入代码中前面定义的$ email变量。这将显示电子邮件来自表单中输入的电子邮件地址。

mail($myemail, $subject, $message, "From: " . $email);

请参阅:http://php.net/manual/en/function.mail.php

相关问题