htmlmail.module 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * @file
  4. * Sends system emails in HTML.
  5. */
  6. /**
  7. * Implements hook_permission().
  8. *
  9. * Defines a permission for setting the per-user plaintext option.
  10. */
  11. function htmlmail_permission() {
  12. $args = array(
  13. '!htmlmail' => url('http://drupal.org/project/htmlmail'),
  14. '%htmlmail' => 'HTML Mail',
  15. );
  16. return array(
  17. 'choose htmlmail_plaintext' => array(
  18. 'title' => t('Choose to receive plaintext emails via %htmlmail', $args),
  19. 'description' => t(
  20. 'Granting this permission allows users to choose whether to receive all their emails in plaintext, rather than the default format provided by the <a href="!htmlmail">%htmlmail</a> module.', $args
  21. ),
  22. ),
  23. );
  24. }
  25. /**
  26. * Implements hook_help().
  27. */
  28. function htmlmail_help($path, $arg) {
  29. switch ($path) {
  30. case 'admin/config/system/htmlmail':
  31. return '<h2>' . t('Theming') . '</h2><p>' . t('The email message goes through three transformations before sending:') . '</p>';
  32. case 'admin/help#htmlmail':
  33. return '<p>'
  34. . t('<a href="!htmlmail">HTML Mail</a> lets you theme your messages the same way you theme the rest of your website.',
  35. array('!htmlmail' => 'http://drupal.org/project/htmlmail')
  36. ) . '</p>';
  37. default:
  38. return '';
  39. }
  40. }
  41. /**
  42. * Implements hook_menu().
  43. */
  44. function htmlmail_menu() {
  45. $items['admin/config/system/htmlmail'] = array(
  46. 'title' => 'HTML Mail',
  47. 'description' => 'Configure HTML Mail system-wide settings.',
  48. 'page callback' => 'drupal_get_form',
  49. 'page arguments' => array('htmlmail_admin_settings'),
  50. 'access arguments' => array('administer site configuration'),
  51. 'file' => 'htmlmail.admin.inc',
  52. );
  53. $items['admin/config/system/htmlmail/settings'] = array(
  54. 'title' => 'Settings',
  55. 'type' => MENU_DEFAULT_LOCAL_TASK,
  56. 'weight' => '-2'
  57. );
  58. $items['admin/config/system/htmlmail/test'] = array(
  59. 'title' => 'Send Test',
  60. 'page callback' => 'drupal_get_form',
  61. 'page arguments' => array('htmlmail_test_form'),
  62. 'access arguments' => array('access administration pages'),
  63. 'type' => MENU_LOCAL_TASK,
  64. 'file' => 'htmlmail.admin.inc',
  65. );
  66. return $items;
  67. }
  68. /**
  69. * Implements hook_theme().
  70. *
  71. * Auto-detects htmlmail template files in the selected theme and in the
  72. * htmlmail module directory.
  73. */
  74. function htmlmail_theme() {
  75. $items = array();
  76. $module_path = drupal_get_path('module', 'htmlmail');
  77. $pattern = '/^htmlmail.*\.tpl\.php$/';
  78. $files = file_scan_directory($module_path, $pattern, array('key' => 'name'));
  79. if ($theme = htmlmail_get_selected_theme()) {
  80. $theme_path = drupal_get_path('theme', $theme);
  81. $files = array_merge($files,
  82. file_scan_directory($theme_path, $pattern, array('key' => 'name'))
  83. );
  84. }
  85. else {
  86. $theme_path = $module_path;
  87. }
  88. ksort($files);
  89. foreach ($files as $file) {
  90. $path = dirname($file->uri);
  91. $template = substr($file->name, 0, -4);
  92. $suggestion = str_replace('--', '__', $template);
  93. $items[$suggestion] = array(
  94. 'variables' => array('message' => array()),
  95. 'template' => $template,
  96. 'path' => $path,
  97. 'theme path' => $theme_path,
  98. );
  99. }
  100. return $items;
  101. }
  102. /**
  103. * Process variables to format email messages.
  104. *
  105. * @see htmlmail.tpl.php
  106. */
  107. function template_preprocess_htmlmail(array &$variables) {
  108. $variables['debug'] = variable_get('htmlmail_debug', '0');
  109. $variables['theme'] = htmlmail_get_selected_theme($variables);
  110. $variables['module_path'] = drupal_get_path('module', 'htmlmail');
  111. if (empty($variables['theme'])) {
  112. $variables['theme'] = 'no theme';
  113. $variables['theme_path'] = $variables['module_path'];
  114. }
  115. else {
  116. $variables['theme_path'] = drupal_get_path('theme', $variables['theme']);
  117. }
  118. $variables['theme_url'] = url(
  119. $variables['theme_path'], array('absolute' => TRUE)
  120. );
  121. $variables['message_id'] = $variables['module'] . '_' . $variables['key'];
  122. $suggestion = 'htmlmail__' . $variables['module'];
  123. $variables['theme_hook_suggestions'][] = $suggestion;
  124. $suggestion .= '__' . $variables['key'];
  125. $variables['theme_hook_suggestions'][] = $suggestion;
  126. }
  127. /**
  128. * Implements hook_mail().
  129. */
  130. function htmlmail_mail($key, &$message, $params) {
  131. $message['module'] = 'htmlmail';
  132. $message['key'] = $key;
  133. $message['subject'] = $params['subject'];
  134. $message['body'] = explode(
  135. MAIL_LINE_ENDINGS . MAIL_LINE_ENDINGS,
  136. $params['body']
  137. );
  138. return $message;
  139. }
  140. /**
  141. * Implements hook_form_FORM_ID_alter().
  142. */
  143. function htmlmail_form_user_profile_form_alter(&$form, &$form_state) {
  144. if ($form['#user_category'] != 'account') {
  145. return;
  146. }
  147. if (!(user_access('choose htmlmail_plaintext') || user_access('administer users'))) {
  148. return;
  149. }
  150. $account = $form['#user'];
  151. $mail = $form['account']['mail'];
  152. $form['account']['mail'] = array(
  153. 'mail' => $mail,
  154. 'htmlmail_plaintext' => array(
  155. '#type' => 'checkbox',
  156. '#title' => t('Plaintext-only emails'),
  157. '#default_value' => empty($account->data['htmlmail_plaintext']) ? 0 : 1,
  158. '#description' => t('The %htmlmail module can send emails with fonts, styles, and other HTML formatting. If you prefer to receive all your emails in unformatted plain text, select this option.',
  159. array('%htmlmail' => 'HTML Mail')
  160. ),
  161. ),
  162. );
  163. }
  164. /**
  165. * Implements hook_user_presave().
  166. */
  167. function htmlmail_user_presave(&$edit, $account, $category) {
  168. if (user_access('choose htmlmail_plaintext') || user_access('administer users')) {
  169. $edit['data']['htmlmail_plaintext'] = empty($edit['htmlmail_plaintext']) ? 0 : 1;
  170. unset($edit['htmlmail_plaintext']);
  171. }
  172. }
  173. /**
  174. * Returns an associative array of allowed themes. The keys are the
  175. * machine-readable names and the values are the .info file names.
  176. * Based on code from the og_theme module.
  177. */
  178. function &htmlmail_get_allowed_themes() {
  179. $allowed = &drupal_static(__FUNCTION__);
  180. if (!isset($allowed)) {
  181. $allowed = array('' => t('No theme'));
  182. $themes = list_themes();
  183. module_load_include('inc', 'system', 'system.admin');
  184. uasort($themes, 'system_sort_modules_by_info_name');
  185. foreach ($themes as $key => $value) {
  186. if ($value->status) {
  187. $allowed[$key] = check_plain($value->info['name']);
  188. }
  189. }
  190. }
  191. return $allowed;
  192. }
  193. /**
  194. * Returns the selected theme to use for outgoing emails.
  195. */
  196. function htmlmail_get_selected_theme(&$message = array()) {
  197. $selected = isset($message['theme'])
  198. ? $message['theme'] : variable_get('htmlmail_theme', '');
  199. if ($selected) {
  200. // Make sure the selected theme is allowed.
  201. $themes = &htmlmail_get_allowed_themes();
  202. if (empty($themes[$selected])) {
  203. $selected = '';
  204. }
  205. }
  206. return $selected;
  207. }
  208. /**
  209. * Checks whether a given recipient email prefers plaintext-only messages.
  210. *
  211. * @param $email
  212. * The recipient email address.
  213. *
  214. * @return
  215. * FALSE if the recipient prefers plaintext-only messages; otherwise TRUE.
  216. */
  217. function htmlmail_is_allowed($email) {
  218. return !($recipient = user_load_by_mail($email))
  219. || empty($recipient->data['htmlmail_plaintext']);
  220. }