mimemail.module 13 KB

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