When specifying an email address that contains a display name the from() method in the Email class ignores that display name.
The following example code illustrates my point:
The recipient then receives an email that looks something like this:
My work around is to parse the email address and look for a display name in the event that one is not provided, as follows:
I have changed to:
The resulting message then correctly reflects the sender's email address and display name, as follows:
I hope this helps someone as the administrators don't feel that it is useful enough to be incorporated in the Email class.
Please Note
I am aware that there is an additional parameter in the CI_Email::from() method that allows you to specify a display name but then you would have to parse and interrogate the email address before invoking the method in order to separate the email address and the display name which you must then submit as two separate parameters.
This would be a lot of unnecessary coding on your part and my solution handles this for you as part of the class method.
The following example code illustrates my point:
PHP Code:
$email_address = "Name of Sender <email@sender.com>";
$this->load->library('email');
$this->email->from($email_address);
$this->email->to('someone@example.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
The recipient then receives an email that looks something like this:
Code:
From: email@sender.com
Sent: 18 October 2017 10:08 AM
To: someone@example.com
Subject: Email Test
Testing the email class.My work around is to parse the email address and look for a display name in the event that one is not provided, as follows:
PHP Code:
// Line 483 - 486 in the Email class:
if (preg_match('/\<(.*)\>/', $from, $match))
{
$from = $match[1];
}
I have changed to:
PHP Code:
if (preg_match('/\<(.*)\>/', $from, $match))
{
$from = $match[1];
if (empty($name))
{
$name = trim(str_ireplace('<' . $match[1] . '>', '', $from));
}
}
The resulting message then correctly reflects the sender's email address and display name, as follows:
Code:
From: Name of Sender <email@sender.com>
Sent: 18 October 2017 10:08 AM
To: someone@example.com
Subject: Email Test
Testing the email class.I hope this helps someone as the administrators don't feel that it is useful enough to be incorporated in the Email class.
Please Note
I am aware that there is an additional parameter in the CI_Email::from() method that allows you to specify a display name but then you would have to parse and interrogate the email address before invoking the method in order to separate the email address and the display name which you must then submit as two separate parameters.
This would be a lot of unnecessary coding on your part and my solution handles this for you as part of the class method.