You are here

function commerce_invoice_receipt_action_mail in Commerce Invoice Receipt 7.2

Same name and namespace in other branches
  1. 7 commerce_invoice_receipt.module \commerce_invoice_receipt_action_mail()

Rules action callback for sending the receipt email.

File

./commerce_invoice_receipt.rules.inc, line 105
Rules integration for Invoice Receipt.

Code

function commerce_invoice_receipt_action_mail($order, $to, $cc = NULL, $bcc = NULL, $subject, $from = NULL) {

  // Get the email address to which we need to send the email.
  $to = str_replace(array(
    "\r",
    "\n",
  ), '', $to);

  // Prepare the recipients.
  $recipients = explode(',', $to);

  // Get the email address from which we are sending the invoice.
  $from = !empty($from) ? str_replace(array(
    "\r",
    "\n",
  ), '', $from) : NULL;

  // Prepare the parameters.
  $params = array(
    'subject' => $subject,
    'order' => $order,
  );

  // CC/BCC.
  if ($cc) {
    $params['cc'] = $cc;
  }
  if ($bcc) {
    $params['bcc'] = $bcc;
  }

  // Check which language the user is preferred; if the site is multilingual,
  // this will allow us to send the email in the language which is set in user
  // preferences (if the site allows users to set that). Otherwise, this will
  // fall back to the default language used on the whole website.
  $account = user_load($order->uid);
  $language = user_preferred_language($account);

  // Send the emails.
  foreach ($recipients as $recipient) {

    // Trim the recipient email address, in case there is a space after the
    // comma.
    $recipient = trim($recipient);

    // Send the email.
    if ($recipient) {
      $message = drupal_mail('commerce_invoice_receipt', 'invoice', $recipient, $language, $params, $from);
      if ($message['result']) {
        watchdog('rules', 'Invoice successfully sent to %recipient. Carbon copy: %cc. Blind carbon copy: %bcc.', array(
          '%recipient' => $recipient,
          '%cc' => $cc ? $cc : 'empty',
          '%bcc' => $bcc ? $bcc : 'empty',
        ));
      }
    }
  }
}