5

我无法设置使用 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

4 回答 4

9

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

$mail->SetFrom("$youremail ", "Your Name");
于 2017-05-11T19:55:26.980 回答
5

原来我的功能确实按预期工作。因为我从这个系统接收到内部地址的电子邮件,他们都自动将我放在他们的地址簿中,因此由于用于发送电子邮件的地址与我的个人地址相关联,他们看到我的名字而不是“Web Mailer”。

使用外部电子邮件帐户进行测试时,发件人姓名会正确列出。

于 2013-01-08T14:31:44.360 回答
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);
于 2013-01-07T14:42:16.410 回答
-1

你可以在你的mail.php中设置

$headers = "From: Your Name <yourname@example.com>";
于 2018-09-06T15:04:45.877 回答