dongpobo6009 2013-11-24 12:33
浏览 31
已采纳

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 is the email entered by the user.

EDIT: 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]

full php script:

$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" . "
";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "
";
$headers .= 'From: ' . $email . "
";
$headers .= 'Reply-To: ' .$email . "
";
$headers .= 'X-Mailer: PHP/' . phpversion();
@mail($to, $subject, $message, $headers); 

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

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

展开全部

  • 写回答

1条回答 默认 最新

  • douhe4608 2013-11-24 13:22
    关注

    I believe this line

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

    Should be fixed to this

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

    And change your sendmail function to this

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

    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" . "
    ";
        $headers .= "Content-type:text/html;charset=iso-8859-1" . "
    ";
        $headers .= 'From: ' . $from . "
    ";
        $headers .= 'Reply-To: ' .$from . "
    ";
        $headers .= 'X-Mailer: PHP/' . phpversion();
        if(mail($to, $subject, $message, $headers)){
            return 1;
        } 
        return 0;
    }
    

    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" . "
    ";
        $headers .= "Content-type:text/html;charset=iso-8859-1" . "
    ";
        $headers .= 'From: ' . $from . "
    ";
        $headers .= 'Reply-To: ' . $from . "
    ";
        $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>';
    }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部