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
#2

You should never edit a CodeIgniter system file!

If you need to make that change then extend the Email Class.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(10-19-2017, 03:22 AM)InsiteFX Wrote: You should never edit a CodeIgniter system file!

If you need to make that change then extend the Email Class.

I have extended the class - it is called MY_Email.php and it is found in the application/libraries folder.

Please don't misunderstand me. The point of my post is that I feel that the email address parsing in the system class is incomplete as it only identifies the email address portion and ignores the display name. A better implementation would be to parse out both variables.

My version of the class also includes a number of other changes such as implementing a mail queue, improving the alt_message to better format the plain text version of an HTML message and hiding the User-Agent (as we have found a number of services which interrogate the X-Mailer as part of their spam scoring).
Reply
#4

Well I' am sorry if you think I misunderstood you, you did not say that
you extended the Class so I was just pointing out that you should never
edit a CodeIgniter system file.

I' am glad that you got it working, I do not use it I use PhpMailer.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB