简体   繁体   English

更改发件人名称php邮件,而不是sitename@hostname.com

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

I am trying to send email from my php : 我正在尝试从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? 我正在遵循教程PHP电子邮件表单发件人姓名而不是电子邮件? 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

You only use $from in your Reply-To header. 您只能在Reply-To标头中使用$from If you want it to be in the From header, you need to set it in the From header. 如果希望它位于From头中,则需要在From头中进行设置。

Put something like this before your mail() command: 在您的mail()命令之前放置以下内容:

$headers .= 'From: ' . $from . "\r\n";

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标题中,将地址放在尖括号中,并将“发送方名称”放在它的外面。

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. 我想让收件箱说Don Draper而不是don.draper ,这就是这样做的方法。

You can also try adding the From address to the mail's 'envelope' by utilizing the fifth parameter in PHP's mail() function: 您还可以尝试利用PHP的mail()函数中的第五个参数,将From地址添加到邮件的“信封”中:

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. 这个答案讨论有关更新sendmail的“受信任的用户”。

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

Simply add the From header. 只需添加From头。 Something like this. 这样的事情。

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

Try to add From: to the header 尝试将From:添加到标题

$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 一些MTA可以更改/忽略此定义以避免SPAM,并用PHP.ini中配置的SMTP用户覆盖

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM