1

I am starting to use the Jmail method to send emails via an extension: http://docs.joomla.org/Sending_email_from_extensions

But the method doesn't seem to allow specifying names for the recipient, at least I haven't found the way to do it.

$mailer->addRecipient($recipient);

The documentation says: "mixed $recipient: Either a string or array of strings [e-mail address(es)]"

Anyone knows how to add the name to the recipient?

I'm using Joomla 2.5, the 1.5 method works.

jackJoe
  • 11,078
  • 8
  • 49
  • 64

2 Answers2

1

In Joomla! 2.5 (or starting with the Platform version 11.1) the function accepts two parameters:

public function addRecipient($recipient, $name = '')

where

$recipient - Either a string or array of strings [email address(es)]

$name - Either a string or array of strings [name(s)]

Usage:

$mailer = JFactory::getMailer();

$mailer->addRecipient('john.doe@example.com', 'John Doe');

Valentin Despa
  • 40,712
  • 18
  • 80
  • 106
  • But how do I use it? I tried 'email name' and it throws an error. – jackJoe Nov 16 '12 at 11:42
  • see usage example above. If it does not work, give us the exact error what you are getting. Also making sure that $mailer is an object is good. Try var_dump($mailer) to see it it's not null. – Valentin Despa Nov 16 '12 at 11:51
  • thanks for the example, I tried it and the email is sent, but the name isn't placed/used when receiving the email. I just looked at the email header and I can confirm that it isn't using the name: `To: email@email.com` – jackJoe Nov 16 '12 at 11:56
  • 1
    It's a bug in Joomla!. Just for testing: Backup your /libraries/joomla/mail/email.php file and replace it with https://github.com/joomla/joomla-platform/blob/staging/libraries/joomla/mail/mail.php. Just for testing to see if it works for you. – Valentin Despa Nov 16 '12 at 12:22
  • that's it, this explains why it wasn't working, thanks for all your dedication and help! – jackJoe Nov 16 '12 at 13:23
  • Great. Only think is that it's only a quick fix, not best practice but JMail is quite isoltated. Do test / take care when updating Joomla, the file may be repalced. – Valentin Despa Nov 16 '12 at 13:52
  • I know, fortunately in this case it is a temporary need that I had, so no harm done if it gets squashed in a future update (maybe they will fix it in a future update) – jackJoe Nov 16 '12 at 14:14
0

That's a BUG.

I just facing the same problem, no matter how I pass the arguments, it just can't specify the name.

And in the Joomla! source code /libraries/joomla/mail/mail.php, from line 167, the doc comment says:

/**
 * Add recipients to the email
 *
 * @param   mixed  $recipient  Either a string or array of strings [email address(es)]
 * @param   mixed  $name       Either a string or array of strings [name(s)]
 *
 * @return  JMail  Returns this object for chaining.
 *
 * @since   11.1
 */

Fine, but the variable $name NEVER be used in the function:

public function addRecipient($recipient, $name = '')
{
    // If the recipient is an array, add each recipient... otherwise just add the one
    if (is_array($recipient))
    {
        foreach ($recipient as $to)
        {
            $to = JMailHelper::cleanLine($to);
            $this->AddAddress($to);
        }
    }
    else
    {
        $recipient = JMailHelper::cleanLine($recipient);
        $this->AddAddress($recipient);
    }

    return $this;
}
nevermind
  • 266
  • 3
  • 12
  • You need to patch with the provided fix in the approved answer and it will work, I don't know if 2.5.8 already has it or not. – jackJoe Dec 14 '12 at 09:41