/    Sign up×
Community /Pin to ProfileBookmark

setting a specific From header in the mail function

Hello

I am using the mail() function to send email.
I would like to set the mail headers such that when I will receive an email from someone, the From field will look like:
From: John Smith
where Jogn Smith would be the name of the person (available to me when I call the mail function.

However, when I reply to the mail, the reply will be sent to John Smith’s mail address.

Here is my code:

[code=php]
$name = ‘John Smith’
// $email holds the email address of John Smith
$header = “From: ” .$email;

$message = “Hello, this is a test”;

mail(‘[email protected]’, ‘this is the subject’, $message, $header);
[/code]

My question is this:
How can I use the $name variable with the mail headers to acheive what I want.

regards
Jason

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@sideshowJul 28.2004 — [code=php]
<?php
$name = 'John Smith';
// $email holds the email address of John Smith
$header = "From: " . $name . "<".$email.">";

$message = "Hello, this is a test";

mail('[email protected]', 'this is the subject', $message, $header);
?>
[/code]
Copy linkTweet thisAlerts:
@jasongrauthorJul 28.2004 — thank you, I got it to work
×

Success!

Help @jasongr spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...