You are here

function pet_send_mail in Previewable email templates 7

Same name and namespace in other branches
  1. 8.4 pet.module \pet_send_mail()
  2. 8 pet.module \pet_send_mail()
  3. 6 pet.module \pet_send_mail()

Send tokenized email to a list of recipients.

Given a list of recipients, and an optional node id, perform token substitution and send an email to each. The node substitutions, if any, are the same in each email sent. The user tokens, if any are custom based on the account (if any) associated with each email.

Parameters

$name: The unique name of the PET template.

$recipients: An array of at least one recipient in one of two formats: 1. a simple email address, in which case the uid is looked up 2. an array('mail' => <email address>, 'uid' => <uid>) in which case the uid is already available (more efficient)

$options: An array of options as follows: nid - An optional node id for token substitutions. subject - An optional subject which if provided will override the subject in the PET. body - An optional body which if provided which will override the body in the PET. body_plain - An optional plain text body which if provided which will override the plain text body in the PET. from - An optional from email which if provided which will override the from in the PET (which in turn overrides the site default). reply_to - Optional cc - Optional cc emails which if provided which will override the cc's in the PET. bcc - Optional bcc emails which if provided which will override the bcc's in the PET.

1 call to pet_send_mail()
pet_user_form_submit in includes/pet.admin.inc
Form submission. Take action on step 2 (confirmation of the populated templates).

File

./pet.module, line 318
Previewable Email Template module.

Code

function pet_send_mail($name, $recipients, $options) {
  $pet = pet_load($name);
  if (!$pet) {
    watchdog('pet', 'Unable to load PET %name.', array(
      '%name' => $name,
    ), WATCHDOG_ERROR);
    return;
  }
  if (!is_array($recipients) || count($recipients) < 1) {
    watchdog('pet', 'At least one recipient must be provided for PET %name.', array(
      '%name' => $name,
    ), WATCHDOG_NOTICE);
    return;
  }

  // Override subject and body if necessary
  $pet->subject = isset($options['subject']) ? $options['subject'] : $pet->subject;
  $pet->mail_body = isset($options['body']) ? $options['body'] : $pet->mail_body;
  $pet->mail_body_plain = isset($options['body_plain']) ? $options['body_plain'] : $pet->mail_body_plain;

  // Resolve from address
  if (pet_isset_or($options['from'])) {
    $from = $options['from'];
  }
  elseif ($pet->from_override) {
    $from = $pet->from_override;
  }
  else {
    $from = variable_get('site_mail', ini_get('sendmail_from'));
  }

  // Store data in params in case a module wants to act on them somehow
  $params = array(
    'pet_from' => $from,
    'pet_recipients' => $recipients,
    'pet_nid' => pet_isset_or($options['nid']),
    'pet_cc' => pet_parse_mails(pet_isset_or($options['cc'])),
    'pet_bcc' => pet_parse_mails(pet_isset_or($options['bcc'])),
    'pet_reply_to' => pet_isset_or($options['reply_to']),
    'pet_options' => $options,
  );

  // array to hold status of messages send
  $message_status = array();
  foreach ($recipients as $recipient) {
    if (is_array($recipient)) {
      $params['pet_to'] = $recipient['mail'];
      $params['pet_uid'] = $recipient['uid'];
    }
    else {

      // Strip leading uid for backward compatability
      $mail = preg_replace('/^[0-9]*\\|/', '', $recipient);
      $params['pet_to'] = $mail;
      $params['pet_uid'] = pet_lookup_uid($mail);
    }
    $message_status[$params['pet_to']] = pet_send_one_mail($pet, $params);
  }

  // return message status
  return $message_status;
}