mimemail.module 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. /**
  3. * @file
  4. * Component module for sending Mime-encoded emails.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function mimemail_menu() {
  10. $path = drupal_get_path('module', 'mimemail') . '/includes';
  11. // Configuration links.
  12. $items['admin/config/system/mimemail'] = array(
  13. 'title' => 'Mime Mail',
  14. 'description' => 'Manage mime mail system settings.',
  15. 'page callback' => 'drupal_get_form',
  16. 'page arguments' => array('mimemail_admin_settings'),
  17. 'access arguments' => array('administer site configuration'),
  18. 'file' => 'mimemail.admin.inc',
  19. 'file path' => $path,
  20. );
  21. $items['mimemail'] = array(
  22. 'page callback' => 'mimemail_post',
  23. 'access callback' => 'mimemail_incoming_access',
  24. 'type' => MENU_CALLBACK,
  25. 'file' => 'mimemail.incoming.inc',
  26. 'file path' => $path,
  27. );
  28. return $items;
  29. }
  30. /**
  31. * Implements hook_permission().
  32. */
  33. function mimemail_permission() {
  34. return array(
  35. 'edit mimemail user settings' => array(
  36. 'title' => t('Edit Mime Mail user settings'),
  37. 'description' => t('Edit user specific settings for Mime Mail.'),
  38. ),
  39. );
  40. }
  41. /**
  42. * Access callback to process incoming messages.
  43. */
  44. function mimemail_incoming_access() {
  45. return variable_get('mimemail_incoming', FALSE);
  46. }
  47. /**
  48. * Implements hook_field_extra_fields().
  49. */
  50. function mimemail_field_extra_fields() {
  51. $extra['user']['user'] = array(
  52. 'form' => array(
  53. 'mimemail' => array(
  54. 'label' => t('Email'),
  55. 'description' => t('Mime Mail module settings form elements.'),
  56. 'weight' => 0,
  57. ),
  58. ),
  59. 'display' => array(
  60. 'mimemail' => array(
  61. 'label' => t('Email'),
  62. 'description' => t('Mime Mail module settings form elements.'),
  63. 'weight' => 0,
  64. ),
  65. ),
  66. );
  67. return $extra;
  68. }
  69. /**
  70. * Implements hook_user_view().
  71. */
  72. function mimemail_user_view($account, $view_mode, $langcode) {
  73. $account->content['mimemail'] = array(
  74. '#type' => 'user_profile_category',
  75. '#title' => t('Email'),
  76. );
  77. $account->content['mimemail']['textonly'] = array(
  78. '#type' => 'user_profile_item',
  79. '#title' => t('Plaintext email only'),
  80. '#markup' => empty($account->data['mimemail_textonly']) ? t('No') : t('Yes'),
  81. );
  82. }
  83. /**
  84. * Implements hook_form_FORM_ID_alter().
  85. *
  86. * Adds the Mime Mail settings on the user settings page.
  87. */
  88. function mimemail_form_user_profile_form_alter(&$form, &$form_state) {
  89. if ($form['#user_category'] == 'account') {
  90. $account = $form['#user'];
  91. $form['mimemail'] = array(
  92. '#type' => 'fieldset',
  93. '#title' => t('Email settings'),
  94. '#weight' => 5,
  95. '#collapsible' => TRUE,
  96. '#access' => user_access('edit mimemail user settings'),
  97. );
  98. $form['mimemail']['mimemail_textonly'] = array(
  99. '#type' => 'checkbox',
  100. '#title' => t('Plaintext email only'),
  101. '#default_value' => !empty($account->data['mimemail_textonly']) ? $account->data['mimemail_textonly'] : FALSE,
  102. '#description' => t('Check this option if you do not wish to receive email messages with graphics and styles.'),
  103. );
  104. }
  105. }
  106. /**
  107. * Implements hook_user_presave().
  108. */
  109. function mimemail_user_presave(&$edit, $account, $category) {
  110. $edit['data']['mimemail_textonly'] = isset($edit['mimemail_textonly']) ? $edit['mimemail_textonly'] : 0;
  111. }
  112. /**
  113. * Implements hook_theme().
  114. */
  115. function mimemail_theme() {
  116. module_load_include('inc', 'mimemail', 'theme/mimemail.theme');
  117. return mimemail_theme_theme();
  118. }
  119. /**
  120. * Implements hook_mail().
  121. */
  122. function mimemail_mail($key, &$message, $params) {
  123. $context = $params['context'];
  124. // Prepare the array of the attachments.
  125. $attachments = array();
  126. $attachments_string = trim($params['attachments']);
  127. if (!empty($attachments_string)) {
  128. $attachment_lines = array_filter(explode("\n", trim($attachments_string)));
  129. foreach ($attachment_lines as $filepath) {
  130. $attachments[] = array(
  131. 'filepath' => trim($filepath),
  132. );
  133. }
  134. }
  135. // We handle different address headers if set.
  136. $address_headers = array(
  137. 'cc' => 'Cc',
  138. 'bcc' => 'Bcc',
  139. 'reply-to' => 'Reply-to',
  140. );
  141. foreach ($address_headers as $param_key => $address_header) {
  142. $params[$param_key] = empty($params[$param_key]) ? array() : explode(',', $params[$param_key]);
  143. if (!empty($params[$param_key])) {
  144. foreach ($params[$param_key] as $key => $address) {
  145. $params[$param_key][$key] = token_replace($address, $context);
  146. }
  147. $message['headers'][$address_header] = implode(',', $params[$param_key]);
  148. }
  149. }
  150. $message['to'] = token_replace($message['to'], $context);
  151. $message['subject'] = token_replace($context['subject'], $context);
  152. $message['body'][] = token_replace($context['body'], $context);
  153. $message['params']['plaintext'] = token_replace($params['plaintext'], $context);
  154. $message['params']['attachments'] = $attachments;
  155. }
  156. /**
  157. * Retreives a list of all available mailer engines.
  158. *
  159. * @return array
  160. * Mailer engine names.
  161. */
  162. function mimemail_get_engines() {
  163. $engines = array();
  164. foreach (module_implements('mailengine') as $module) {
  165. $engines[$module] = module_invoke($module, 'mailengine', 'list');
  166. }
  167. return $engines;
  168. }
  169. /**
  170. * Implements hook_mailengine().
  171. *
  172. * @param string $op
  173. * The operation to perform on the message.
  174. * @param array $message
  175. * The message to perform the operation on.
  176. *
  177. * @return boolean
  178. * Returns TRUE if the operation was successful or FALSE if it was not.
  179. */
  180. function mimemail_mailengine($op, $message = array()) {
  181. module_load_include('inc', 'mimemail');
  182. switch ($op) {
  183. case 'list':
  184. $engine = array(
  185. 'name' => t('Mime Mail'),
  186. 'description' => t("Default mailing engine."),
  187. );
  188. return $engine;
  189. case 'settings':
  190. // Not implemented.
  191. break;
  192. case 'multiple':
  193. case 'single':
  194. case 'send':
  195. // Default values.
  196. $default = array(
  197. 'to' => '',
  198. 'subject' => '',
  199. 'body' => '',
  200. 'from' => '',
  201. 'headers' => ''
  202. );
  203. $message = array_merge($default, $message);
  204. // If 'Return-Path' isn't already set in php.ini, we pass it separately
  205. // as an additional parameter instead of in the header.
  206. // However, if PHP's 'safe_mode' is on, this is not allowed.
  207. if (isset($message['headers']['Return-Path']) && !ini_get('safe_mode')) {
  208. $return_path_set = strpos(ini_get('sendmail_path'), ' -f');
  209. if (!$return_path_set) {
  210. $return_path = trim($message['headers']['Return-Path'], '<>');
  211. unset($message['headers']['Return-Path']);
  212. }
  213. }
  214. $crlf = variable_get('mimemail_crlf', MAIL_LINE_ENDINGS);
  215. $recipients = (!is_array($message['to'])) ? array($message['to']) : $message['to'];
  216. $subject = mime_header_encode($message['subject']);
  217. $body = preg_replace('@\r?\n@', $crlf, $message['body']);
  218. $headers = mimemail_rfc_headers($message['headers']);
  219. $result = TRUE;
  220. foreach ($recipients as $to) {
  221. if (isset($return_path) && !empty($return_path)) {
  222. if (isset($_SERVER['WINDIR']) || strpos($_SERVER['SERVER_SOFTWARE'], 'Win32') !== FALSE) {
  223. // On Windows, PHP will use the value of sendmail_from for the
  224. // Return-Path header.
  225. $old_from = ini_get('sendmail_from');
  226. ini_set('sendmail_from', $return_path);
  227. $result = @mail($to, $subject, $body, $headers) && $result;
  228. ini_set('sendmail_from', $old_from);
  229. }
  230. else {
  231. // On most non-Windows systems, the "-f" option to the sendmail command
  232. // is used to set the Return-Path.
  233. $result = @mail($to, $subject, $body, $headers, '-f' . $return_path) && $result;
  234. }
  235. }
  236. else {
  237. // The optional $additional_parameters argument to mail() is not allowed
  238. // if safe_mode is enabled. Passing any value throws a PHP warning and
  239. // makes mail() return FALSE.
  240. $result = @mail($to, $subject, $body, $headers) && $result;
  241. }
  242. }
  243. return $result;
  244. }
  245. return FALSE;
  246. }
  247. /**
  248. * Prepares the message for sending.
  249. *
  250. * @param array $message
  251. * An array containing the message data. The optional parameters are:
  252. * - plain: Whether to send the message as plaintext only or HTML. If evaluates to TRUE,
  253. * then the message will be sent as plaintext.
  254. * - plaintext: Optional plaintext portion of a multipart email.
  255. * - attachments: An array of arrays which describe one or more attachments.
  256. * Existing files can be added by path, dinamically-generated files
  257. * can be added by content. The internal array contains the following elements:
  258. * - filepath: Relative Drupal path to an existing file (filecontent is NULL).
  259. * - filecontent: The actual content of the file (filepath is NULL).
  260. * - filename: The filename of the file.
  261. * - filemime: The MIME type of the file.
  262. * The array of arrays looks something like this:
  263. * Array
  264. * (
  265. * [0] => Array
  266. * (
  267. * [filepath] => '/sites/default/files/attachment.txt'
  268. * [filecontent] => 'My attachment.'
  269. * [filename] => 'attachment.txt'
  270. * [filemime] => 'text/plain'
  271. * )
  272. * )
  273. *
  274. * @return array
  275. * All details of the message.
  276. */
  277. function mimemail_prepare_message($message) {
  278. module_load_include('inc', 'mimemail');
  279. $module = $message['module'];
  280. $key = $message['key'];
  281. $to = $message['to'];
  282. $from = $message['from'];
  283. $subject = $message['subject'];
  284. $body = $message['body'];
  285. $headers = isset($message['params']['headers']) ? $message['params']['headers'] : array();
  286. $plain = isset($message['params']['plain']) ? $message['params']['plain'] : NULL;
  287. $plaintext = isset($message['params']['plaintext']) ? $message['params']['plaintext'] : NULL;
  288. $attachments = isset($message['params']['attachments']) ? $message['params']['attachments'] : array();
  289. $site_name = variable_get('site_name', 'Drupal');
  290. $site_mail = variable_get('site_mail', ini_get('sendmail_from'));
  291. $simple_address = variable_get('mimemail_simple_address', 0);
  292. // Override site mails default sender when using default engine.
  293. if ((empty($from) || $from == $site_mail)
  294. && variable_get('mimemail_engine', 'mimemail') == 'mimemail') {
  295. $mimemail_name = variable_get('mimemail_name', $site_name);
  296. $mimemail_mail = variable_get('mimemail_mail', $site_mail);
  297. $from = array(
  298. 'name' => !empty($mimemail_name) ? $mimemail_name : $site_name,
  299. 'mail' => !empty($mimemail_mail) ? $mimemail_mail : $site_mail,
  300. );
  301. }
  302. // Body is empty, this is a plaintext message.
  303. if (empty($body)) {
  304. $plain = TRUE;
  305. }
  306. // Try to determine recipient's text mail preference.
  307. elseif (is_null($plain)) {
  308. if (is_object($to) && isset($to->data['mimemail_textonly'])) {
  309. $plain = $to->data['mimemail_textonly'];
  310. }
  311. elseif (is_string($to) && valid_email_address($to)) {
  312. if (is_object($account = user_load_by_mail($to)) && isset($account->data['mimemail_textonly'])) {
  313. $plain = $account->data['mimemail_textonly'];
  314. // Might as well pass the user object to the address function.
  315. $to = $account;
  316. }
  317. }
  318. }
  319. $subject = str_replace(array(" \n", "\n"), '', trim(drupal_html_to_text($subject)));
  320. $hook = array(
  321. 'mimemail_message__' . $key,
  322. 'mimemail_message__' . $module .'__'. $key,
  323. );
  324. $variables = array(
  325. 'module' => $module,
  326. 'key' => $key,
  327. 'recipient' => $to,
  328. 'subject' => $subject,
  329. 'body' => $body
  330. );
  331. $body = theme($hook, $variables);
  332. foreach (module_implements('mail_post_process') as $module) {
  333. $function = $module . '_mail_post_process';
  334. $function($body, $key);
  335. }
  336. $plain = $plain || variable_get('mimemail_textonly', 0);
  337. $from = mimemail_address($from);
  338. $mail = mimemail_html_body($body, $subject, $plain, $plaintext, $attachments);
  339. $headers = array_merge($message['headers'], $headers, $mail['headers']);
  340. $message['to'] = mimemail_address($to, $simple_address);
  341. $message['from'] = $from;
  342. $message['subject'] = $subject;
  343. $message['body'] = $mail['body'];
  344. $message['headers'] = mimemail_headers($headers, $from);
  345. return $message;
  346. }