PHPMailer更改发件人名称

7
我在使用PHPMailer类发送邮件时,设置邮件名称遇到了一些问题。
我编写了以下函数,以便可以像php内置的mail()函数一样使用。
function pmail($to, $subject, $message, $headers = "", $attachments = "")
{
    date_default_timezone_set('Europe/London');

    require_once($_SERVER['DOCUMENT_ROOT']."/lib/inc/class.phpmailer.php");
    //include($_SERVER['DOCUMENT_ROOT']."/lib/inc/class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

    $defaultEmail = "reply@example.com";
    $defaultEmailName = "Web Mailer";

    $mail = new PHPMailer();

    $mail->IsSMTP();                                // telling the class to use SMTP
    $mail->Host       = "mail.example.com";         // SMTP server
    $mail->SMTPDebug  = false;                      // enables SMTP debug information (for testing, 1 = errors and messages, 2 = messages only, false = off)
    $mail->SMTPAuth   = true;                       // enable SMTP authentication
    //$mail->SMTPSecure = "tls";                    // sets the prefix to the servier
    $mail->Host       = "mail.example.com";         // sets the SMTP server
    $mail->Port       = 25;                         // set the SMTP port for the GMAIL server
    $mail->Username   = "###";                      // SMTP account username
    $mail->Password   = "###";                      // SMTP account password
    $mail->SetFrom( ($headers['fromEmail'] != "" ? $headers['fromEmail'] : $defaultEmail), ($headers['fromName'] != "" ? $headers['fromName'] : $defaultEmailName) );
    $mail->AddReplyTo( ($headers['replyToEmail'] != "" ? $headers['replyToEmail'] : $defaultEmail), ($headers['replyToName'] != "" ? $headers['replyToName'] : $defaultEmailName) );
    $mail->AddAddress($to);
    $mail->Subject = $subject;
    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    $mail->MsgHTML($message);

    foreach($attachments as $attachment) {
        //$mail->AddAttachment("images/phpmailer.gif");      // attachment example
        $mail->AddAttachment($attachment);
    }

    if(!$mail->Send()) {
        //echo "Mailer Error: ".$mail->ErrorInfo;
        return false;
    } else {
        //echo "Message sent!";
        return true;
    }
}

当使用以下方式进行测试时;

pmail("test@test.com", "test email", "test message here");

一切正常,发件人地址如预期一样显示为reply@example.com,然而,收件人收到邮件后在收件箱中看到的名称不是Web Mailer,而是发送邮件所使用用户账户的默认名称。 在邮件头中,发件人名称确实显示为Web Mailer,但我希望在收件箱中看到这个名称。

我们无法在系统上设置更多的用户账户以便创建一个具有所需名称和电子邮件的新用户账户,因此我们必须通过现有用户账户发送邮件。 在这种情况下,邮件会带上我的名称,但我们希望将其显示为Web Mailer。

这是否可能?

4个回答

11

如果您使用 GitHub 的 PHP Mailer API,您可以使用以下方法设置发件人名称:

$mail->SetFrom("$youremail ", "Your Name");

我不确定这是否是原帖作者想要的,但这正是我所需要的,所以谢谢! :) - Sean the Bean

5
原来我的函数确实按照预期工作了。因为我收到来自该系统发送到内部地址的电子邮件,它们都自动将我加入了它们的通讯录中,因此由于用于发送电子邮件的地址与我的个人地址相关联,他们看到了我的名字而不是“Web Mailer”。
在使用外部电子邮件帐户进行测试时,发件人名称显示正确。

哈哈,我也遇到过这种情况。当我使用外部电子邮件地址时,一切都正常。 - Maulik Vora
1
@MaulikVora 烦人吧?我在这上面浪费了很多时间。 - Novocaine
是的 @Novocaine 但你在这里为我节省了时间。;-) - Maulik Vora

2
我曾经使用过这段PHP代码,它可以正常工作:(它会显示下面的内容,而不是我的Gmail凭据)
$headers = "From: Cartrader UK <noreply@cartrader.co.uk";

. . .

if (mail($to, $subject, $msg, $headers)) {
echo "Mail sent successfully";
} else {
echo "There was some problem sending the E-Mail";
}

也许这能对您有所帮助。
编辑: 我会尝试更改使用 SetFrom 的行:
$mail->SetFrom($defaultEmail,$defaultEmailName);

这可以使用内置的PHP mail()方法来实现,但我正在使用PHPMailer类来发送电子邮件,不幸的是,它与原来的方法有些不同。 - Novocaine
好的,我明白了。不过还有一件事,你真的需要那个三元运算符吗?如果你想要显示的名称是“Web Mailer”,你可以直接赋值吗? - JanT
不,我现在不需要三元运算符,但是将来的开发中可能会有一些用途(多个不同域名的网站需要不同的电子邮件地址),这就是为什么我这样实现它的原因。 - Novocaine
好的,我能想到的最后一件事是您使用的phpmailer版本旧于5.0。是这种情况吗?如果不是,我不知道问题可能在哪里。 - JanT
我正在使用phpmailer的v5.2.2版本,所以我想那里应该没有问题。 - Novocaine
显示剩余2条评论

-1

你可以在mail.php中进行设置。

$headers = "From: Your Name <yourname@example.com>";

1
这与PHPMailer类无关。您提供的是标准php电子邮件,而这个问题不是关于它的。 - Novocaine

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接