Change the sender name php mail instead of sitename@hostname.com

岁酱吖の 提交于 2019-11-26 20:23:55

问题


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

回答1:


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";



回答2:


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.




回答3:


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.




回答4:


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);



回答5:


Simply add the From header. Something like this.

$headers .= "From: website@mydomainname.com";



回答6:


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



来源:https://stackoverflow.com/questions/8365754/change-the-sender-name-php-mail-instead-of-sitenamehostname-com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!