簡體   English   中英

似乎無法讓我的PHP表單像從用戶的電子郵件中發送一樣

[英]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.ukmyname@email.example.com類的地址上失敗。 使用PHP的filter_var代替。

第二點:即使您可以使用此服務,您的ISP也會很好地阻止任何似乎不是來自其服務器上電子郵件帳戶的郵件,因此無論如何都不會發送該郵件。 最好設置一個虛擬帳戶,如webform@mydomain.com (使用您的實際域),將其用作發件人地址,並將用戶電子郵件包含在郵件正文中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM