home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (mimemail_action.module)

This example Drupal source code file (mimemail_action.module) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

address, array, author, file, foreach, function, if, node, php, recipient, string, the, title, type

The mimemail_action.module Drupal example source code

<?php // $Id: mimemail_action.module,v 1.1.2.4 2010/11/06 18:13:40 sgabe Exp $

/**
 * @file
 * Provide actions for Mime Mail.
 */

/**
* Implementation of hook_action_info().
*/
function mimemail_action_info() {
  return array(
    'mimemail_send_mail_action' => array(
      'description' => t('Send an HTML e-mail'),
      'type' => 'system',
      'configurable' => TRUE,
      'hooks' => array(
        'nodeapi' => array('view', 'insert', 'update', 'delete'),
        'comment' => array('view', 'insert', 'update', 'delete'),
        'user' => array('view', 'insert', 'update', 'delete', 'login'),
        'taxonomy' => array('insert', 'update', 'delete'),
      ),
    ),
  );
}

/**
 * Implementation of a configurable Drupal action. Sends an email.
 */
function mimemail_send_mail_action(&$object, $context = array()) {
  global $user;

  switch ($context['hook']) {
    case 'nodeapi':
      // Because this is not an action of type 'node' (it's an action
      // of type 'system') the node will not be passed as $object,
      // but it will still be available in $context.
      $node = $context['node'];
      break;
    case 'comment':
      // The comment hook provides nid, in $context.
      $comment = $context['comment'];
      $node = node_load($comment->nid);
      break;
    case 'user':
      // Because this is not an action of type 'user' the user
      // object is not passed as $object, but it will still be
      // available in $context.
      $account = $context['account'];
      if (isset($context['node'])) {
        $node = $context['node'];
      }
      elseif (strpos($context['recipient'], '%author') !== FALSE) {
        // If we don't have a node, we don't have a node author.
        watchdog('error', 'Cannot use %author token in this context.');
        return;
      }
      break;
    default:
      // We are being called directly.
      $node = $object;
  }

  $recipient = $context['to'];
  $subject = $context['subject'];
  $body = $context['message_html'];
  $text = $context['message_plaintext'];
  $mailkey = 'mimemail_action_send_mail';

  $headers = array();
  if (!empty($context['bcc'])) {
    $headers['Bcc'] = $context['bcc'];
  }
  if (!empty($context['cc'])) {
    $headers['Cc'] = $context['cc'];
  }

  // Prepare the array of the attachments.
  $attachments = array();
  $attachments_string = trim($context['attachments']);
  if (!empty($attachments_string)) {
    $attachment_lines = array_filter(explode('\n', trim($attachments_string)));
    foreach ($attachment_lines as $key => $attachment_line) {
      $attachment = explode(':', trim($attachment_line), 2);
      $attachments[] = array(
        'filepath' => $attachment[1],
        'filemime' => $attachment[0],
      );
    }
  }

  if (isset($node)) {
    if (!isset($account)) {
      $account = user_load(array('uid' => $node->uid));
    }
  }

  if (!isset($account)) {
    $account = $user;
  }

  // Set site wide variables.
  $site_name = variable_get('site_name', 'Drupal');
  $from = variable_get('site_mail', ini_get('sendmail_from'));

  // Replace variables.
  if (isset($node) && is_object($node)) {
    $variables = array(
      '%author' => $account->mail,
      '%site_name' => $site_name,
      '%username' => $account->name,
      '%uid' => $node->uid,
      '%node_url' => url('node/'. $node->nid, array('absolute' => TRUE)),
      '%node_type' => $node->type,
      '%title' => $node->title,
      '%teaser' => $node->teaser,
      '%body' => $node->body,
    );
    $recipient = strtr($recipient, $variables);
    $subject = strtr($subject, $variables);
    $body = strtr($body, $variables);
    $text = strtr($text, $variables);
  }

  $status = mimemail($from, $recipient, $subject, $body, NULL, $headers, $text, $attachments, $mailkey);
  $recipients = trim(implode(', ', array_merge(explode(',', $recipient), explode(',', $context['bcc']), explode(',', $context['cc']))), ', ');

  if ($status) {
    watchdog('action', 'Sent HTML email to %recipients', array('%recipients' => $recipients));
  }
  else {
    watchdog('error', 'Unable to send HTML email to %recipients', array('%recipient' => $recipients));
  }
}

/**
 * Form for configurable Drupal action to send an HTML mail.
 */
function mimemail_send_mail_action_form($context) {
  $context += array('to' => '', 'cc' => '', 'bcc' => '', 'subject' => '', 'message_html' => '', 'message_plaintext' => '', 'attachments' => '');

  $form['to'] = array(
    '#type' => 'textfield',
    '#title' => t('Recipient'),
    '#default_value' => $context['to'],
    '#maxlength' => 254,
    '#description' => t("The mail's recipient address. You may separate multiple addresses with comma and use %author if the recipient is the author of the original post.", array('%author' => '%author')),
    '#required' => TRUE,
  );
 $form['cc'] = array(
    '#type' => 'textfield',
    '#title' => t('CC Recipient'),
    '#default_value' => $context['cc'],
    '#description' => t("The mail's carbon copy address. You may separate multiple addresses with comma."),
    '#required' => FALSE,
  );
  $form['bcc'] = array(
    '#type' => 'textfield',
    '#title' => t('BCC Recipient'),
    '#default_value' => $context['bcc'],
    '#description' => t("The mail's blind carbon copy address. You may separate multiple addresses with comma."),
    '#required' => FALSE,
  );
  $form['subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#maxlength' => 254,
    '#default_value' => $context['subject'],
    '#description' => t("The mail's subject."),
  );
  $form['message_html'] = array(
    '#type' => 'textarea',
    '#title' => t('HTML message'),
    '#default_value' => $context['message_html'],
    '#description' => t("The message body in HTML format. You may include the following variables: %site_name, %username, %node_url, %node_type, %title, %teaser, %body. Not all variables will be available in all contexts."),
  );
  $form['message_plaintext'] = array(
    '#type' => 'textarea',
    '#title' => t('Text message'),
    '#default_value' => $context['message_plaintext'],
    '#description' => t("Optional plaintext portion of a multipart message."),
  );
  $form['attachments'] = array(
    '#type' => 'textarea',
    '#title' => t('Attachments'),
    '#default_value' => $context['attachments'],
    '#description' => t('A list of attachments, one file per line like [mimetype]:[path] e.g. "image/png:/files/images/mypic.png".'),
  );

  return $form;
}

/**
 * Validate the action form.
 */
function mimemail_send_mail_action_validate($form, $form_state) {
  $to = explode(',', $form_state['values']['to']);
  $cc = explode(',', $form_state['values']['cc']);
  $bcc = explode(',', $form_state['values']['bcc']);

  foreach ($to as $recipient) {
    $recipient = trim($recipient);
    if (!valid_email_address($recipient) && $recipient != '%author') {
      form_set_error('recipient', t('Please enter a valid e-mail address or %author.', array('%author' => '%author')));
    }
  }
  foreach ($cc as $recipient) {
    $recipient = trim($recipient);
    if (!empty($recipient) && !valid_email_address($recipient) && $recipient != '%author') {
      form_set_error('cc', t('Please enter a valid e-mail address or %author.', array('%author' => '%author')));
    }
  }
  foreach ($bcc as $recipient) {
    $recipient = trim($recipient);
    if (!empty($recipient) && !valid_email_address($recipient) && $recipient != '%author') {
      form_set_error('bcc', t('Please enter a valid e-mail address or %author.', array('%author' => '%author')));
    }
  }
}

/**
 * Handle submission of the action form.
 */
function mimemail_send_mail_action_submit($form, $form_state) {
  $form_values = $form_state['values'];

  $params = array(
    'to' => $form_values['to'],
    'cc' => $form_values['cc'],
    'bcc' => $form_values['bcc'],
    'subject' => $form_values['subject'],
    'message_html' => $form_values['message_html'],
    'message_plaintext' => $form_values['message_plaintext'],
    'attachments' => $form_values['attachments'],
  );

  return $params;
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal mimemail_action.module source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.