5

So this is my code where I send same email to 3 different users

$mailer->sendMail($emailfrom, $sitename, 'adam@malinator.com', $subject, $message, 1);
$mailer->sendMail($emailfrom, $sitename, 'a.fini@ambulatoriprivati.it', $subject, $message, 1);
$mailer->sendMail($emailfrom, $sitename, 'adam.madrzejewski@gmail.com', $subject, $message, 1);

And this is what I see in my gmail (My email is the last one adam.madrzejewski@gmail.com):

enter image description here

But I don't want that all successive recipients see to who email was sent before. How can it be solved? I can't find anything on JMail's API page:

https://api.joomla.org/cms-3/classes/JMail.html

Using Joomla 3.5.1

3 Answers 3

7

As you can see on the API reference page, the sendMail() function has a $bcc parameter:

sendMail(string $from, string $fromName, mixed $recipient, string $subject, string $body, boolean $mode = false, mixed $cc = null, mixed $bcc = null, mixed $attachment = null, mixed $replyTo = null, mixed $replyToName = null)

Also, if you're sending the same email to 3 different recipients, you don't need to use the sendMail() function 3 times, but instead, simply define an array of recipients.

Have a try of the following:

$to = array(
    'adam@malinator.com'
);

$bcc = array(
    'a.fini@ambulatoriprivati.it',
    'adam.madrzejewski@gmail.com'
);

$mailer->sendMail($emailfrom, $sitename, $to, $subject, $message, 1, null, $bcc);
1
  • Using cc or bcc seems to be the best solution for sending multiple emails. I tried another solution, that can be used, but doesn't seem to be the best solution and looks more like a workaround.
    – Adam M.
    Jun 7, 2016 at 7:15
3

There are 2 solutions you can try of the following.

  • By avoiding the mailer instance : $mailerand using the following code -

JFactory::getMailer()

JFactory::getMailer()->sendMail($emailfrom, $sitename, 'adam@malinator.com', $subject, $message, 1);
JFactory::getMailer()->sendMail($emailfrom, $sitename, 'a.fini@ambulatoriprivati.it', $subject, $message, 1);
JFactory::getMailer()->sendMail($emailfrom, $sitename, 'adam.madrzejewski@gmail.com', $subject, $message,1);`

Refer for more details :

https://developer.joomla.org/joomlacode-archive/issue-31986.html

  • Take an array of recipients and place the mailer instance inside the loop as following -

$mailer

$recipients = array('email1','email2','email3' );
for($i=0;$i<3;$i++){
    $mailer = JFactory::getMailer();
    $mailer->isHTML(true);
    $mailer->Encoding = 'base64';
    $mailer->setSubject('Your subject string');
    $mailer->setBody($body);
    $mailer->addRecipient($recipients[$i]);
    $mailer->setSender($sender);
    $send = $mailer->Send();
}

Refer for more details:

https://developer.joomla.org/joomlacode-archive/issue-29095.html

2
  • Ye this seems to solve the problem. I think that by default it should clean the recipients array each time we call sendMail() funcion, maybe it doesn't because we have email's cc and bcc which seems to be the best option to send multiple emails and thats why I'm going to accept Lodder's answer and for you +1 :)
    – Adam M.
    Jun 7, 2016 at 7:11
  • not an issue :) whichever you find the best solution for you. I find this method appropriate so I use this.
    – Liz.
    Jun 7, 2016 at 7:17
0

Joomla Mailer extends the PHPMailer, so you can add this line of code after the send()

$mailer->clearAllRecipients();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.