algorithmicCoder
algorithmicCoder

Reputation: 6789

How do I set the name of an email sender via PHP

So I want the from field when an email is opened to be something like

"Jack Sparrow Via somesite" as opposed to an explicit email address.

I wondering how to set this in PHP's mail() function?

Upvotes: 19

Views: 72030

Answers (6)

Luis Cardoso
Luis Cardoso

Reputation: 21

$to = "[email protected]";
$message = "Your message here.";
$subject = "Your subject";

mail($to,$subject,$message,$header, '-f [email protected] -F "Jack Sparrow"') or die();

Just add the -f parameter to provide the sender's email and -F parameter to provide the sender's name to the mail(), all as a string.

Note: double quotes around "Jack Sparrow"

Assuming that you are using sendmail on a Linux machine:

You can find more detailed information by typing "man sendmail" in your shell.

Upvotes: 2

JWC May
JWC May

Reputation: 654

If you use PHP Mailer from GitHub, then you do it by:

$mail->SetFrom("[email protected]", "MIBC");

Upvotes: 2

vinoth
vinoth

Reputation: 111

just use like this

$headers .= 'From: [Name of the person]'.'<'. $this->from .'>'. "\n";

in Header Section.

Upvotes: 3

enderskill
enderskill

Reputation: 7674

You can accomplish this by using basic headers.

<?php
$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: Jack Sparrow <[email protected]>' . PHP_EOL .
    'Reply-To: Jack Sparrow <[email protected]>' . PHP_EOL .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

Upvotes: 52

kwelch
kwelch

Reputation: 2469

I am not sure exactly what you mean, but as you can see on PHP.net mail function.

To add the name to the from section you need to send this in the headers. From:Name<[email protected]>

Upvotes: 1

James
James

Reputation: 3805

You have to set your headers:

$to = "[email protected]";
$header = "FROM: Jack Sparrow <[email protected]>\r\n";
$message = "Your message here.";
$subject = "Your subject";

mail($to,$subject,$message,$header) or die();

Upvotes: 16

Related Questions