Search code examples
phpphpmailer

Trim email address from header of PHP Mailer


I am using PHPMailer to send email to my clients. But I am not satisfied with the output of the

enter image description here

I want to remove <[email protected]> at the end of my name but not sure how to do it.

My current script:

$mail->SetFrom("[email protected]",'Michael Chu');
$mail->XMailer = 'Microsoft Mailer';
$mail->AddAddress($email);
$mail->Subject = "TEST Email";
$mail->Body = "<p>TEST Email<p>";

Solution

  • The relevant parts in the PHPMailer code is in the Pre Send routine, which assembles the mail (and which is obviously called internally always before 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;
    

    This will be called always. Now: when we look at the createHeader-function we see this:

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

    So: Create Header always adds the From Address part, but it relies on addrAppend to format it (passing 'From' and an array containing one address-array [email, name])

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

    The address-array is passed on:

    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])
             . '>';
    }
    

    and formatted with the email... Nothing you can do about it.

    So with phpmailer you can't do it. But you can write your own subclass. Probably something along those lines

    <?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]);
        }
    }   
    }