简体   繁体   English

如何在简单的php邮件脚本中仅显示名称而不是发件人的电子邮件地址

[英]How to show only the name instead of the email address of the sender in a simple php mail script

I have a simple php mail script which will send emails. 我有一个简单的PHP邮件脚本,它将发送电子邮件。

The problem is while sending it is displaying sendername <senderemail@domain.com> 问题是发送时显示sendername <senderemail@domain.com>

I have given in the code as $headers = "From: $name <$senderEmail>"; 我在代码中给出了$headers = "From: $name <$senderEmail>";

I want to display only name and not email address of the sender. 我只想显示发件人的姓名,而不显示电子邮件地址。

How do I do that? 我怎么做?

Here is the program in its entirety. 这是程序的整体。

<?php

if($_POST["submit"]) {
    $recipient=$_POST["senderemail"];
    $subject=$_POST["subject"];
    $name=$_POST["name"];
    $senderEmail=$_POST["email"];
    $city=$_POST["city"];
    $headers = "From: $name <$senderEmail>";
    $mailBody="$city\n";
    $process=explode(",",$recipient);
    reset($process);

    foreach ($process as $to) {
        mail($to,$subject, $mailBody,$headers);
    }

    $thankYou="<p>Thank you! Your Message has been sent.</p>";
    echo 'Thank you! Your Message has been sent.';
}
?>

Your generated message already seems to contain the user's full name in the From: header (assuming $name is the human-readable name). 您生成的消息似乎已经在From:标头中包含用户的全名(假设$name是人类可读的名称)。 How this is displayed by any individual user agent is no longer in your hands. 任何单个用户代理如何显示此信息都不再由您掌握。 Very often, it will display both parts regardless; 通常,它将显示两个部分,无论如何; or only shorten to the human-readable name if the sender's email address is in the recipient's address book. 或仅在收件人的通讯录中包含发件人的电子邮件地址时缩写为人类可读的名称。 (Thunderbird takes this a step further and displays the information from the address book rather than the actual information in the message!) (Thunderbird更进一步,并显示通讯录中的信息而不是消息中的实际信息!)

One common problem is if the generated message has a different Sender: header; 一个常见的问题是,生成的消息是否具有不同的Sender:标头; often, this will cause the recipient's user agent to display more than just the usual full name. 通常,这将导致收件人的用户代理显示的不只是通常的全名。 Your question doesn't mention this, nor does it show one of the generated messages; 您的问题没有提及这一点,也没有显示生成的消息之一。 but perhaps this will help you find a solution. 但这也许可以帮助您找到解决方案。

A From header containing both name and address should be formatted like this: 包含名称和地址的From标头应采用以下格式:

From: User Name <user@example.com>

If you don't provide a name, it will display the email address. 如果您不提供姓名,它将显示电子邮件地址。

PHPMailer has built-in support for creating this header correctly for you via the From and FromName properties, and the setFrom method (it doesn't matter which you use): PHPMailer具有内置支持,可通过FromFromName属性以及setFrom方法(无论使用setFrom方法)为您正确创建此标头:

$mail->setFrom('user@example/com', 'User Name');

or 要么

$mail->From = 'user@example/com';
$mail->FromName = 'User Name';

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

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