mailgun.admin.inc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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($form, &$form_state) {
  18. $library = libraries_detect('mailgun');
  19. if (!$library['installed']) {
  20. drupal_set_message(t('The Mailgun PHP library is not installed. Please see <a href="@url">documentation</a> for more information.', array('@url' => url('https://www.drupal.org/node/2547591'))), 'error');
  21. }
  22. $key = variable_get('mailgun_api_key', '');
  23. $form['mailgun_api_key'] = array(
  24. '#title' => t('Mailgun API key'),
  25. '#type' => 'textfield',
  26. '#description' => t('Get your Secret API key from the <a href="@url">Mailgun dashboard</a>.', array('@url' => url('https://mailgun.com/app/dashboard'))),
  27. '#default_value' => $key,
  28. '#required' => TRUE,
  29. );
  30. $client = FALSE;
  31. if (!empty($key)) {
  32. try {
  33. $client = mailgun_get_client($key);
  34. } catch (Exception $e) {
  35. watchdog('mailgun', 'An exception occurred. @code: @message', array('@code' => $e->getCode(), '@message' => $e->getMessage()), WATCHDOG_WARNING, 'admin/config/system/mailgun');
  36. drupal_set_message(t('Mailgun: %message', array('%message' => $e->getMessage())), 'error');
  37. }
  38. }
  39. // Display settings only when a valid API key is present and client is active
  40. if ($client) {
  41. $domain_options = array(
  42. '_sender' => t('Get domain from sender address'),
  43. );
  44. $domains = array();
  45. $result = $client->get('domains');
  46. if ($result && $result->http_response_code == 200) {
  47. foreach ($result->http_response_body->items as $domain) {
  48. $domains[$domain->name] = $domain;
  49. $domain_options[$domain->name] = $domain->name;
  50. }
  51. }
  52. $form['mailgun_domain'] = array(
  53. '#title' => t('Domain'),
  54. '#type' => 'select',
  55. '#options' => $domain_options,
  56. '#description' => t('Mails will be sent using this domain'),
  57. '#default_value' => variable_get('mailgun_domain', '_sender'),
  58. );
  59. $form['mailgun_test'] = array(
  60. '#title' => t('Test mode'),
  61. '#type' => 'checkbox',
  62. '#default_value' => variable_get('mailgun_test', FALSE),
  63. '#description' => t('Enables sending in test mode'),
  64. );
  65. $form['mailgun_queue'] = array(
  66. '#title' => t('Queue mails'),
  67. '#type' => 'checkbox',
  68. '#description' => t('Mails will be queued and sent during cron runs. Useful for sending a large number of emails.'),
  69. '#default_value' => variable_get('mailgun_queue', FALSE),
  70. );
  71. $form['mailgun_log'] = array(
  72. '#title' => t('Log mails'),
  73. '#type' => 'checkbox',
  74. '#description' => t('Log mails sent through Mailgun. Should not be enabled on production sites. Messages fail to send will be logged regardless of this setting.'),
  75. '#default_value' => variable_get('mailgun_log', FALSE),
  76. );
  77. $formats = array('_none' => t('- None -'));
  78. foreach (filter_formats() as $format) {
  79. if ($format->format == 'php_code') {
  80. continue;
  81. }
  82. $formats[$format->format] = t($format->name);
  83. }
  84. $form['mailgun_format'] = array(
  85. '#title' => t('Text format'),
  86. '#type' => 'select',
  87. '#description' => t('Specify an additional text format to filter the message through before sending the email.'),
  88. '#options' => $formats,
  89. '#default_value' => variable_get('mailgun_format', '_none'),
  90. );
  91. $form['defaults'] = array(
  92. '#type' => 'fieldset',
  93. '#title' => t('Default settings'),
  94. '#description' => t('These default settings apply to messages sent using Mailgun and may be overriden on a per-message basis.'),
  95. '#collapsible' => FALSE,
  96. '#collapsed' => FALSE,
  97. );
  98. $form['defaults']['mailgun_tracking'] = array(
  99. '#title' => t('Enable tracking'),
  100. '#type' => 'select',
  101. '#options' => array('default' => t('Use default setting'), 'enabled' => t('Enabled'), 'disabled' => t('Disabled')),
  102. '#description' => t('Whether to enable event tracking by default or not. See <a href="@url">Tracking Messages</a> for details.', array('@url' => url('https://documentation.mailgun.com/user_manual.html#tracking-messages'))),
  103. '#default_value' => variable_get('mailgun_tracking', 'default'),
  104. );
  105. $form['defaults']['mailgun_tracking_clicks'] = array(
  106. '#title' => t('Enable click tracking'),
  107. '#type' => 'select',
  108. '#options' => array('default' => t('Use default setting'), 'enabled' => t('Enabled'), 'disabled' => t('Disabled')),
  109. '#description' => t('Whether to enable click tracking by default or not.'),
  110. '#default_value' => variable_get('mailgun_tracking_clicks', 'default'),
  111. );
  112. $form['defaults']['mailgun_tracking_opens'] = array(
  113. '#title' => t('Enable open tracking'),
  114. '#type' => 'select',
  115. '#options' => array('default' => t('Use default setting'), 'enabled' => t('Enabled'), 'disabled' => t('Disabled')),
  116. '#description' => t('Whether to enable open tracking by default or not.'),
  117. '#default_value' => variable_get('mailgun_tracking_opens', 'default'),
  118. );
  119. }
  120. $form = system_settings_form($form);
  121. $form['#validate'][] = 'mailgun_admin_settings_validate';
  122. return $form;
  123. }
  124. /**
  125. * Form validation handler for mailgun_admin_settings().
  126. *
  127. * Perform additional validation to ensure the API key entered is valid.
  128. */
  129. function mailgun_admin_settings_validate($form, &$form_state) {
  130. if ($form['mailgun_api_key']['#default_value'] != $form_state['values']['mailgun_api_key']) {
  131. // The API key has changed. Perform validation.
  132. $form_state['values']['mailgun_api_key'] = trim($form_state['values']['mailgun_api_key']);
  133. $client = mailgun_get_client($form_state['values']['mailgun_api_key']);
  134. try {
  135. $result = $client->get('domains');
  136. drupal_set_message(t('Your API key has been successfully validated.'));
  137. } catch (Exception $e) {
  138. form_set_error('mailgun_api_key', t('An exception occurred. @code: @message', array('@code' => $e->getCode(), '@message' => $e->getMessage())));
  139. }
  140. }
  141. }
  142. /**
  143. * Form builder. Display a form for sending a test e-mail.
  144. */
  145. function mailgun_test_form($form, &$form_state) {
  146. drupal_set_title(t('Send test mail'));
  147. $form['to'] = array(
  148. '#type' => 'textfield',
  149. '#title' => t('To'),
  150. '#default_value' => variable_get('site_mail', ''),
  151. '#description' => t('Type in an address to have the test email sent there.'),
  152. '#required' => TRUE,
  153. );
  154. $message = "Howdy!\n\nIf this e-mail is displayed correctly and delivered sound and safe, congrats! You have successfully configured Mailgun.";
  155. $message .= ' Visit the <a href="@project">project page</a> to contribute or read <a href="@documentation">documentation</a> to learn more.';
  156. $message = t($message, array('@project' => url('https://www.drupal.org/project/mailgun'), '@documentation' => url('https://www.drupal.org/node/2547591')));
  157. $form['message'] = array(
  158. '#type' => 'textarea',
  159. '#title' => t('Message'),
  160. '#default_value' => $message,
  161. '#required' => TRUE,
  162. );
  163. $form['attachment'] = array(
  164. '#title' => t('Include attachment'),
  165. '#type' => 'checkbox',
  166. '#description' => t('If checked, the Drupal icon will be included as an attachment with the test e-mail.'),
  167. '#default_value' => TRUE,
  168. );
  169. $form['submit'] = array(
  170. '#type' => 'submit',
  171. '#value' => t('Send'),
  172. );
  173. $form['cancel'] = array(
  174. '#type' => 'link',
  175. '#href' => 'admin/config/system/mailgun',
  176. '#title' => t('Cancel'),
  177. );
  178. return $form;
  179. }
  180. /**
  181. * Form submission handler for mailgun_test_form().
  182. * Send the test e-mail.
  183. */
  184. function mailgun_test_form_submit($form, &$form_state) {
  185. $to = $form_state['values']['to'];
  186. $params = array(
  187. 'message' => $form_state['values']['message'],
  188. 'attachment' => $form_state['values']['attachment'],
  189. );
  190. $site_name = variable_get('site_name', '');
  191. $default_from = variable_get('site_mail', ini_get('sendmail_from'));
  192. $from = (!empty($site_name)) ? $site_name . ' <' . $default_from . '>' : $default_from;
  193. $result = drupal_mail('mailgun', 'test', $to, $GLOBALS['language'], $params, $from);
  194. 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.', array('%from' => $result['from'], '%to' => $result['to'], '@url' => url('admin/reports/dblog'))), 'status');
  195. }