Lord Goderick
Lord Goderick

Reputation: 985

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: [email protected]". What I want is to show a full name before that, for example:"My Name [email protected]".

I think I would have to add something within $to = "[email protected]". Any idea on how to achieve this? Thanks.

Upvotes: 1

Views: 196

Answers (2)

gmc
gmc

Reputation: 3990

Copy/paste from the docs:

$to_name = "Mary";
$to_address = "[email protected]";
// Additional headers
$headers[] = 'To: '.$to_name.' <'.$to_address.'>, Kelly <[email protected]>'; // <-- this is what you want?
$headers[] = 'From: Birthday Reminder <[email protected]>';
$headers[] = 'Cc: [email protected]';
$headers[] = 'Bcc: [email protected]';

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

Upvotes: 1

Xeoncross
Xeoncross

Reputation: 57184

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 protected]>\r\n

Upvotes: 1

Related Questions