gpt4 book ai didi

PHP 邮件功能未正确设置 header

转载 作者:行者123 更新时间:2023-12-02 15:34:32 25 4
gpt4 key购买 nike

我有一个 php 邮件功能,可以发送包含联系表内容的电子邮件。但是当我在我的网站上发送这封电子邮件时。输入的电子邮件地址不是 from header ,而是 username@srv30.000webhost.com,我不知道为什么。我的 php 邮件功能如下。

$email 是用户输入的邮箱。

编辑:主题:你好评论X-PHP-脚本:kwp.host22.com/form_action.php for 78.147.187.64消息 ID:<20131124204151.478EC28021@srv30.000webhost.com>日期:2013 年 11 月 24 日星期日 15:41:51 -0500 (EST)发件人:a5485011@srv30.000webhost.com返回路径:a5485011@srv30.000webhost.comX-OriginalArrivalTime:2013 年 11 月 24 日 20:41:52.0292 (UTC) FILETIME=[9B09B640:01CEE955]

完整的 php 脚本:

$name = ($_GET['Name']) ? $_GET['Name'] : $_POST['Name'];
$email = ($_GET['Email']) ?$_GET['Email'] : $_POST['Email'];
$phone = ($_GET['Phone']) ?$_GET['Phone'] : $_POST['Phone'];
$comment = ($_GET['Comment']) ?$_GET['Comment'] : $_POST['Comment'];

// flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;

// Simple server side validation for POST data, of course, you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.';
if (!$comment) $errors[count($errors)] = 'Please enter your comment.';

// if the errors array is empty, send the mail
if (!$errors) {

// recipient
$to = '<example@example.com>';
// sender
$from = $name . ' <' . $email . '>';

// subject and the html message
$subject = 'Comment from ' . $name;
$message = '
<!DOCTYPE html>
<html>
<head></head>
<body>
<table>
<tr><td>Name</td><td>' . $name . '</td></tr>
<tr><td>Email</td><td>' . $email . '</td></tr>
<tr><td>Phone</td><td>' . $phone . '</td></tr>
<tr><td>Comment</td><td>' . nl2br($comment) . '</td></tr>
</table>
</body>
</html>';

// send the mail
$result = @mail($to, $subject, $message, $from);

// if POST was used, display the message straight away
if ($_POST) {
if ($result) echo 'Thank you! We have received your message.';
else echo 'Sorry, unexpected error. Please try again later';

// else if GET was used, return the boolean value so that
// ajax script can react accordingly
// 1 means success, 0 means failed
} else {
echo $result; }

// if the errors array has values
} else {
// display the errors message
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
echo '<a href="contact.php">Back</a>';

exit;}
function sendmail($to, $subject, $message, $from) {

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $email . "\r\n";
$headers .= 'Reply-To: ' .$email . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
@mail($to, $subject, $message, $headers);

if ($result) return 1;
else return 0;}

我是 PHP 的新手,因此我们将不胜感激。谢谢

最佳答案

我相信这条线

$result = @mail($to, $subject, $message, $from);

应该固定为这个

$result = sendmail($to, $subject, $message, $from);

并将您的发送邮件功能更改为此

function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Reply-To: ' .$from . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
@mail($to, $subject, $message, $headers);

if ($result) return 1;
else return 0;
}

此处您使用的是 $email,但也许您应该使用 $from。您的函数接受 $to、$subject、$message、$from。

此外,您也许应该稍微更改一下代码

function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Reply-To: ' .$from . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)){
return 1;
}
return 0;
}

sendmail 是一个用于发送电子邮件的 unix 应用程序二进制文件。用 PHP 编写您自己的 sendmail 函数不会做任何事情。我相信 PHP 邮件功能在 linux 上默认使用 sendmail,在 windows 上,您需要配置 SMTP。但是在发送您的电子邮件时,这不是问题所在。也许你应该重命名函数,这样你就不会无意中混用这些东西。

这里也重写了整个代码,也许你可以学到一两件事:)

$post = false;
// flag to indicate which method it uses. If POST set it to 1
if (isset($_POST['Name'])) {
$post = true;
}

$name = (isset($_GET['Name'])) ? $_GET['Name'] : $_POST['Name'];
$email = (isset($_GET['Email'])) ? $_GET['Email'] : $_POST['Email'];
$phone = (isset($_GET['Phone'])) ? $_GET['Phone'] : $_POST['Phone'];
$comment = (isset($_GET['Comment'])) ? $_GET['Comment'] : $_POST['Comment'];


// Simple server side validation for POST data, of course, you should validate the email
if (!$name) {
$errors[] = 'Please enter your name.';
}
if (!$email) {
$errors[] = 'Please enter your email.';
}
if (!$comment) {
$errors[] = 'Please enter your comment.';
}
// if the errors array is empty, send the mail
if (count($errors) == 0) {

// recipient
$to = 'example@example.com';
// sender
$from = $name . ' <' . $email . '>';

// subject and the html message
$subject = 'Comment from ' . $name;
$message = '<!DOCTYPE html>
<html>
<head></head>
<body>
<table>
<tr><td>Name</td><td>' . $name . '</td></tr>
<tr><td>Email</td><td>' . $email . '</td></tr>
<tr><td>Phone</td><td>' . $phone . '</td></tr>
<tr><td>Comment</td><td>' . nl2br($comment) . '</td></tr>
</table>
</body>
</html>';


$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Reply-To: ' . $from . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$mailSuccess = mail($to, $subject, $message, $headers);

// if POST was used, display the message straight away
if ($post) {
if ($mailSuccess) {
echo 'Thank you! We have received your message.';
} else {
echo 'Sorry, unexpected error. Please try again later';
}
// else if GET was used, return the boolean value so that
// ajax script can react accordingly
// 1 means success, 0 means failed
} else {
echo (int)$mailSuccess;
}
// if the errors array has values
} else {
// display the errors message
foreach ($errors as $error) {
echo $error . '<br/>';
}
echo '<a href="contact.php">Back</a>';
}

关于PHP 邮件功能未正确设置 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20180626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com