簡體   English   中英

php mail()函數未發送指定的“發件人”值

[英]php mail() function not sending specified 'from' value

當我更改用戶在我網站上的權限時,我想向用戶發送電子郵件,但是當我為mail() header指定一個'from'值時,如在emailing.php文件中可以看到的,就是'My Name at MySite.net' ,它會一起使用,並在電子郵件中顯示為'My.Name.at.MySite.net@netsolhost.com' (我的主機顯然是Network Solutions)。

My Name at MySite.net ,以便當收件箱中出現電子郵件時,它首先My Name at MySite.net和主題上說“ My Name at MySite.net ”,但是當我單擊它然后答復時,它將答復發送到myemail@gmail.com 我嘗試了幾種不同的標題,但沒有任何效果。 也許與網絡解決方案有關,但如果是編程的答案,我將不勝感激...如果這是問題,我仍將感謝網絡解決方案的幫助!

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>

這是'emailing.php'的內容:

<?php
    function sendEmail($sender_name, $sender_email, $mail_to, $subject, $message, $html_format) {
        // VARIABLES
        $headers = 'From: '.$sender_name."\r\n";
        $headers .= 'Reply-To: '.$sender_email."\r\n";
        if ($html_format === true) {
            $headers .= "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
            /*   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' . "\r\n";*/
        }
        // SEND EMAIL
        $mail_status = mail($mail_to, $subject, $message, $headers);

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

希望這是足夠的信息來回答這個問題。

嘗試:

$headers = "From: $sender_name <$sender_email>\r\n";

您的From:標頭不包含地址,僅包含名稱。

如果與From:地址相同,則不需要Reply-to:標頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM