Buy Now
Sign in

When cc is list e-mail not sent

dzseti , May 06 2017, 15:58
D
dzseti 37
May 06 2017, 15:58 #367837
When use only one cc email, then it is sent successfully. Otherwise, if I use comma-separated email addresses, then [cc] becomes an array (not a single value) and I get the error:

[result] => the Mail could not be sent.

Also, is it possible to include the name and email address in cc like "name<email address>"?
D
dzseti 37
May 07 2017, 14:42 #367870
This seems to be a Joomla (or maybe php) bug. I checked the coding against the sendMail() method for JMail api and your code looked correct, but alawys returned an error.

After amending your code in mail.php as follows I managed to get both the cc list and email names working; it should also work for the WordPress code, because emails need to be listed either as address or as name<name@address> (which should work in header). But I have not tested WordPress.

Modified code for the Joomla version of
public function send()



      // ADDED
      $cc_recipients = (is_array($this->cc)) ? $this->cc : array($this->cc);
      $bcc_recipients = (is_array($this->bcc)) ? $this->bcc : array($this->bcc);
      
      $cc_recipient_names = array_map(function($address) {
        $name = explode('<', $address);
        return (isset($name[1])) ? trim($name[0]) : '';
      }, $cc_recipients);
      
      $cc_recipients = array_map(function($address, $name, $key) {
        if ($name == '') return $address;
        else return trim(explode('<', $address)[1], '> ');
      }, $cc_recipients, $cc_recipient_names, array_keys($cc_recipients));
      
      $bcc_recipient_names = array_map(function($address) {
        $name = explode('<', $address);
        return (isset($name[1])) ? trim($name[0]) : '';
      }, $bcc_recipients);
      
      $bcc_recipients = array_map(function($address, $name, $key) {
        if ($name == '') return $address;
        else return trim(explode('<', $address)[1], '> ');
      }, $bcc_recipients, $bcc_recipient_names, array_keys($bcc_recipients));
      // END ADDED
      
      $mailer = \JFactory::getMailer();
      $mailer->setSender([$this->from_email, $this->from_name]);
      $mailer->addRecipient($this->to);
      $mailer->setSubject($this->subject);
      $mailer->setBody($this->body);
      $mailer->isHtml(($this->mode == 'html') ? true : false); // AMENDED
      $mailer->addCc($cc_recipients, $cc_recipient_names); // ADDED
      $mailer->addCc($bcc_recipients, $bcc_recipient_names); // ADDED
      foreach($this->attachments as $attachment){
        $mailer->addAttachment($attachment);
      }
      $result = $mailer->Send(); // ADDED; FOLLOWING COMMENTED OUT
      /*$result = $mailer->sendMail(
        $this->from_email,
        $this->from_name,
        $this->to,
        $this->subject,
        $this->body,
        ($this->mode == 'html') ? true : false,
        $this->cc,
        $this->bcc,
        !empty($this->attachments) ? $this->attachments : null,
        $this->reply_email,
        $this->reply_name
      );*/