我有一个php邮件功能,可以发送包含联系表单内容的电子邮件。但是当我在我的网站上发送这封电子邮件时。而不是从头部输入的电子邮件地址,它是username@srv30.000webhost.com,我不知道为什么。我的php邮件功能如下。
$ email是用户输入的电子邮件。
编辑: 主题:来自你好的评论 X-PHP-Script:kwp.host22.com/form_action.php for 78.147.187.64 消息标识:< 20131124204151.478EC28021@srv30.000webhost.com> 日期:太阳,2013年11月24日15:41:51 -0500(EST) 来自:a5485011@srv30.000webhost.com 返回路径:a5485011@srv30.000webhost.com X-OriginalArrival时间: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很新,所以任何帮助都会被贬低。 三江源
答案 0 :(得分:5)
我相信这一行
$result = @mail($to, $subject, $message, $from);
应该修复此
$result = sendmail($to, $subject, $message, $from);
将您的sendmail功能更改为此
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,$。来自。
你也应该稍微修改你的代码
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>';
}