mailgun.admin.inc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * @file
  4. * Administration page callbacks for Mailgun.
  5. */
  6. /**
  7. * Menu callback: displays the Mailgun module settings page.
  8. *
  9. * @param array $form
  10. * Form render array.
  11. * @param array $form_state
  12. * Array containing form state values.
  13. *
  14. * @return array
  15. * An array containing form items to place on the module settings page.
  16. */
  17. function mailgun_admin_settings(array $form, array &$form_state) {
  18. // Check if the Mailgun PHP library is installed.
  19. if (!mailgun_check_library()) {
  20. drupal_set_message(t('The Mailgun PHP library is not installed. Please see Installation section in the !link.', array(
  21. '!link' => l(t('documentation'), MAILGUN_DOCUMENTATION_LINK),
  22. )), 'error');
  23. }
  24. $key = variable_get('mailgun_api_key', '');
  25. $form['mailgun_api_key'] = array(
  26. '#title' => t('Mailgun API key'),
  27. '#type' => 'textfield',
  28. '#description' => t('Get your Secret API key from the !link.', array(
  29. '!link' => l(t('Mailgun dashboard'), MAILGUN_DASHBOARD_LINK),
  30. )),
  31. '#default_value' => $key,
  32. '#required' => TRUE,
  33. );
  34. $client = FALSE;
  35. if (!empty($key)) {
  36. try {
  37. $client = mailgun_get_client($key);
  38. }
  39. catch (Exception $e) {
  40. watchdog('mailgun', 'An exception occurred. @code: @message', array(
  41. '@code' => $e->getCode(),
  42. '@message' => $e->getMessage(),
  43. ), WATCHDOG_WARNING, MAILGUN_ADMIN_PAGE);
  44. drupal_set_message(t('Mailgun: %message', array('%message' => $e->getMessage())), 'error');
  45. }
  46. }
  47. // Display settings only when a valid API key is present and client is active.
  48. if ($client) {
  49. $domain_options = array(
  50. '_sender' => t('Get domain from sender address'),
  51. );
  52. $result = $client->domains()->index();
  53. if (!empty($result)) {
  54. foreach ($result->getDomains() as $domain) {
  55. $domain_options[$domain->getName()] = $domain->getName();
  56. }
  57. }
  58. $form['mailgun_domain'] = array(
  59. '#title' => t('Domain'),
  60. '#type' => 'select',
  61. '#options' => $domain_options,
  62. '#description' => t('Mails will be sent using this domain.'),
  63. '#default_value' => variable_get('mailgun_domain', '_sender'),
  64. );
  65. $form['mailgun_test'] = array(
  66. '#title' => t('Test mode'),
  67. '#type' => 'checkbox',
  68. '#default_value' => variable_get('mailgun_test', FALSE),
  69. '#description' => t('Mailgun will accept the message but will not send it. This is useful for testing purposes.'),
  70. );
  71. $form['mailgun_log'] = array(
  72. '#title' => t('Log mails'),
  73. '#type' => 'checkbox',
  74. '#description' => t('Log all mails sent through Mailgun. Messages fail to send will be logged regardless of this setting.'),
  75. '#default_value' => variable_get('mailgun_log', FALSE),
  76. );
  77. $form['extra'] = array(
  78. '#type' => 'fieldset',
  79. '#title' => t('Additional settings'),
  80. '#description' => t('These default settings apply to messages sent using Mailgun and may be overriden on a per-message basis.'),
  81. '#collapsible' => TRUE,
  82. '#collapsed' => TRUE,
  83. );
  84. // We have the same options for all settings.
  85. $options = array(
  86. 'default' => t('Use default setting'),
  87. 'enabled' => t('Enabled'),
  88. 'disabled' => t('Disabled'),
  89. );
  90. $form['extra']['tracking'] = array(
  91. '#type' => 'fieldset',
  92. '#title' => t('Tracking'),
  93. );
  94. $form['extra']['tracking']['mailgun_tracking'] = array(
  95. '#title' => t('Enable tracking'),
  96. '#type' => 'select',
  97. '#options' => $options,
  98. '#description' => t('Whether to enable event tracking by default or not. See !link for details.', array(
  99. '!link' => l(t('Tracking Messages'), 'https://documentation.mailgun.com/user_manual.html#tracking-messages'),
  100. )),
  101. '#default_value' => variable_get('mailgun_tracking', 'default'),
  102. );
  103. $form['extra']['tracking']['mailgun_tracking_clicks'] = array(
  104. '#title' => t('Enable click tracking'),
  105. '#type' => 'select',
  106. '#options' => $options,
  107. '#description' => t('Whether to enable click tracking by default or not.'),
  108. '#default_value' => variable_get('mailgun_tracking_clicks', 'default'),
  109. );
  110. $form['extra']['tracking']['mailgun_tracking_opens'] = array(
  111. '#title' => t('Enable open tracking'),
  112. '#type' => 'select',
  113. '#options' => $options,
  114. '#description' => t('Whether to enable open tracking by default or not.'),
  115. '#default_value' => variable_get('mailgun_tracking_opens', 'default'),
  116. );
  117. $formats = array('_none' => t('- None -'));
  118. foreach (filter_formats() as $format) {
  119. if ($format->format === 'php_code') {
  120. continue;
  121. }
  122. $formats[$format->format] = $format->name;
  123. }
  124. $form['extra']['mailgun_format'] = array(
  125. '#title' => t('Text format'),
  126. '#type' => 'select',
  127. '#description' => t('Specify an additional text format to filter the message through before sending the email.'),
  128. '#options' => $formats,
  129. '#default_value' => variable_get('mailgun_format', '_none'),
  130. );
  131. $form['extra']['mailgun_queue'] = array(
  132. '#title' => t('Queue mails'),
  133. '#type' => 'checkbox',
  134. '#description' => t('Mails will be queued and sent during cron runs. Useful for sending a large number of emails.'),
  135. '#default_value' => variable_get('mailgun_queue', FALSE),
  136. );
  137. $form['extra']['mailgun_tagging_mailkey'] = array(
  138. '#type' => 'checkbox',
  139. '#title' => t('Enable tags by mail key'),
  140. '#description' => t('See !url for details.', array(
  141. '!url' => l(t('Tagging'), 'https://documentation.mailgun.com/user_manual.html#tagging', array(
  142. 'attributes' => array(
  143. 'onclick' => "target='_blank'",
  144. ),
  145. )),
  146. )),
  147. '#default_value' => variable_get('mailgun_tagging_mailkey', TRUE),
  148. );
  149. }
  150. $form = system_settings_form($form);
  151. $form['#validate'][] = 'mailgun_admin_settings_validate';
  152. return $form;
  153. }
  154. /**
  155. * Form validation handler for mailgun_admin_settings().
  156. *
  157. * Perform additional validation to ensure the API key entered is valid.
  158. */
  159. function mailgun_admin_settings_validate($form, &$form_state) {
  160. if ($form['mailgun_api_key']['#default_value'] != $form_state['values']['mailgun_api_key']) {
  161. // The API key has changed. Perform validation.
  162. $form_state['values']['mailgun_api_key'] = trim($form_state['values']['mailgun_api_key']);
  163. $client = mailgun_get_client($form_state['values']['mailgun_api_key']);
  164. if ($client === FALSE) {
  165. drupal_set_message(t('Could not connect to Mailgun API. Please check your settings'), 'warning');
  166. return;
  167. }
  168. try {
  169. $client->domains()->index();
  170. drupal_set_message(t('Your API key has been successfully validated.'));
  171. }
  172. catch (Exception $e) {
  173. form_set_error('mailgun_api_key', t('An exception occurred. @code: @message', array(
  174. '@code' => $e->getCode(),
  175. '@message' => $e->getMessage(),
  176. )));
  177. }
  178. }
  179. }
  180. /**
  181. * Form builder. Display a form for sending a test e-mail.
  182. */
  183. function mailgun_test_form($form, &$form_state) {
  184. drupal_set_title(t('Send test mail'));
  185. $form['to'] = array(
  186. '#type' => 'textfield',
  187. '#title' => t('To'),
  188. '#default_value' => variable_get('site_mail', ''),
  189. '#description' => t('Type in an address to have the test email sent there.'),
  190. '#required' => TRUE,
  191. );
  192. $message = "Howdy!\n\nIf this e-mail is displayed correctly and delivered sound and safe, congrats! You have successfully configured Mailgun. ";
  193. $message .= t('Visit the !project to contribute or read !documentation to learn more.', array(
  194. '!project' => l(t('project page'), 'https://www.drupal.org/project/mailgun'),
  195. '!documentation' => l(t('documentation'), MAILGUN_DOCUMENTATION_LINK),
  196. ));
  197. $form['message'] = array(
  198. '#type' => 'textarea',
  199. '#title' => t('Message'),
  200. '#default_value' => $message,
  201. '#required' => TRUE,
  202. );
  203. $form['attachment'] = array(
  204. '#title' => t('Include attachment'),
  205. '#type' => 'checkbox',
  206. '#description' => t('If checked, the Drupal icon will be included as an attachment with the test e-mail.'),
  207. '#default_value' => TRUE,
  208. );
  209. $form['submit'] = array(
  210. '#type' => 'submit',
  211. '#value' => t('Send'),
  212. );
  213. $form['cancel'] = array(
  214. '#type' => 'link',
  215. '#href' => MAILGUN_ADMIN_PAGE,
  216. '#title' => t('Cancel'),
  217. );
  218. return $form;
  219. }
  220. /**
  221. * Form submission handler for mailgun_test_form().
  222. *
  223. * Send the test e-mail.
  224. */
  225. function mailgun_test_form_submit($form, &$form_state) {
  226. $to = $form_state['values']['to'];
  227. $body = explode('\n', $form_state['values']['message']);
  228. $params = array(
  229. 'message' => $body,
  230. 'attachment' => $form_state['values']['attachment'],
  231. );
  232. $site_name = variable_get('site_name', '');
  233. $default_from = variable_get('site_mail', ini_get('sendmail_from'));
  234. $from = (!empty($site_name)) ? $site_name . ' <' . $default_from . '>' : $default_from;
  235. $result = drupal_mail('mailgun', 'test', $to, $GLOBALS['language'], $params, $from);
  236. drupal_set_message(t('Test email sent from %from to %to. If you have the "Log mails" setting enabled, check the <a href="@url">database log</a> for details.',
  237. array(
  238. '%from' => $result['from'],
  239. '%to' => $result['to'],
  240. '@url' => url('admin/reports/dblog'),
  241. )), 'status');
  242. }