PHP邮件功能未正确设置 [英] PHP mail function not setting from header correctly

查看:157
本文介绍了PHP邮件功能未正确设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php邮件功能,从联系表单发送电子邮件的内容,但是当我发送这个电子邮件在我的网站上。而不是从标题输入的电子邮件地址,它是username@srv30.000webhost.com,我不知道为什么。我的php邮件功能在下面。

i have a php mail function which sends out emails with content from the contact form.but when i send this email on my website. instead of the from header being the email address entered, it is username@srv30.000webhost.com and i don't know why. my php mail function is below.

$ email是用户输入的电子邮件。

$email is the email entered by the user.

编辑:
主题:来自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:24 Nov 2013 20:41:52.0292(UTC)FILETIME = [9B09B640:01CEE955]

Subject: Comment from hello X-PHP-Script: kwp.host22.com/form_action.php for 78.147.187.64 Message-Id: <20131124204151.478EC28021@srv30.000webhost.com> Date: Sun, 24 Nov 2013 15:41:51 -0500 (EST) From: a5485011@srv30.000webhost.com Return-Path: a5485011@srv30.000webhost.com X-OriginalArrivalTime: 24 Nov 2013 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来说相当新鲜,所以任何帮助都将被忽略。
thankyou

im quite new to PHP so any help would be apreciated. thankyou

推荐答案

我相信这一行

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

应该修正为这个

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

将您的sendmail功能更改为此

And change your sendmail function to this

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;
}

这里您使用$电子邮件,但也许您应该使用$。
您的功能需要$ to,$ subject,$ message,$ from。

Here you are using $email, but maybe you should use $from. Your function takes $to, $subject, $message, $from.

此外,您应该可以更改代码

Also you should maybe change your code a bit

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。但是,随着你的电子邮件被发送,这不是问题。也许你应该重命名这个功能,所以你不要偶然地混合这些东西。

sendmail is a unix application binary used to send emails. Writing your own sendmail function in PHP doesn't do anything. The PHP mail function uses sendmail on linux by default I believe, and on windows, you need to configure SMTP. But as your emails are being sent, this is not the problem. Maybe you should rename the function, so you don't mix these things by accident.

同样这里是整个代码重写,也许你可以学习一两件事:)

Also here is whole code rewritten, maybe you can learn a thing or two :)

$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邮件功能未正确设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆