Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPMailer Change the senders name

Tags:

php

phpmailer

I'm having trouble setting the name that's sent with mailings sent using the PHPMailer class.

I've written the following function so that it can be used in a similar way the php's bulit in mail() function.

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 = "[email protected]";
    $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;
    }
}

When testing with something like so;

pmail("[email protected]", "test email", "test message here");

Everything works fine, the from address shows up as [email protected] in the headers as expected, however the name that I see in the inbox of the recipient is not Web Mailer its the default account associated with the user whos credentials are used to send the email. In the headers the from name does show up as Web Mailer, but its the inbox where I want to see it

We are unable to set up more user accounts on our system to allow us to just make a new one with the desired name and email, therefore we have to send it via an existing user account. In this case mine, and emails get sent with my name attached, but we want the name to show up as Web Mailer.

Is this even possible?

like image 218
Novocaine Avatar asked Jan 07 '13 14:01

Novocaine


2 Answers

If you use PHP Mailer API of GitHub, you can use this to set the Sender Name:

$mail->SetFrom("$youremail ", "Your Name");
like image 79
JWC May Avatar answered Oct 21 '22 08:10

JWC May


Turns out my function does work as intended. Because I was receiving emails from this system to internal addresses, they all automatically had me in their address book, therefore as the address used to send out the emails was associated with my personal address, they saw my name instead of "Web Mailer".

When testing with an external email account the senders name is listed correctly.

like image 38
Novocaine Avatar answered Oct 21 '22 08:10

Novocaine