duandi4238 2014-06-13 01:55
浏览 46
已采纳

php mail()函数没有发送指定的'from'值

I want to send an email to a user when I change their privileges on my site, but when I specify a 'from' value for the mail() header which, as you can see in the emailing.php file, is 'My Name at MySite.net', it plays with it and comes out as 'My.Name.at.MySite.net@netsolhost.com' in the email (my host is obviously Network Solutions).

I want to make it so when the email comes up in the inbox, it first says My Name at MySite.net and the subject, but when I click on it and then reply, it sends the reply to myemail@gmail.com. I've tried a couple different headers, but nothing has worked. Maybe it has something to do with Network Solutions, but if it is a programming answer, I'd appreciate any help... and I'd still appreciate help with Network Solutions if that is the problem!

change-user-privileges.php:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <?php
            if (isLoggedIn()) {
                if ($_SESSION['privileges'] >= 5 && isset($_GET['user_id']) && isset($_GET['new_privileges'])) {
                    require_once 'connect-database.php';
                    require_once 'database-functions.php';
                    if ($_GET['new_privileges'] == detail($connection, 'privileges', 'id', $_GET['user_id'])) {
                        $update_status = 'There is no point in changing this user\'s privileges to what they already are!';
                        $email_status = 'Email was not sent.';
                    } else if ($_GET['new_privileges'] < detail($connection, 'privileges', 'id', $_GET['user_id'])) {
                        $update_status = update($connection, 'privileges', $_GET['new_privileges'], 'id', $_GET['user_id']);
                        if ($update_status) {
                            $update_status = 'User\'s privileges have been updated!';
                            /*   I HAVE THE CONTENTS OF THIS FILE BELOW THIS FILE   */
                            require_once '../../emailing/emailing.php';
                            $recipient = detail($connection, 'email', 'id', $_GET['user_id']);
                            $message = '<p>Dear '.detail($connection, 'name', 'id', $_GET['user_id']).',<br><br>I am sorry to tell you that your privileges on MySite.net have been dropped :( If you don\'t know why this has been done, you can send me an email from the <a href="http://mysite.net/email" target="_blank">MySite.net email form</a>.</p><a href="http://mysite.net/home" target="_blank" style="text-decoration: none;"><div style="background-color: '.$theme_color.'; display: inline-block; margin: 0px auto; position: relative; border-radius: 5px; border: 0px; padding: 10px; text-decoration: none; color: white;">Visit MySite.net >></div></a><p>Best,<br>My Name</p><br>';
                            $email_status = sendEmail('My Name at MySite.net', 'myemail@gmail.com', $recipient, 'Your privileges have been dropped on MySite.net', $message, true);
                            if ($email_status) {
                                $email_status = 'User was notified of their change in privileges.';
                            } else {
                                $email_status = 'There was an error notifying user of their privilege change.';
                            }
                        } else {
                            $update_status = 'There was an error trying to update user\'s privileges.';
                            $email_status = 'Email was not sent.';
                        }
                    } else {
                        $update_status = update($connection, 'privileges', $_GET['new_privileges'], 'id', $_GET['user_id']);
                        if ($update_status) {
                            $update_status = 'User\'s privileges have been updated!';
                            /*   I HAVE THE CONTENTS OF THIS FILE BELOW THIS FILE   */
                            require_once '../../emailing/emailing.php';
                            $recipient = detail($connection, 'email', 'id', $_GET['user_id']);
                            $message = '<p>Dear '.detail($connection, 'name', 'id', $_GET['user_id']).',<br><br>I am happy to tell you that your privileges on MySite.net have been raised! Discover what has been revealed to you!</p><a href="http://mysite.net/home" target="_blank" style="text-decoration: none;"><div style="background-color: '.$theme_color.'; display: inline-block; margin: 0px auto; position: relative; border-radius: 5px; border: 0px; padding: 10px; text-decoration: none; color: white;">Visit MySite.net >></div></a><p>Best,<br>My Name</p><br>';
                            $email_status = sendEmail('My Name at MySite.net', 'myemail@gmail.com', $recipient, 'Your privileges have been raised on MySite.net!', $message, true);
                            if ($email_status) {
                                $email_status = 'User was notified of their change in privileges.';
                            } else {
                                $email_status = 'There was an error notifying user of their privilege change.';
                            }
                        } else {
                            $update_status = 'There was an error trying to update user\'s privileges.';
                                $email_status = 'Email was not sent.';
                        }
                    }
                    require_once 'disconnect-database.php';
                } else {
                    header('Location: /home');
                }
            } else {
                header('Location: /login');
            }
        ?>
    <title>Change User Privileges | MySite.net</title>
</head>
    <body>
        <h1>Change User Privileges</h1>
        <div class="break"></div>
        <p>Update Status:&nbsp;<?php echo $update_status; ?></p>
        <p>Email Status:&nbsp;<?php echo $email_status; ?></p>
    </body>
</html>

Here are the contents of 'emailing.php':

<?php
    function sendEmail($sender_name, $sender_email, $mail_to, $subject, $message, $html_format) {
        // VARIABLES
        $headers = 'From: '.$sender_name."
";
        $headers .= 'Reply-To: '.$sender_email."
";
        if ($html_format === true) {
            $headers .= "MIME-Version: 1.0" . "
";
            $headers .= "Content-type:text/html;charset=UTF-8" . "
";
            /*   I'VE TRIED USING THE BELOW HEADER, INSTEAD OF THE LINE ABOVE, BUT IT ALSO DOESN'T WORK   */
            /*$headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";*/
        }
        // SEND EMAIL
        $mail_status = mail($mail_to, $subject, $message, $headers);

        return ($mail_status) ? true : false;
    }
?>

Hope this is enough information to answer the question.

  • 写回答

1条回答 默认 最新

  • dsdioa9545 2014-06-13 02:00
    关注

    Try:

    $headers = "From: $sender_name <$sender_email>
    ";
    

    Your From: header didn't contain an address, it only contained a name.

    You don't need a Reply-to: header if it's the same address as in From:.

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

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 机器学习简单问题解决
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写