Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the sender name php mail instead of [email protected]

I am trying to send email from my php :

$to = '[email protected]';
    $email_from = "[email protected]";

    $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 
[email protected]  Fri, 11:24 pm       testing sender name
[email protected]  Fri, 11:24 pm       testing sender name
like image 217
Suraj Hazarika Avatar asked Dec 03 '11 05:12

Suraj Hazarika


2 Answers

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

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

like image 120
Ben Avatar answered Oct 22 '22 06:10

Ben


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";
like image 27
Trott Avatar answered Oct 22 '22 07:10

Trott