简体   繁体   中英

Change sender's name PHP email?

Okay,

So I have email notifications to my users setup but they receive it saying it's from notifications@domain.com. Instead I want it to appear in their inbox as 'Name/Title' rather than the email address.

Here's what I've got so far:

$settings['notification'] = 'Notifications <notifications@domain.com>';

function nemail($to,$subject,$tpl,$var=array())
{
global $settings;
extract($var, EXTR_OVERWRITE);
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=utf-8" . "\r\n";
$headers .= 'From: '. trim($settings['notification']) . "\r\n" .
    'Reply-To: '. trim($settings['notification']) . "\r\n";
ob_start();
ob_implicit_flush(false);
require(realpath("themes/".$settings['theme']."/template/email_".$tpl.".php"));
$message = ob_get_clean();
@mail($to, $subject, $message, $headers);
}

As RTB says in his comment, you should look at using PHPMailer for emailing.

But, I think the simple answer to your question would be to set the Sender header as well as the From - the RFC says you "should not" if the From header contains a single email, but it is not forbidden.

http://tools.ietf.org/html/rfc2822#section-3.6.2

你可以这样试试(来自http://php.net/manual/en/function.mail.php #Example 4)

$headers .= 'From: Blah Blah Blah <'.trim($settings['notification']) . ">' . "\r\n";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM