| 1 |
<?php /* $Id: mimemail.module,v 1.39 2009/02/22 01:37:44 jerdavis Exp $ */ |
<?php /* $Id: mimemail.module,v 1.44 2009/02/23 04:28:46 jerdavis Exp $ */ |
| 2 |
|
|
| 3 |
/** |
/** |
| 4 |
* @file |
* @file |
| 9 |
* Implementation of hook_menu() |
* Implementation of hook_menu() |
| 10 |
*/ |
*/ |
| 11 |
function mimemail_menu() { |
function mimemail_menu() { |
| 12 |
|
$path = drupal_get_path('module', 'mimemail') .'/includes'; |
| 13 |
$items['admin/settings/mimemail'] = array( |
$items['admin/settings/mimemail'] = array( |
| 14 |
'title' => 'Mime Mail', |
'title' => 'Mime Mail', |
| 15 |
'description' => 'HTML E-mail settings', |
'description' => 'HTML E-mail settings', |
| 16 |
'page callback' => 'drupal_get_form', |
'page callback' => 'drupal_get_form', |
| 17 |
'page arguments' => array('mimemail_settings'), |
'page arguments' => array('mimemail_admin_settings'), |
| 18 |
'access arguments' => array('administer site configuration'), |
'access arguments' => array('administer site configuration'), |
| 19 |
'type' => MENU_NORMAL_ITEM, |
'type' => MENU_NORMAL_ITEM, |
| 20 |
|
'file' => 'mimemail.admin.inc', |
| 21 |
|
'file path' => $path, |
| 22 |
); |
); |
| 23 |
$items['mimemail'] = array( |
$items['mimemail'] = array( |
| 24 |
'page callback' => 'mimemail_post', |
'page callback' => 'mimemail_post', |
| 25 |
'access callback' => variable_get('mimemail_incoming', FALSE), |
'access callback' => variable_get('mimemail_incoming', FALSE), |
| 26 |
'type' => MENU_CALLBACK, |
'type' => MENU_CALLBACK, |
| 27 |
|
'file' => 'mimemail.incoming.inc', |
| 28 |
|
'file path' => $path, |
| 29 |
); |
); |
| 30 |
return $items; |
return $items; |
| 31 |
} |
} |
| 32 |
|
|
| 33 |
/** |
/** |
|
* Administration settings. |
|
|
* |
|
|
* @return |
|
|
* The administration from. |
|
|
*/ |
|
|
function mimemail_settings() { |
|
|
|
|
|
// override the smtp_library value if mimemail is chosen to handle all mail |
|
|
// this will cause drupal_mail to call mimemail() |
|
|
if (variable_get('mimemail_alter', 0)) { |
|
|
if (!strpos(variable_get('smtp_library', ''), 'mimemail')) { |
|
|
variable_set('smtp_library', drupal_get_filename('module', 'mimemail')); |
|
|
} |
|
|
} |
|
|
else { |
|
|
if (strpos(variable_get('smtp_library', ''), 'mimemail')) { |
|
|
db_query("DELETE FROM {variable} WHERE name = 'smtp_library'"); |
|
|
} |
|
|
} |
|
|
|
|
|
$engines = mimemail_get_engines(); |
|
|
|
|
|
$form = array(); |
|
|
$form['site_mail'] = array( |
|
|
'#type' => 'textfield', |
|
|
'#title' => t('E-mail address'), |
|
|
'#default_value' => variable_get('site_mail', ini_get('sendmail_from')), |
|
|
'#size' => 60, |
|
|
'#maxlength' => 128, |
|
|
'#description' => t('A valid e-mail address for this website, used by the auto-mailer during registration, new password requests, notifications, etc.') |
|
|
); |
|
|
$form['mimemail']['mimemail_alter'] = array( |
|
|
'#type' => 'checkbox', |
|
|
'#title' => t('Use mime mail for all messages'), |
|
|
'#default_value' => variable_get('mimemail_alter', 0), |
|
|
'#description' => t('Use the mime mail module to deliver all site messages. With this option, system emails will have styles and formatting'), |
|
|
); |
|
|
|
|
|
$filter_format = variable_get('mimemail_format', FILTER_FORMAT_DEFAULT); |
|
|
$form['mimemail']['mimemail_format'] = filter_form($filter_format, NULL, array("mimemail_format")); |
|
|
|
|
|
$form['mimemail']['mimemail_textonly'] = array( |
|
|
'#type' => 'checkbox', |
|
|
'#title' => t('Plaintext email only'), |
|
|
'#default_value' => variable_get('mimemail_textonly', 0), |
|
|
'#description' => t('This option disables the use of email messages with graphics and styles. All messages will be converted to plain text.'), |
|
|
); |
|
|
|
|
|
$form['mimemail']['incoming'] = array( |
|
|
'#type' => 'fieldset', |
|
|
'#title' => t('Advanced Settings'), |
|
|
'#collapsible' => TRUE, |
|
|
'#collapsed' => TRUE, |
|
|
); |
|
|
$form['mimemail']['incoming']['mimemail_incoming'] = array( |
|
|
'#type' => 'checkbox', |
|
|
'#title' => t('Process incoming messages posted to this site'), |
|
|
'#default_value' => variable_get('mimemail_incoming', 0), |
|
|
'#description' => t('This is an advanced setting that should not be enabled unless you know what you are doing'), |
|
|
); |
|
|
$form['mimemail']['incoming']['mimemail_key'] = array( |
|
|
'#type' => 'textfield', |
|
|
'#title' => t('Message validation string'), |
|
|
'#default_value' => variable_get('mimemail_key', md5(rand())), |
|
|
'#required' => TRUE, |
|
|
'#description' => t('This string will be used to validate incoming messages. It can be anything, but must be used on both sides of the transfer'), |
|
|
); |
|
|
|
|
|
// hide the settings if only 1 engine is available |
|
|
if (count($engines) == 1) { |
|
|
variable_set('mimemail_engine', key($engines)); |
|
|
$form['mimemail_engine'] = array( |
|
|
'#type' => 'hidden', |
|
|
'#title' => t('E-mail engine'), |
|
|
'#default_value' => variable_get('mimemail_engine', 'mail'), |
|
|
'#options' => $engines, |
|
|
'#description' => t('Choose an e-mail engine for sending mails from your site.') |
|
|
); |
|
|
} |
|
|
else { |
|
|
$form['mimemail_engine'] = array( |
|
|
'#type' => 'select', |
|
|
'#title' => t('E-mail engine'), |
|
|
'#default_value' => variable_get('mimemail_engine', 'mail'), |
|
|
'#options' => $engines, |
|
|
'#description' => t('Choose an e-mail engine for sending mails from your site.') |
|
|
); |
|
|
} |
|
|
|
|
|
if (variable_get('mimemail_engine', 0)) { |
|
|
$settings = module_invoke(variable_get('mimemail_engine', 'mail'), 'mailengine', 'settings'); |
|
|
if ($settings) { |
|
|
$form['mimemail_engine_settings'] = array( |
|
|
'#type' => 'fieldset', |
|
|
'#title' => t('Engine specific settings'), |
|
|
); |
|
|
foreach ($settings as $name => $value) { |
|
|
$form['mimemail_engine_settings'][$name] = $value; |
|
|
} |
|
|
} |
|
|
} |
|
|
else { |
|
|
drupal_set_message(t('Please choose a mail engine.'), 'error'); |
|
|
} |
|
|
|
|
|
return system_settings_form($form); |
|
|
} |
|
|
|
|
|
/** |
|
| 34 |
* Implementation of hook_user(). |
* Implementation of hook_user(). |
| 35 |
*/ |
*/ |
| 36 |
function mimemail_user($op, &$edit, &$account, $category = '') { |
function mimemail_user($op, &$edit, &$account, $category = '') { |
| 93 |
* An array containing the MIME encoded message, including headers and body. |
* An array containing the MIME encoded message, including headers and body. |
| 94 |
*/ |
*/ |
| 95 |
function mimemail_prepare($sender, $recipient, $subject, $body, $plaintext = NULL, $headers = array(), $text = NULL, $attachments = array(), $mailkey = '') { |
function mimemail_prepare($sender, $recipient, $subject, $body, $plaintext = NULL, $headers = array(), $text = NULL, $attachments = array(), $mailkey = '') { |
| 96 |
|
module_load_include('inc', 'mimemail'); |
|
require_once dirname(__FILE__) .'/mimemail.inc'; |
|
| 97 |
|
|
| 98 |
if (is_null($sender)) { // use site default for sender |
if (is_null($sender)) { // use site default for sender |
| 99 |
$sender = array( |
$sender = array( |
| 117 |
} |
} |
| 118 |
} |
} |
| 119 |
$subject = mime_header_encode(drupal_html_to_text($subject)); |
$subject = mime_header_encode(drupal_html_to_text($subject)); |
| 120 |
|
$body = theme('mimemail_message', $body, $mailkey); |
| 121 |
|
foreach (module_implements('mail_post_process') as $module) { |
| 122 |
|
$function = $module .'_mail_post_process'; |
| 123 |
|
$function($body, $mailkey); |
| 124 |
|
} |
| 125 |
|
|
| 126 |
$plaintext = $plaintext || variable_get('mimemail_textonly', 0); |
$plaintext = $plaintext || variable_get('mimemail_textonly', 0); |
| 127 |
$sender = mimemail_address($sender); |
$sender = mimemail_address($sender); |
| 128 |
$mail = mimemail_html_body(theme('mimemail_message', $body, $mailkey), $subject, $plaintext, $text, $attachments); |
$mail = mimemail_html_body($body, $subject, $plaintext, $text, $attachments); |
| 129 |
$headers = array_merge($headers, $mail['headers']); |
$headers = array_merge($headers, $mail['headers']); |
| 130 |
$message = array( |
$message = array( |
| 131 |
'address' => mimemail_address($recipient), |
'address' => mimemail_address($recipient), |
| 276 |
} |
} |
| 277 |
|
|
| 278 |
/** |
/** |
|
* Receive messages POSTed from an external source. |
|
|
* |
|
|
* This function enables messages to be sent via POST or some other RFC822 |
|
|
* source input (e.g. directly from a mail server). |
|
|
* |
|
|
* @return |
|
|
* The POSTed message. |
|
|
*/ |
|
|
function mimemail_post() { |
|
|
$message = $_POST['message']; |
|
|
$token = $_POST['token']; |
|
|
$hash = md5(variable_get('mimemail_key', '**') . $message ); |
|
|
|
|
|
if ($hash != $token) { |
|
|
watchdog('access denied', 'Authentication error for POST e-mail', WATCHDOG_WARNING); |
|
|
return drupal_access_denied(); |
|
|
} |
|
|
return mimemail_incoming($message); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Parses an externally received message. |
|
|
* |
|
|
* @param $message |
|
|
* The message to parse. |
|
|
*/ |
|
|
function mimemail_incoming($message) { |
|
|
require_once dirname(__FILE__) .'/mimemail.inc'; |
|
|
$mail = mimemail_parse($message); |
|
|
|
|
|
foreach (module_implements('mimemail_incoming_alter') as $module) { |
|
|
call_user_func_array($module .'_mimemail_incoming_alter', $mail); |
|
|
} |
|
|
|
|
|
module_invoke_all('mimemail_incoming', $mail); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Formats an address string. |
|
|
* TODO could use some enhancement and stress testing. |
|
|
* |
|
|
* @param $address |
|
|
* A user object, a text email address or an array containing name, mail. |
|
|
* @return |
|
|
* A formatted address string or FALSE. |
|
|
*/ |
|
|
function mimemail_address($address) { |
|
|
|
|
|
if (is_array($address)) { |
|
|
|
|
|
// it's an array containing 'mail' and/or 'name' |
|
|
if (isset($address['mail'])) { |
|
|
$output = ''; |
|
|
if (empty($address['name'])) { |
|
|
return $address['mail']; |
|
|
} |
|
|
else { |
|
|
return '"'. addslashes(mime_header_encode($address['name'])) .'" <'. $address['mail'] .'>'; |
|
|
} |
|
|
} |
|
|
|
|
|
// it's an array of address items |
|
|
$addresses = array(); |
|
|
foreach ($address as $a) { |
|
|
$addresses[] = mimemail_address($a); |
|
|
} |
|
|
return $addresses; |
|
|
} |
|
|
|
|
|
// it's a user object |
|
|
if (is_object($address) && isset($address->mail)) { |
|
|
return '"'. addslashes(mime_header_encode($address->name)) .'" <'. $address->mail .'>'; |
|
|
} |
|
|
|
|
|
// it's formatted or unformatted string |
|
|
// TODO shouldn't assume it's valid - should try to re-parse |
|
|
if (is_string($address)) { |
|
|
return $address; |
|
|
} |
|
|
|
|
|
// it's null. return the site default address |
|
|
if (is_null($address)) { |
|
|
return array( |
|
|
'name' => mime_header_encode(variable_get('site_name', 'Drupal')), |
|
|
'mail' => variable_get('site_mail', ini_get('sendmail_from')), |
|
|
); |
|
|
} |
|
|
|
|
|
return FALSE; |
|
|
} |
|
|
|
|
|
/** |
|
| 279 |
* Implementation of hook_theme(). |
* Implementation of hook_theme(). |
| 280 |
*/ |
*/ |
| 281 |
function mimemail_theme() { |
function mimemail_theme() { |
| 282 |
|
$path = drupal_get_path('module', 'mimemail') .'/theme'; |
| 283 |
|
|
| 284 |
return array( |
return array( |
| 285 |
'mimemail_message' => array( |
'mimemail_message' => array( |
| 286 |
'arguments' => array('body' => NULL, 'mailkey' => NULL) |
'arguments' => array('body' => NULL, 'mailkey' => NULL), |
| 287 |
|
'template' => 'mimemail', |
| 288 |
|
'pattern' => 'mimemail__', |
| 289 |
|
'path' => $path, |
| 290 |
) |
) |
| 291 |
); |
); |
| 292 |
} |
} |
| 293 |
|
|
| 294 |
/** |
/** |
| 295 |
* Themes the message body. |
* A preprocess function for theme('mimemail_message'). |
| 296 |
* |
* |
| 297 |
* @param $body |
* The $variables array initially contains the following arguments: |
| 298 |
* The message body to theme. |
* - $body: The message body |
| 299 |
* @param $mailkey |
* - $mailkey: The mailkey associated with the message |
| 300 |
* An identifier for the message. |
* |
| 301 |
* @return |
* See includes/mimemail.tpl.php for additional variables |
|
* The themed HTML message body. |
|
| 302 |
*/ |
*/ |
| 303 |
function theme_mimemail_message($body, $mailkey = NULL) { |
function template_preprocess_mimemail_message(&$variables) { |
| 304 |
//require_once drupal_get_path('module','mimemail') .'/mimemail_css_combine.inc'; |
$theme = variable_get('theme_default', NULL); |
| 305 |
// attempt to include a mail-specific version of the css. |
|
| 306 |
// if you want smaller mail messages, add a mail.css file to your theme |
// Check for the existence of a mail.css file in the current theme folder |
|
$theme = variable_get('theme_default', NULL); |
|
|
|
|
|
// If we have a $mailkey, check for a mailkey-specific style sheet and |
|
|
// set it as a class for <body>. |
|
|
if ($mailkey) { |
|
|
$key = check_plain($mailkey); |
|
|
$key = str_replace(' ','-', trim($key)); |
|
|
print_r($key); die; |
|
|
$styles = drupal_get_path('theme', $theme) .'/mail-'. $key .'.css'; |
|
|
$class = ' class="mail-'. $key .'"'; |
|
|
} |
|
| 307 |
if (!file_exists($styles)) { |
if (!file_exists($styles)) { |
| 308 |
$styles = drupal_get_path('theme', $theme) .'/mail.css'; |
$styles = drupal_get_path('theme', $theme) .'/mail.css'; |
| 309 |
} |
} |
| 310 |
|
|
| 311 |
|
// If no mail.css was found, gather all style sheets |
| 312 |
if (!file_exists($styles)) { |
if (!file_exists($styles)) { |
| 313 |
// embed a version of all style definitions |
// embed a version of all style definitions |
| 314 |
$styles = preg_replace('|<link.*href="'. base_path() .'([^"?]*)[?"].*|', '\1', drupal_get_css()); |
$styles = preg_replace('|<link.*href="'. base_path() |
| 315 |
|
.'([^"?]*)[?"].*|', '\1', drupal_get_css()); |
| 316 |
} |
} |
| 317 |
|
|
| 318 |
|
// Process each style sheet |
| 319 |
foreach (explode("\n", $styles) as $style) { |
foreach (explode("\n", $styles) as $style) { |
| 320 |
if (file_exists($style)) $css .= file_get_contents($style); |
if (file_exists($style)) $css .= file_get_contents($style); |
| 321 |
} |
} |
| 322 |
|
|
| 323 |
$html = '<html><head>'; |
$variables['css'] = str_replace(' ','', str_replace("\n", '', $css)); |
|
$html .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'; |
|
|
|
|
|
if (module_exists('mimemail_css_combiner')) { |
|
|
$css = preg_replace('|\{|',"\n{\n", $css); |
|
|
$css = preg_replace('|\}|',"\n}\n", $css); |
|
|
$html .= '</head><body id="mimemail-body"'. $class .'><div id="center">'; |
|
|
$html .= '<div id="main">'. $body .'</div></div></body></html>'; |
|
|
$html = preg_replace('|</p>|','',$html); |
|
|
$output = new mimemail_css_combiner($html, $css); |
|
|
$output = $output->combine(); |
|
|
} |
|
|
else { |
|
|
$html .= '<style type="text/css"><!--'. $css .'--></style>'; |
|
|
$html .= '</head><body id="mimemail-body"><div id="center">'; |
|
|
$html .= '<div id="main">'. $body .'</div></div></body></html>'; |
|
|
$output = $html; |
|
|
} |
|
|
|
|
|
return $output; // preg_replace('/\s+|\n|\r|^\s|\s$/', ' ', $output); |
|
| 324 |
} |
} |