30

I am trying to send email from my php :

$to = 'it@7sisters.in';
    $email_from = "info@7sisters.in";

    $full_name = 'Suraj Hazarika';
    $from_mail = $full_name.'<'.$email_from.'>';



    $subject = "testing sender name";
    $message = "";
    $message .= '
            <p><strong>This is only a test . Please do not reply.</strong><br />
    ';
    $from = $from_mail;

    $headers = "" .
               "Reply-To:" . $from . "\r\n" .
               "X-Mailer: PHP/" . phpversion();
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";        
    mail($to,$subject,$message,$headers);

I am following the tutorial PHP E-mail Form Sender Name Instead Of E-mail? but I am still getting the emails with sender name with hostname.

     From                              Date             Subject 
sisters@rsx02.justhost.com  Fri, 11:24 pm       testing sender name
sisters@rsx02.justhost.com  Fri, 11:24 pm       testing sender name
Community
  • 1
  • 1
Suraj Hazarika
  • 647
  • 3
  • 9
  • 23
  • You might want to look into header injections attacks. Your code isn't directly vulnerable but be sure to look out for it. – tangrs Dec 03 '11 at 05:35

6 Answers6

30

I came to this question looking for something different but with similar words, so here's how to do what I was looking for:

In the from header, put the address in angle brackets, and the "sender name" outside of it.

from: Don Draper <don.draper@website.com>\n

I wanted the inbox to say Don Draper instead of don.draper and that's how to do it.

Ben
  • 54,723
  • 49
  • 178
  • 224
29

You only use $from in your Reply-To header. If you want it to be in the From header, you need to set it in the From header.

Put something like this before your mail() command:

$headers .= 'From: ' . $from . "\r\n";
Trott
  • 66,479
  • 23
  • 173
  • 212
  • 1
    It's worth noting that the `From` address that is being set here is not the same as the "envelope sender" address. In order to prevent emails not being delivered it's worth setting both. – jlh Dec 22 '19 at 11:04
12

Add this line before header line

$headers .= "From: Your Name <sitename@hostname.com> \r\n";

So the code will be

$headers = "";
$headers .= "From: WIFI Metropolis <sitename@hostname.com> \r\n";
$headers .= "Reply-To:" . $from . "\r\n" ."X-Mailer: PHP/" . phpversion();
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";        
mail($to,$subject,$message,$headers);
Ahmed Bermawy
  • 2,290
  • 4
  • 35
  • 42
8

You can also try adding the From address to the mail's 'envelope' by utilizing the fifth parameter in PHP's mail() function:

mail($to_mail, $subject, $message, $headers, "-f$from_email");

Reference here.

Reference regarding mail headers and envelopes here

Note that some server setups may disallow this or throw a warning, depending on their configuration and/or the configuration of the mail transfer agent. This answer talks about updating sendmail's 'trusted users' with regards to this.

Community
  • 1
  • 1
indextwo
  • 5,535
  • 5
  • 48
  • 63
  • It's not forcing anything, the `From:` header sets the MIME sender, whereas `-f` additional parameter sets the envelope sender. This is disallowed in safe mode. – LeonardChallis Jun 13 '14 at 20:17
  • @LeonardChallis Quite right; I didn't fully understand the difference between headers and envelopes. I've updated my answer with a (hopefully) better, clearer explanation and references. – indextwo Jun 16 '14 at 12:12
  • 1
    OMG. I've been struggling for years with emails going to spam because the domain in the from header had proper DKIM/SPF records but the envelope domain was still default and did not. You sir, just changed my life. Why more tutorials don't bring this up is beyond me. – Nosajimiki Oct 10 '22 at 17:25
1

Simply add the From header. Something like this.

$headers .= "From: website@mydomainname.com";
Ben
  • 1,525
  • 11
  • 19
1

Try to add From: to the header

$headers = "" .
           "Reply-To:" . $from . "\r\n" .
           "From:" . $from . "\r\n" .
           "X-Mailer: PHP/" . phpversion();
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  

Some MTA can change/ignore this definition to avoid SPAM, and override with user of SMTP configured in PHP.ini

Paulo H.
  • 1,228
  • 10
  • 15