简体   繁体   English

似乎无法让我的PHP表单像从用户的电子邮件中发送一样

[英]Can't seem to get my PHP form to send as though from user's email

I have tried everything but I can't seem to add the function to make it so that the email appears in my inbox as though from the user's email rather than my host name. 我已经尝试了所有方法,但似乎无法添加使它生效的功能,以使该电子邮件像从用户的电子邮件而不是主机名一样显示在我的收件箱中。

I've tried adding the header attributes but it still didn't seem to work. 我尝试添加标头属性,但它似乎仍然无法正常工作。

Here's the code: 这是代码:

<?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();
}
?>

Thanks! 谢谢!

Take the $headers from here: 从此处获取$headers

@mail($email_to, $email_subject, $email_message, $headers);  

and put it in here: 并放在这里:

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

Then remove the first line (copied from a site somewhere I'd guess) 然后删除第一行(从我猜到的某个站点复制)

You should be left with 你应该留给你

// 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);

Note: your regex validating the email address will fail on addresses like myname@example.co.uk or myname@email.example.com . 注意:您的正则表达式验证电子邮件地址将在myname@example.co.ukmyname@email.example.com类的地址上失败。 use PHP's filter_var instead. 使用PHP的filter_var代替。

2nd note: even if you get this to work your ISP may well block any mail that doesn't appear to originate from an email account on their servers, so the mail won't be delivered anyway. 第二点:即使您可以使用此服务,您的ISP也会很好地阻止任何似乎不是来自其服务器上电子邮件帐户的邮件,因此无论如何都不会发送该邮件。 Better to set up a dummy account like webform@mydomain.com (use your actual domain), use that as a from address and include the user email in the body of the message. 最好设置一个虚拟帐户,如webform@mydomain.com (使用您的实际域),将其用作发件人地址,并将用户电子邮件包含在邮件正文中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM