[英]Can't seem to get my PHP form to send as though from user's email
我已经尝试了所有方法,但似乎无法添加使它生效的功能,以使该电子邮件像从用户的电子邮件而不是主机名一样显示在我的收件箱中。
我尝试添加标头属性,但它似乎仍然无法正常工作。
这是代码:
<?php
/* Set e-mail recipient */
$myemail = "julia@hotmail.com";
/* Check all form inputs using check_input function */
$email = check_input($_POST['email']);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "
The email below would like to be added to the mailing list.
E-mail: $email
";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: trial2.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();
}
?>
谢谢!
从此处获取$headers
:
@mail($email_to, $email_subject, $email_message, $headers);
并放在这里:
/* Send the message using mail() function */
mail($myemail, $subject, $message);
然后删除第一行(从我猜到的某个站点复制)
你应该留给你
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
/* Send the message using mail() function */
mail($myemail, $subject, $message, $headers);
注意:您的正则表达式验证电子邮件地址将在myname@example.co.uk
或myname@email.example.com
类的地址上失败。 使用PHP的filter_var
代替。
第二点:即使您可以使用此服务,您的ISP也会很好地阻止任何似乎不是来自其服务器上电子邮件帐户的邮件,因此无论如何都不会发送该邮件。 最好设置一个虚拟帐户,如webform@mydomain.com
(使用您的实际域),将其用作发件人地址,并将用户电子邮件包含在邮件正文中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.