简体   繁体   中英

How do I display the user's name before the their email address when sending PHP mail?

For example in the Gmail application, when clicking "show details" there would be

"To: whatever@gmail.com". What I want is to show a full name before that, for example:"My Name whatever@gmail.com".

I think I would have to add something within $to = "whatever@gmail.com" . Any idea on how to achieve this? Thanks.

Copy/paste from the docs:

$to_name = "Mary";
$to_address = "mary@example.com";
// Additional headers
$headers[] = 'To: '.$to_name.' <'.$to_address.'>, Kelly <kelly@example.com>'; // <-- this is what you want?
$headers[] = 'From: Birthday Reminder <birthday@example.com>';
$headers[] = 'Cc: birthdayarchive@example.com';
$headers[] = 'Bcc: birthdaycheck@example.com';

// Mail it
mail($to, $subject, $message, implode("\r\n", $headers));

If you have the IMAP extension you can use imap_rfc822_write_address() .

echo imap_rfc822_write_address("hartmut", "example.com", "Hartmut Holzgraefe");

Or you can just wing it and hope everyone uses "safe" email addresses.

echo "$name <$email>";

The actual header would be:

To: NameHere <email@example.com>\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