Send Bcc Email When Order Is Complete And Notify User

Hi,

We want if the order is completed to send an order notification with a bcc. The code below works, but we want only send the email with bcc if we notify the user and if notify the user is unchecked there must be no notification sent to the user. This should be easy. When we tested the code below the user received 2 emails. Is there a way around it?

When we tested the code below the user received 2 emails. Is there a way around it?

if ($order_info['status'] == 'C') {
	    /** @var \Tygh\Mailer\Mailer $mailer */
        $mailer = Tygh::$app['mailer'];
        
	    $mailer->send(array(
	        'to' => $order_info['email'],
	        'from' => 'company_orders_department',
	        'bcc' => 'default_company_bcc',
	        'data' => array(
	            'notify_user' => true,
	            'order_info' => $order_info,
	            'shipments' => $shipments,
	            'tracking_numbers' => $tracking_numbers,
	            'shipping_methods' => $shipping_methods,
	            'order_status' => $order_status,
	            'payment_method' => $payment_method,
	            'profile_fields' => $profile_fields,
	            'profields' => $profields,
	            'secondary_currency' => $secondary_currency,
	            'take_surcharge_from_vendor' => $take_surcharge_from_vendor
	        ),
	        'template_code' => $email_template_name,
	        'company_id' => $order_info['company_id'],
	    ), 'C', $order_info['lang_code']);
    
} 

Hi,

We want if the order is completed to send an order notification with a bcc. The code below works, but we want only send the email with bcc if we notify the user and if notify the user is unchecked there must be no notification sent to the user. This should be easy. When we tested the code below the user received 2 emails. Is there a way around it?

When we tested the code below the user received 2 emails. Is there a way around it?

if ($order_info['status'] == 'C') {
	    /** @var \Tygh\Mailer\Mailer $mailer */
        $mailer = Tygh::$app['mailer'];
        
	    $mailer->send(array(
	        'to' => $order_info['email'],
	        'from' => 'company_orders_department',
	        'bcc' => 'default_company_bcc',
	        'data' => array(
	            'notify_user' => true,
	            'order_info' => $order_info,
	            'shipments' => $shipments,
	            'tracking_numbers' => $tracking_numbers,
	            'shipping_methods' => $shipping_methods,
	            'order_status' => $order_status,
	            'payment_method' => $payment_method,
	            'profile_fields' => $profile_fields,
	            'profields' => $profields,
	            'secondary_currency' => $secondary_currency,
	            'take_surcharge_from_vendor' => $take_surcharge_from_vendor
	        ),
	        'template_code' => $email_template_name,
	        'company_id' => $order_info['company_id'],
	    ), 'C', $order_info['lang_code']);
    
} 

Hello!

Do you use the fn_order_notification function? Then you can check the $force_notification variable to know if "notify user" is enabled.

If you have the "notify orders department" set for a status, then 2 emails will be sent. If you uncheck that setting then your code above will BCC you when sent to your customer. I'm guessing you have 'default_company_bcc' set somewhere so that it is turned into an address.

It can also be done using the hook ('mailer_send_pre') but there is no setting out of the box to use BCC (or even CC) field.

I think using that hook would be a lot cleaner for simply setting the BCC field rather than duplicating the call to the Mailer::sendMail() method. Just two unsolicited cents...

Hi, Thanks for your suggestions.

‘default_company_bcc’ is a field somewhere where we set the bcc address.
One question. I have modified it so it uses the (‘send_mail_pre’) hook. To add a bcc address i use addrAppend. Now i want to check if the order status is Completed. Can i do this like below?
if ($params['data']['order_info']['status'] == 'C') {

}

That should work.

Yes, thats works. Only thing is that it will not add a Bcc address. I use the following code to ad a bcc
$mailer->addrAppend('bcc', 'example@example.com');

Is this correct or am i missing something?

It think it's case sensitive since it stores addresses in an array. Try using 'Bcc' instead of 'bcc'. Note too that your address should be an array. I.e.

$mailer->addrAppend('Bcc', array('fexample@example.com"));

Note that while the method is named 'append' it is really a 'set'. So be careful that you don't mistakenly overwrite other header fields.