'Mime Mail', 'description' => 'Manage mime mail system settings.', 'page callback' => 'drupal_get_form', 'page arguments' => array('mimemail_admin_settings'), 'access arguments' => array('administer site configuration'), 'file' => 'mimemail.admin.inc', 'file path' => $path, ); $items['mimemail'] = array( 'page callback' => 'mimemail_post', 'access callback' => 'mimemail_incoming_access', 'type' => MENU_CALLBACK, 'file' => 'mimemail.incoming.inc', 'file path' => $path, ); return $items; } /** * Access callback to process incoming messages. */ function mimemail_incoming_access() { return variable_get('mimemail_incoming', FALSE); } /** * Implements hook_form_FORM_ID_alter(). * * Adds the Mime Mail settings on the user settings page. */ function mimemail_form_user_profile_form_alter(&$form, &$form_state) { if ($form['#user_category'] == 'account') { $account = $form['#user']; $form['mimemail'] = array( '#type' => 'fieldset', '#title' => t('Email settings'), '#weight' => 5, '#collapsible' => TRUE, ); $form['mimemail']['mimemail_textonly'] = array( '#type' => 'checkbox', '#title' => t('Plaintext email only'), '#default_value' => !empty($account->data['mimemail_textonly']) ? $account->data['mimemail_textonly'] : FALSE, '#description' => t('Check this option if you do not wish to receive email messages with graphics and styles.'), ); } } /** * Implements hook_user_presave(). */ function mimemail_user_presave(&$edit, $account, $category) { $edit['data']['mimemail_textonly'] = isset($edit['mimemail_textonly']) ? $edit['mimemail_textonly'] : 0; } /** * Implements hook_theme(). */ function mimemail_theme() { module_load_include('inc', 'mimemail', 'theme/mimemail.theme'); return mimemail_theme_theme(); } /** * Implements hook_mail(). */ function mimemail_mail($key, &$message, $params) { $context = $params['context']; $subject = token_replace($context['subject'], $context); $body = token_replace($context['body'], $context); if (isset($params['plaintext']) && !empty($params['plaintext'])) { $plaintext = token_replace($params['plaintext'], $context); $message['params']['plaintext'] = drupal_html_to_text($plaintext); } $message['subject'] .= str_replace(array("\r", "\n"), '', $subject); $message['body'][] = $body; } /** * Retreives a list of all available mailer engines. * * @return * An array of mailer engine names. */ function mimemail_get_engines() { $engines = array(); foreach (module_implements('mailengine') as $module) { $engines[$module] = module_invoke($module, 'mailengine', 'list'); } return $engines; } /** * Implements hook_mailengine(). * * @param $op * The operation to perform on the message. * @param $message * The message to perform the operation on. * @return * Returns TRUE if the operation was successful or FALSE if it was not. */ function mimemail_mailengine($op, $message = array()) { module_load_include('inc', 'mimemail'); switch ($op) { case 'list': $engine = array( 'name' => t('Mime Mail'), 'description' => t("Default mailing engine."), ); return $engine; case 'settings': // Not implemented. break; case 'multiple': case 'single': case 'send': // Default values. $default = array('to' => '', 'subject' => '', 'body' => '', 'from' => '', 'headers' => ''); $message = array_merge($default, $message); // If 'Return-Path' isn't already set in php.ini, we pass it separately // as an additional parameter instead of in the header. // However, if PHP's 'safe_mode' is on, this is not allowed. if (isset($message['headers']['Return-Path']) && !ini_get('safe_mode')) { $return_path_set = strpos(ini_get('sendmail_path'), ' -f'); if (!$return_path_set) { $message['Return-Path'] = $message['headers']['Return-Path']; unset($message['headers']['Return-Path']); } } $recipients = (!is_array($message['to'])) ? array($message['to']) : $message['to']; $subject = mime_header_encode($message['subject']); $headers = mimemail_rfc_headers($message['headers']); foreach ($recipients as $to) { if (isset($message['Return-Path']) && !ini_get('safe_mode')) { $mail_result = mail($to, $subject, $message['body'], $headers, // Pass the Return-Path via sendmail's -f command. '-f ' . $message['Return-Path'] ); } else { // The optional $additional_parameters argument to mail() is not allowed // if safe_mode is enabled. Passing any value throws a PHP warning and // makes mail() return FALSE. $mail_result = mail($to, $subject, $message['body'], $headers); } } return $mail_result; } return FALSE; } /** * Prepares the message for sending. * * @param $message * An array containing the message data. The optional parameters in 'params' are: * - 'plain': * Whether to send the message as plaintext only or HTML. If evaluates to TRUE, * then the message will be sent as plaintext. * - 'plaintext': * Optional plaintext portion of a multipart email. * - 'attachments': * An array of arrays which describe one or more attachments. The internal array * consists of two parts: the file's path and the file's MIME type. * The array of arrays looks something like this: * Array * ( * [0] => Array * ( * [filepath] => '/path/to/file.name', * [filemime] => 'mime/type' * ) * ) * @return * The $message array structure containing all details of the message. */ function mimemail_prepare_message($message) { module_load_include('inc', 'mimemail'); $key = $message['key']; $to = $message['to']; $from = $message['from']; $subject = $message['subject']; $body = $message['body']; $headers = isset($message['params']['headers']) ? $message['params']['headers'] : array(); $plain = isset($message['params']['plain']) ? $message['params']['plain'] : NULL; $plaintext = isset($message['params']['plaintext']) ? $message['params']['plaintext'] : NULL; $attachments = isset($message['params']['attachments']) ? $message['params']['attachments'] : array(); $site_name = variable_get('site_name', 'Drupal'); $site_mail = variable_get('site_mail', ini_get('sendmail_from')); // Override site mails default sender when using default engine. if ((empty($from) || $from == $site_mail) && variable_get('mimemail_engine', 'mimemail') == 'mimemail') { $from = array( 'name' => variable_get('mimemail_name', $site_name), 'mail' => variable_get('mimemail_mail', $site_mail), ); } // Body is empty, this is a plaintext message. if (empty($body)) { $plain = TRUE; } // Try to determine recipient's text mail preference. elseif (is_null($plain)) { if (is_object($to) && isset($to->data['mimemail_textonly'])) { $plain = $to->data['mimemail_textonly']; } elseif (is_string($to) && valid_email_address($to)) { if (is_object($account = user_load_by_mail($to)) && isset($account->data['mimemail_textonly'])) { $plain = $account->data['mimemail_textonly']; $to = $account; // Might as well pass the user object to the address function. } } } $subject = str_replace("\n", '', trim(drupal_html_to_text($subject))); $key = str_replace('_', '-', $key); $body = theme(array('mimemail_message__' . $key, 'mimemail_message'), array('key' => $key, 'subject' => $subject, 'body' => $body)); foreach (module_implements('mail_post_process') as $module) { $function = $module . '_mail_post_process'; $function($body, $key); } $plain = $plain || variable_get('mimemail_textonly', 0); $from = mimemail_address($from); $subject = mime_header_encode($subject); $mail = mimemail_html_body($body, $subject, $plain, $plaintext, $attachments); $headers = array_merge($message['headers'], $headers, $mail['headers']); $message['to'] = mimemail_address($to); $message['from'] = $from; $message['subject'] = $subject; $message['body'] = $mail['body']; $message['headers'] = mimemail_headers($headers, $from); return $message; }