我有一个php邮件功能,可以发送包含联系表单中内容的电子邮件。但是当我在我的网站上发送此电子邮件时。而不是from标头是输入的电子邮件地址,它是username@srv30.000webhost.com,我不知道为什么。我的PHP邮件功能如下。
$ email是用户输入的电子邮件。
编辑:主题:来自hello X-PHP脚本的评论:kwp.host22.com/form_action.php for 78.147.187.64 Message-Id:<20131124204151.478EC28021@srv30.000webhost.com>日期:2013年11月24日,星期日15:41 :51 -0500(EST) 发件人:a5485011@srv30.000webhost.com 返回路径:a5485011@srv30.000webhost.com X-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;}
Run Code Online (Sandbox Code Playgroud)
我对PHP还是很陌生,所以对您的帮助将不屑一顾。谢谢
我相信这条线
$result = @mail($to, $subject, $message, $from);
Run Code Online (Sandbox Code Playgroud)
应该固定为此
$result = sendmail($to, $subject, $message, $from);
Run Code Online (Sandbox Code Playgroud)
并将您的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;
}
Run Code Online (Sandbox Code Playgroud)
在这里,您使用的是$ 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;
}
Run Code Online (Sandbox Code Playgroud)
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>';
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10293 次 |
| 最近记录: |