How to send an email to multiple receivers in opencart?

8.2k views Asked by At

This is how I'm currently sending notifications to the (two) admin of the shop

$mail = new Mail();
$mail->protocol = $this->config->get('config_mail_protocol');
$mail->parameter = $this->config->get('config_mail_parameter');
$mail->hostname = $this->config->get('config_smtp_host');
$mail->username = $this->config->get('config_smtp_username');
$mail->password = $this->config->get('config_smtp_password');
$mail->port = $this->config->get('config_smtp_port');
$mail->timeout = $this->config->get('config_smtp_timeout');     

$mailText = html_entity_decode($mailText,ENT_QUOTES, 'UTF-8');
/* Email para el admins Alcudia */
$mail->setFrom($this->request->post['email']);
$mail->setSender($this->request->post['name']);
$mail->setTo($admin_alcudia);
$mail->setSubject(html_entity_decode('Se ha realizado una solicitud de reserva', ENT_QUOTES, 'UTF-8'));
$mail->setHtml($mailText);
$mail->send();
/* Email para el admin de palma */
$mail->setFrom($this->request->post['email']);
$mail->setSender($this->request->post['name']);
$mail->setTo($admin_palma);
$mail->setSubject(html_entity_decode('Se ha realizado una solicitud de reserva', ENT_QUOTES, 'UTF-8'));
$mail->setHtml($mailText);
$mail->send();

The thing is that they're saying that the second one is not reciving it...

Any idea how to improve this? is there any CC functionality?

I've been waiting all day but http://docs.opencart.com/ won't get back to life..

4

There are 4 answers

0
BenM On BEST ANSWER

Try separating the sendTo() function with commas in a string:

$mail = new Mail();

$mail->protocol = $this->config->get('config_mail_protocol');
$mail->parameter = $this->config->get('config_mail_parameter');
$mail->hostname = $this->config->get('config_smtp_host');
$mail->username = $this->config->get('config_smtp_username');
$mail->password = $this->config->get('config_smtp_password');
$mail->port = $this->config->get('config_smtp_port');
$mail->timeout = $this->config->get('config_smtp_timeout');     

$mailText = html_entity_decode($mailText,ENT_QUOTES, 'UTF-8');

$mail->setFrom($this->request->post['email']);
$mail->setSender($this->request->post['name']);
$mail->setTo($admin_alcudia.','.$admin_palma);
$mail->setSubject(html_entity_decode('Se ha realizado una solicitud de reserva', ENT_QUOTES, 'UTF-8'));
$mail->setHtml($mailText);

$mail->send();

This should eliminate the need to have duplicate code.

2
Serg Fortoo On

OpenCart2

/catalog/model/checkout/order.php -> addOrderHistory()

there is an additional script for sending additional addresses, which are configured in the 'Mail' tab

// this is the default code
    $mail = new Mail();
    $mail->protocol = $this->config->get('config_mail_protocol');
    $mail->parameter = $this->config->get('config_mail_parameter');
    $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
    $mail->smtp_username = $this->config->get('config_mail_smtp_username');
    $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
    $mail->smtp_port = $this->config->get('config_mail_smtp_port');
    $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');

    $mail->setTo($this->config->get('config_email'));
    $mail->setFrom($this->config->get('config_email'));
    $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'));
    $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
    $mail->setHtml($this->load->view('mail/order', $data));
    $mail->setText($text);
    $mail->send();

// Send to additional alert emails
    $emails = explode(',', $this->config->get('config_alert_email'));

    foreach ($emails as $email) {
        if ($email && preg_match($this->config->get('config_mail_regexp'), $email)) {
            $mail->setTo($email);
            $mail->send();
        }
    }
// END this is the default code

As a result, you can make the following code

// added mailing from System->Setting->Store->E-Mail
    $mail2 = new Mail();
    $mail2->protocol = $this->config->get('config_mail_protocol');
    $mail2->parameter = $this->config->get('config_mail_parameter');
    $mail2->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
    $mail2->smtp_username = $this->config->get('config_mail_smtp_username');
    $mail2->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
    $mail2->smtp_port = $this->config->get('config_mail_smtp_port');
    $mail2->smtp_timeout = $this->config->get('config_mail_smtp_timeout');

    $mail2->setTo($this->config->get('config_email'));
    $mail2->setFrom($this->config->get('config_email'));
    $mail2->setSender(html_entity_decode($data['store_name'], ENT_QUOTES, 'UTF-8'));
    $mail2->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
    $mail2->setHtml(html_entity_decode($html, ENT_QUOTES, 'UTF-8'));
    $mail2->setText($text);
    $mail2->send();

    // Send to additional alert emails
    $emails = explode(',', $this->config->get('config_alert_email'));

    foreach ($emails as $email) {
      if ($email && preg_match($this->config->get('config_mail_regexp'), $email)) {
          $mail2->setTo($email);
          $mail2->send();
        }
      }
0
Tool Man On

In OpenCart 2.x you need to pass in an array to the setTo() method.

$mail->setTo(array(0 => '[email protected]', 1 => '[email protected]'));
1
Jem On

The other option is to not have to modify any code and to change it in the settings,

If you go to system and then into settings and then click edit on your store it will open up the settings for your store...

You then need to go across to the mail tab ans then scroll down to the "Additional Alert E-Mails" text box and then simply add the additional email address

No change in coding necessary...

Hope this helps,