修剪PHP Mailer标题中的电子邮件地址

时间:2018-06-12 20:08:06

标签: php phpmailer

我正在使用PHPMailer向我的客户发送电子邮件。但我对

的输出不满意

enter image description here

我想删除姓名末尾的<michael@gmail.com>,但不知道该怎么做。

我目前的剧本:

$mail->SetFrom("michael@gmail.com",'Michael Chu');
$mail->XMailer = 'Microsoft Mailer';
$mail->AddAddress($email);
$mail->Subject = "TEST Email";
$mail->Body = "<p>TEST Email<p>";

1 个答案:

答案 0 :(得分:1)

PHPMailer代码中的相关部分位于Pre Send例程中,该例程汇编邮件(显然在发送之前内部调用它):

public function preSend() {
    ...
    try {
        $this->error_count = 0; // Reset errors
        $this->mailHeader = '';
    ...         
        // Create body before headers in case body makes changes to headers (e.g. altering transfer encoding)
        $this->MIMEHeader = '';
        $this->MIMEBody = $this->createBody();
        // createBody may have added some headers, so retain them
        $tempheaders = $this->MIMEHeader;
        $this->MIMEHeader = $this->createHeader();
        $this->MIMEHeader .= $tempheaders;
    ...         
    return true;

这将永远被调用。现在:当我们查看createHeader函数时,我们会看到:

public function createHeader()
{
    $result = '';
    ...
    $result .= $this->addrAppend('From', [[trim($this->From), $this->FromName]]);
    ...
    return $result;
}

所以:Create Header总是添加From Address部分,但它依赖addrAppend来格式化它(传递&#39; From&#39;以及包含一个地址数组[email,name]的数组)

public function addrAppend($type, $addr)
{
    $addresses = [];
    foreach ($addr as $address) {
        $addresses[] = $this->addrFormat($address);
    }
    return $type . ': ' . implode(', ', $addresses) . static::$LE;
}

传递地址数组:

public function addrFormat($addr)
{
    if (empty($addr[1])) { // No name provided
        return $this->secureHeader($addr[0]);
    }
    return $this->encodeHeader($this->secureHeader($addr[1]), 'phrase') .
         ' <' . 
         $this->secureHeader($addr[0])
         . '>';
}

并使用电子邮件格式化......您无能为力。

因此,使用phpmailer,您无法做到。但是你可以写自己的subclass。 可能是沿着这些方向发展的东西

<?php

//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '../vendor/autoload.php';
/**
 * Use PHPMailer as a base class and extend it
 */
class myPHPMailer extends PHPMailer
{

public function addrFormat($addr)
{
    if (empty($addr[1])) { // No name provided
            return $this->secureHeader($addr[0]);
        }
    else {          
            return $this->secureHeader($addr[1]);
    }
}   
}