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

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?

You can accomplish this by using basic headers.
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: Jack Sparrow <jsparrow#blackpearl.com>' . PHP_EOL .
'Reply-To: Jack Sparrow <jsparrow#blackpearl.com>' . PHP_EOL .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>

You have to set your headers:
$to = "Someone#email.com";
$header = "FROM: Jack Sparrow <some#site.com>\r\n";
$message = "Your message here.";
$subject = "Your subject";
mail($to,$subject,$message,$header) or die();

just use like this
$headers .= 'From: [Name of the person]'.'<'. $this->from .'>'. "\n";
in Header Section.

If you use PHP Mailer from GitHub, then you do it by:
$mail->SetFrom("info#mibckerala.org", "MIBC");

$to = "Someone#email.com";
$message = "Your message here.";
$subject = "Your subject";
mail($to,$subject,$message,$header, '-f some#site.com -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.

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#example.com>

Related

PHP Email Function not working when to and from email is same

When sent to and send from email address same the php mail send function is not working but when i change to and from email address different then it works.
here is my sample code where now here i used exampleemail#example.com as demo email address.
can anybody face same problems? if yes please help me to sort out this issues.
<?php
echo "<h1> Simple Email Test Page</h1>";
$to = 'exampleemail#example.com';
$subject = 'subject';
$message = 'Demo Message';
$headers = 'From: exampleemail#example.com' . "\r\n" .
'Reply-To: exampleemail#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
echo "Mail Sent";
?>
No, there is no such issue where TO and FROM should not be same.
Well, I have tried your code on my hosting account and works fine(Got a mail by the same mail ID).
Check with your hosting provider.
You can Validate like this,
$retval = mail($to, $subject, $message, $headers);
if( $retval == true )
{
echo "Message sent successfully...";
}
else
{
echo "Message could not be sent...";
}

php email script doesn't work

I have this script:
<?php
$to1 = $_GET["number"];
$to2 = $_GET["at"];
$subject = $_GET["sub"];
$message = $_GET["message"];
$from = "admin#chipperyman573.com";
$headers = "From:" . $from;
$fin = $to1+"#"+$to2;
mail($to,$subject,$message,$headers);
?>
I go to this url:
http://chipperyman573.com/send.php?to1=whatsittoya573&to2=gmail.com&subject=Test&message=Test2
however I get no email in my gmail inbox. admin#chipperyman573.com does exist.
In the link, you say subject=Test, but you use it like $subject = $_GET["sub"];?
Try this: $subject = $_GET["subject"];
Solved over chat, transcript:
You're passing an invalid variable $to to mail
mail($to,$subject,$message,$headers);
should be...
mail($fin,$subject,$message,$headers);
You define $fin but never use it. You use $to but never declare it.
This might have something to do with it not working ;)
Your code should be like this...
<?php
$to1 = $_GET["number"];
$to2 = $_GET["at"];
$subject = $_GET["sub"];
$message = $_GET["message"];
$from = "admin#chipperyman573.com";
$headers = "From:" . $from;
$fin = $to1."#".$to2;
mail($fin,$subject,$message,$headers);
?>

Change the Return-Path in PHP mail function

Is it possible to change the Return-Path value in emails are sending via mail() function of PHP ?
It's value is 'www-data#mydomain.com' in emails I send in my site and it causes some problems on email delivery failed process. I want to set it to my email address.
Here's the code I have tried:
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= "Content-type: text/html; charset=utf-8" . "\n";
$headers .= "Return-Path: <adminemail#yahoo.com>"."\n";
$headers .= "Errors-To: <adminemail#yahoo.com>"."\n";
// Additional headers
$headers .= "To: email1#yahoo.com <adminemail#yahoo.com>" . "\n";
$headers .= "From: adminemail#yahoo.com <adminemail#yahoo.com>";
// Mail it
mail('email1#yahoo.com', 'test', 'salam', $headers, "f");
You can set reply to & return path into headers as below
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'Return-Path: webmaster#example.com'
OR
as the fifth parameter to adjust the return path
mail($to, $subject, $message, $headers, "-f email#wherever.com");
where email#wherever.com should be replaced by your mail.
The issue is mail format requires headers to use \r\n line endings... not \n, the trick with that is some servers will accept both (convert them for you and it seems to work magically) while others will consider those without \r\n endings to be invalid and basically ignore all your headers. So try instead:
$headers = "MIME-Version: 1.0\r\n".
"Content-type: text/html; charset=utf-8\r\n".
"Return-Path: adminemail#yahoo.com";
mail ("email1#yahoo.com","test","salam",$headers);
By the way, return-path expects an RFC1123 mailbox (without the angle brackets, just the email address) ... not sure why you'd want to set return-path as in your example since it's the same as the from address (so superfluous)
Just define Return-Path
$returnPath = "-fadminemail#yahoo.com"; //-f will do the job
mail('email1#yahoo.com', 'test', 'salam', $headers, $returnPath);

Why my html code also display when i send email?

hi dear this is my code to send email
my message code is this
$to = $email;
$subject = "Activation";
$message = "Your activation key is this " .$key.'<br>'.' click here to activate your acount. here';
$from = "riaz_qadeer90#yahoo.com";
$headers = "From:" . $from;
if(mail($to,$subject,$message,$headers))
{
echo "Check your email to activate your acount.";
}
The problem is this when i send email the whole message shown in my inbox with code. why it not show "Click" as an anchore.....
Mail isn't HTML format by default, so it's sending this as plain text. See Example #4 on the PHP page for mail() for sending HTML email. You need to specify the content type for the message headers:
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Note the .= operator being used here. You'd be appending this to your existing $headers value. So you'll also want to make sure your existing header is terminated properly:
$headers = "From:" . $from . "\r\n";
You have to check following things:-
1>message content properly it has enclosed with which inverted comma,either ' or ".
2>check your header and additional header information properly concatenate with (.) operator or not.
Most of the time problem arrives in second case.
$headers .= 'To: abc, bcd' . "\r\n";
$headers .= 'From:xyz ' . "\r\n";
Because you must define the header of your e-mail as HTML.
http://www.w3schools.com/php/func_mail_mail.asp

mail header issue in sendmail in CentOs 6

I have setup centOs 6 on rackspace server, and installed Apache PHP & other modules.
I also installed sendmail to use mail() function from PHP, it working, but i am not able to set my own header in mail().
$to = "myemail#gmail.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$from = " Team <my#odomain.com>";
$headers = "From: $from\r\n";
$headers = "MIME-Version: 1.0\n" ;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
But i getting spam emails with header "Apache apache#server". Header is not setting.
I also tried "-f emailaddress", but not working.
What should i do? I had also try some sendmail configuration but still not solved.
Ritesh
Replace \r\n with \n in the line $headers = "From: $from\r\n";