Mohammad Naji
Mohammad Naji

Reputation: 5442

Setting a name (alias) for codeigniter's email recipient

In the user manual of Codeigniter's Email Library, under Email Function Reference, wer see $this->email->from() with two parameters: an email address as well as as 'Your Name' that is set as the second parameter.

But when it comes to $this->email->to(), we cannot set the recipient's name. While we can see that in the real world (eg. gmail), the request has been answered as it's expected.

Upvotes: 2

Views: 2492

Answers (3)

Mqiu
Mqiu

Reputation: 11

I just found a trick to bypass the cleaning process in Codeigniter. Instead of using $this->email->to(), you should manually set the email header.

$this->email->set_header('To', '"' . $recipient_display_name . '" <'. $recipient_email . '>');

I haven't tested out providing a list of recipient emails, but I think the idea is the same.

Upvotes: 1

Igor Parra
Igor Parra

Reputation: 10348

Don't waste your time trying to pass recipient's name with code like this:

 $this->email->to('John Smith <[email protected]>');

because to() function clean all parameters passed with clean_email()

    public function to($to)
    {
        $to = $this->_str_to_array($to);
        $to = $this->clean_email($to);
    // ...



/**
 * Clean Extended Email Address: Joe Smith <[email protected]>
 *
 * @access  public
 * @param   string
 * @return  string
 */
public function clean_email($email)
{
    if ( ! is_array($email))
    {
        if (preg_match('/\<(.*)\>/', $email, $match))
        {
            return $match['1'];
        }
        else
        {
            return $email;
        }
    }
    // ...

Upvotes: 2

sybear
sybear

Reputation: 7784

Unfortunately, function to() does not provide any means of settings recipient's name.

However, you could extend the Email class and add manually that additional functionality such as setting names.

Otherwise you should probably use another tool for sending emails. For example PHPMailer.

Upvotes: 2

Related Questions