mimemail.module 12 KB

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