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?
If you use PHP Mailer API of GitHub, you can use this to set the Sender Name:
$mail->SetFrom("$youremail ", "Your Name");
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With