Welcome Guest, Not a member yet? Register   Sign In
CI_Email::from() ignores the display name in the email address
#1

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:

PHP Code:
$email_address "Name of Sender <[email protected]>";

$this->load->library('email');

$this->email->from($email_address);
$this->email->to('[email protected]');
$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 protected]
Sent: 18 October 2017 10:08 AM
To: [email protected]
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 protected]>
Sent: 18 October 2017 10:08 AM
To: [email protected]
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.
Reply


Messages In This Thread
CI_Email::from() ignores the display name in the email address - by daveherman - 10-18-2017, 04:39 AM



Theme © iAndrew 2016 - Forum software by © MyBB