htmlmail.admin.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * @file
  4. * Admin forms for HTML Mail
  5. */
  6. /**
  7. * Implements hook_admin_settings().
  8. */
  9. function htmlmail_admin_settings() {
  10. $formats = array('0' => t('Unfiltered'));
  11. foreach (filter_formats() as $id => $filter) {
  12. $formats[$id] = $filter->name;
  13. }
  14. $form['template'] = array(
  15. '#type' => 'fieldset',
  16. '#title' => t('Step 1'),
  17. '#collapsible' => FALSE,
  18. );
  19. $form['template']['htmlmail_template'] = array(
  20. '#type' => 'fieldset',
  21. '#prefix' => '<strong>' . t('Template file') . ':</strong><br />'
  22. . t('A template file is applied to your message header, subject, and body text. You may copy the <a href="!uri">!template</a> file to your default theme directory and use it to customize your messages.',
  23. array(
  24. '!uri' => url('http://drupalcode.org/project/htmlmail.git/blob_plain/refs/heads/7.x-2.x:/htmlmail.tpl.php'),
  25. '!template' => '<code>htmlmail.tpl.php</code>'
  26. )
  27. ),
  28. '#title' => t('Instructions'),
  29. '#collapsible' => TRUE,
  30. '#collapsed' => TRUE,
  31. );
  32. $form['template']['htmlmail_template']['instructions'] = array(
  33. '#type' => 'item',
  34. '#suffix' => t('!Instructions
  35. <p>When formatting an email message with a given <code>$module</code> and <code>$key</code>, <a href="http://drupal.org/project/htmlmail">HTML Mail</a> will use the first template file it finds from the following list:</p>
  36. <ol style="list-style-type: decimal;">
  37. <li><code>htmlmail--$module--$key.tpl.php</code></li>
  38. <li><code>htmlmail--$module.tpl.php</code></li>
  39. <li><code>htmlmail.tpl.php</code></li>
  40. </ol>
  41. <p>For each filename, <a href="http://drupal.org/project/htmlmail">HTML Mail</a> looks first in the chosen <em>Email theme</em> directory, then in its own module directory, before proceeding to the next filename.</p>
  42. <p>For example, if <code>example_module</code> sends mail with:</p>
  43. <pre>
  44. <code>drupal_mail("example_module", "outgoing_message" ...)
  45. </code>
  46. </pre>
  47. <p>the possible template file names would be:</p>
  48. <ol style="list-style-type: decimal;">
  49. <li><code>htmlmail--example_module--outgoing_message.tpl.php</code></li>
  50. <li><code>htmlmail--example_module.tpl.php</code></li>
  51. <li><code>htmlmail.tpl.php</code></li>
  52. </ol>
  53. <p>Template files are cached, so remember to clear the cache by visiting <u>admin/config/development/performance</u> after changing any <code>.tpl.php</code> files.</p>
  54. <p>The following variables available in this template:</p>
  55. <dl>
  56. <dt><strong><code>$body</code></strong></dt>
  57. <dd>
  58. <p>The message body text.</p>
  59. </dd>
  60. <dt><strong><code>$module</code></strong></dt>
  61. <dd>
  62. <p>The first argument to <a href="http://api.drupal.org/api/drupal/includes--mail.inc/function/drupal_mail/7"><code>drupal_mail()</code></a>, which is, by convention, the machine-readable name of the sending module.</p>
  63. </dd>
  64. <dt><strong><code>$key</code></strong></dt>
  65. <dd>
  66. <p>The second argument to <a href="http://api.drupal.org/api/drupal/includes--mail.inc/function/drupal_mail/7"><code>drupal_mail()</code></a>, which should give some indication of why this email is being sent.</p>
  67. </dd>
  68. <dt><strong><code>$message_id</code></strong></dt>
  69. <dd>
  70. <p>The email message id, which should be equal to <code>"{$module}_{$key}"</code>.</p>
  71. </dd>
  72. <dt><strong><code>$headers</code></strong></dt>
  73. <dd>
  74. <p>An array of email <code>(name =&gt; value)</code> pairs.</p>
  75. </dd>
  76. <dt><strong><code>$from</code></strong></dt>
  77. <dd>
  78. <p>The configured sender address.</p>
  79. </dd>
  80. <dt><strong><code>$to</code></strong></dt>
  81. <dd>
  82. <p>The recipient email address.</p>
  83. </dd>
  84. <dt><strong><code>$subject</code></strong></dt>
  85. <dd>
  86. <p>The message subject line.</p>
  87. </dd>
  88. <dt><strong><code>$body</code></strong></dt>
  89. <dd>
  90. <p>The formatted message body.</p>
  91. </dd>
  92. <dt><strong><code>$language</code></strong></dt>
  93. <dd>
  94. <p>The language object for this message.</p>
  95. </dd>
  96. <dt><strong><code>$params</code></strong></dt>
  97. <dd>
  98. <p>Any module-specific parameters.</p>
  99. </dd>
  100. <dt><strong><code>$template_name</code></strong></dt>
  101. <dd>
  102. <p>The basename of the active template.</p>
  103. </dd>
  104. <dt><strong><code>$template_path</code></strong></dt>
  105. <dd>
  106. <p>The relative path to the template directory.</p>
  107. </dd>
  108. <dt><strong><code>$template_url</code></strong></dt>
  109. <dd>
  110. <p>The absolute URL to the template directory.</p>
  111. </dd>
  112. <dt><strong><code>$theme</code></strong></dt>
  113. <dd>
  114. <p>The name of the <em>Email theme</em> used to hold template files. If the <a href="http://drupal.org/project/echo">Echo</a> module is enabled this theme will also be used to transform the message body into a fully-themed webpage.</p>
  115. </dd>
  116. <dt><strong><code>$theme_path</code></strong></dt>
  117. <dd>
  118. <p>The relative path to the selected <em>Email theme</em> directory.</p>
  119. </dd>
  120. <dt><strong><code>$theme_url</code></strong></dt>
  121. <dd>
  122. <p>The absolute URL to the selected <em>Email theme</em> directory.</p>
  123. </dd>
  124. <dt><strong><code>$debug</code></strong></dt>
  125. <dd>
  126. <p><code>TRUE</code> to add some useful debugging info to the bottom of the message.</p>
  127. </dd>
  128. </dl>
  129. <p>Other modules may also add or modify theme variables by implementing a <code>MODULENAME_preprocess_htmlmail(&amp;$variables)</code> <a href="http://api.drupal.org/api/drupal/modules--system--theme.api.php/function/hook_preprocess_HOOK/7">hook function</a>.</p>',
  130. array('!Instructions' => '')
  131. ),
  132. );
  133. $form['template']['htmlmail_debug'] = array(
  134. '#type' => 'checkbox',
  135. '#prefix' => '<br />',
  136. '#title' => '<em>' . t('(Optional)') . '</em> ' . t('Debug'),
  137. '#default_value' => variable_get('htmlmail_debug', '0'),
  138. '#description' => t('Add debugging info (Set <code>$debug</code> to <code>TRUE</code>).'),
  139. );
  140. $form['theme'] = array(
  141. '#type' => 'fieldset',
  142. '#title' => t('Step 2'),
  143. '#collapsible' => FALSE,
  144. );
  145. $form['theme']['htmlmail_theme'] = array(
  146. '#type' => 'select',
  147. '#title' => t('Email theme'),
  148. '#default_value' => variable_get('htmlmail_theme', ''),
  149. '#options' => htmlmail_get_allowed_themes(),
  150. '#suffix' => '<p>'
  151. . t('Choose the theme that will hold your customized templates from Step 1 above.')
  152. . '</p><p>'
  153. . (module_exists('echo') ?
  154. t('The templated text will be styled by your chosen theme. This lets you use any one of <a href="!themes">over 800</a> themes to style your messages. Creating an email-specific sub-theme lets you use the full power of the <a href="!theme_system">drupal theme system</a> to format your messages.',
  155. array(
  156. '!themes' => 'http://drupal.org/project/themes',
  157. '!theme_system' => 'http://drupal.org/documentation/theme',
  158. )
  159. ) :
  160. t('If you install and enable the <a href="!echo">Echo</a> module, the theme you select will also be used to style your messages as if they were pages on your website.',
  161. array(
  162. '!echo' => 'http://drupal.org/project/echo'
  163. )
  164. )
  165. )
  166. . '</p><p>'
  167. . (module_exists('mailmime') ?
  168. t('Since you have the <a href="!mailmime">Mail MIME</a> module installed, your images will be automatically converted to inline attachments, and a plain-text alternative will be available to recipients who prefer it.',
  169. array('!mailmime' => 'http://drupal.org/project/mailmime')
  170. ) :
  171. t('If you install the <a href="!mailmime">Mail MIME</a> module, images in your emails will be automatically converted to inline attachments, and a plain-text alternative will be made available. This prevents your recipients from seeing broken image links and scary security warnings when they don\'t have the sender\'s address in their email addressbook. <a href="!mailmime">Mail MIME</a> also allows <a href="!htmlmail">HTML Mail</a> to handle MIME-formatted messages sent by other modules such as <a href="!print">Send by-email</a>.',
  172. array(
  173. '!mailmime' => 'http://drupal.org/project/mailmime',
  174. '!print' => 'http://drupal.org/project/print',
  175. )
  176. )
  177. )
  178. . '</p>',
  179. );
  180. $form['filter'] = array(
  181. '#type' => 'fieldset',
  182. '#title' => t('Step 3'),
  183. '#collapsible' => FALSE,
  184. );
  185. $form['filter']['htmlmail_postfilter'] = array(
  186. '#type' => 'select',
  187. '#title' => t('Post-filtering'),
  188. '#default_value' => variable_get('htmlmail_postfilter', ''),
  189. '#options' => $formats,
  190. '#suffix' => '<p>'
  191. . t('You may choose a <a href="!formats">text format</a> to be used for filtering email messages <em>after</em> theming. This allows you to use any combination of <a href="!filters">over 200 filter modules</a> to make final changes to your message before sending.',
  192. array(
  193. '!formats' => url('admin/config/content/formats'),
  194. '!filters' => url('http://drupal.org/project/modules/?filters=type%3Aproject_project%20tid%3A63%20hash%3A1hbejm%20-bs_project_sandbox%3A1%20bs_project_has_releases%3A1'),
  195. )
  196. )
  197. . '</p><p>'
  198. . t('Here is a recommended configuration:')
  199. . '</p><ul><li><dl><dt>'
  200. . t('<a href="!emogrifier">Emogrifier</a>',
  201. array('!emogrifier' => url('http://drupal.org/project/emogrifier'))
  202. )
  203. . '</dt><dd>'
  204. . t('Converts stylesheets to inline style rules for consistent display on mobile devices and webmail.')
  205. . '</dd></dl></li><li><dl><dt>'
  206. . t('<a href="!transliteration">Transliteration</a>',
  207. array('!transliteration' => url('http;//drupal.org/project/filter_transliteration'))
  208. )
  209. . '</dt><dd>'
  210. . t('Converts non-ASCII text to US-ASCII equivalents. This helps prevent Microsoft <q>smart-quotes</q> from appearing as question-marks in Mozilla Thunderbird.'
  211. )
  212. . '</dd></dl></li><li><dl><dt>'
  213. . t('<a href="!pathologic">Pathologic</a>',
  214. array('!pathologic' => url('http://drupal.org/project/pathologic'))
  215. )
  216. . '</dt><dd>'
  217. . t('Converts relative URLS to absolute URLS so that clickable links in your message will work as intended.')
  218. . '</dd></dl></ul>'
  219. );
  220. return system_settings_form($form);
  221. }
  222. /**
  223. * Builds a form for sending a test message.
  224. */
  225. function htmlmail_test_form($form_values = NULL) {
  226. $defaults = variable_get(
  227. 'htmlmail_test',
  228. array(
  229. 'to' => variable_get('site_mail', 'user@example.com'),
  230. 'subject' => 'test',
  231. 'body' => array(
  232. 'value' => 'test',
  233. ),
  234. )
  235. );
  236. $defaults['body']['format'] = filter_fallback_format();
  237. $form['to'] = array(
  238. '#type' => 'textfield',
  239. '#title' => t('To'),
  240. '#default_value' => $defaults['to'],
  241. '#maxlength' => 128,
  242. '#required' => TRUE,
  243. );
  244. $form['subject'] = array(
  245. '#type' => 'textfield',
  246. '#title' => t('Subject'),
  247. '#default_value' => $defaults['subject'],
  248. '#maxlength' => 128,
  249. '#required' => TRUE,
  250. );
  251. $form['body'] = array(
  252. '#type' => 'text_format',
  253. '#title' => t('Body'),
  254. '#rows' => 20,
  255. '#default_value' => $defaults['body']['value'],
  256. '#format' => $defaults['body']['format'],
  257. '#required' => TRUE,
  258. );
  259. $mailsystem = mailsystem_get();
  260. if (empty($mailsystem['htmlmail'])) {
  261. $mailsystem['htmlmail'] = 'HTMLMailSystem';
  262. }
  263. $form['class'] = array(
  264. '#type' => 'select',
  265. '#title' => t('Test mail sending class'),
  266. '#default_value' => $mailsystem['htmlmail'],
  267. '#options' => array_combine(mailsystem_get_classes(), mailsystem_get_classes()),
  268. '#description' => 'Select the MailSystemInterface implementation to be tested.',
  269. );
  270. $form['submit'] = array(
  271. '#type' => 'submit',
  272. '#value' => t('Send test message'),
  273. );
  274. return $form;
  275. }
  276. /**
  277. * Sends the test messsage and saves the contents for re-use.
  278. */
  279. function htmlmail_test_form_submit($form, &$form_state) {
  280. // Get the form values.
  281. $defaults = array(
  282. 'to' => $form_state['values']['to'],
  283. 'subject' => $form_state['values']['subject'],
  284. 'body' => $form_state['values']['body'],
  285. );
  286. // Set the defaults for reuse.
  287. variable_set('htmlmail_test', $defaults);
  288. // Set the mail sending class.
  289. mailsystem_set(array('htmlmail' => $form_state['values']['class']));
  290. // Send the email.
  291. $params = array(
  292. 'subject' => $form_state['values']['subject'],
  293. 'body' => check_markup(
  294. $form_state['values']['body']['value'],
  295. $form_state['values']['body']['format']
  296. ),
  297. );
  298. if (
  299. drupal_mail(
  300. 'htmlmail',
  301. 'test',
  302. $form_state['values']['to'],
  303. language_default(),
  304. $params
  305. )
  306. ) {
  307. drupal_set_message(t('HTML Mail test message sent.'));
  308. }
  309. }