[英]php mail() function not sending specified 'from' value
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). 当我更改用户在我网站上的权限时,我想向用户发送电子邮件,但是当我为mail()
header
指定一个'from'
值时,如在emailing.php
文件中可以看到的,就是'My Name at MySite.net'
,它会一起使用,并在电子邮件中显示为'My.Name.at.MySite.net@netsolhost.com'
(我的主机显然是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
. 我My Name at MySite.net
,以便当收件箱中出现电子邮件时,它首先My Name at MySite.net
和主题上说“ My Name at MySite.net
”,但是当我单击它然后答复时,它将答复发送到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: 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: <?php echo $update_status; ?></p>
<p>Email Status: <?php echo $email_status; ?></p>
</body>
</html>
Here are the contents of 'emailing.php': 这是'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;
}
?>
Hope this is enough information to answer the question. 希望这是足够的信息来回答这个问题。
Try: 尝试:
$headers = "From: $sender_name <$sender_email>\r\n";
Your From:
header didn't contain an address, it only contained a name. 您的From:
标头不包含地址,仅包含名称。
You don't need a Reply-to:
header if it's the same address as in From:
. 如果与From:
地址相同,则不需要Reply-to:
标头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.