mailjet.admin.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @file
  4. * The backoffice of module.
  5. */
  6. /**
  7. * Implements hook_settings().
  8. */
  9. function mailjet_admin_settings() {
  10. $form['onoff'] = array(
  11. '#type' => 'fieldset',
  12. '#title' => t('General settings'),
  13. );
  14. $form['onoff']['mailjet_on'] = array(
  15. '#type' => 'radios',
  16. '#title' => t('Send emails through Mailjet'),
  17. '#default_value' => variable_get('mailjet_on', FALSE),
  18. '#options' => array(1 => t('Yes'), 0 => t('No')),
  19. );
  20. $form['onoff']['mailjet_from'] = array(
  21. '#type' => 'textfield',
  22. '#title' => t('E-mail from address'),
  23. '#default_value' => variable_get('mailjet_from', variable_get('site_mail', '')),
  24. '#description' => t('The e-mail address that all e-mails will be from.'),
  25. '#required' => TRUE,
  26. );
  27. $form['onoff']['mailjet_test'] = array(
  28. '#type' => 'radios',
  29. '#title' => t('Send test mail now'),
  30. '#default_value' => variable_get('mailjet_test', FALSE),
  31. '#options' => array(1 => t('Yes'), 0 => t('No')),
  32. );
  33. $form['onoff']['mailjet_test_address'] = array(
  34. '#type' => 'textfield',
  35. '#title' => t('Recipient of test mail'),
  36. '#default_value' => variable_get('mailjet_test_address', ''),
  37. );
  38. $form['auth'] = array(
  39. '#type' => 'fieldset',
  40. '#title' => t('Mailjet settings'),
  41. );
  42. $form['auth']['mailjet_username'] = array(
  43. '#type' => 'textfield',
  44. '#title' => t('API Key'),
  45. '#default_value' => variable_get('mailjet_username', ''),
  46. '#required' => TRUE,
  47. );
  48. $form['auth']['mailjet_password'] = array(
  49. '#type' => 'textfield',
  50. '#title' => t('Secret Key'),
  51. '#default_value' => variable_get('mailjet_password', ''),
  52. '#required' => TRUE,
  53. );
  54. $test_address = variable_get('mailjet_test_address', '');
  55. if (variable_get('mailjet_test', FALSE) && valid_email_address($test_address)) {
  56. $mailjet_on = variable_get('mailjet_on', FALSE);
  57. variable_set('mail_system', array('default-system' => 'MailjetSmtpMailSystem'));
  58. variable_set('mailjet_test', FALSE);
  59. global $language;
  60. $params['subject'] = t('Your test mail from Mailjet');
  61. $params['body'] = array(t('Your Mailjet configuration is ok!'));
  62. $mail = drupal_mail('mailjet', 'mailjet-test', $test_address, $language, $params);
  63. variable_set('mailjet_on', $mailjet_on);
  64. if (isset($mail['result']) && $mail['result']) {
  65. drupal_set_message(t('A test e-mail has been sent to @email.', array('@email' => check_plain($test_address))));
  66. }
  67. }
  68. if (variable_get('mailjet_on', FALSE)) {
  69. variable_set('mail_system', array('default-system' => 'MailjetSmtpMailSystem'));
  70. }
  71. else {
  72. variable_set('mail_system', array('default-system' => 'DefaultMailSystem'));
  73. }
  74. return system_settings_form($form);
  75. }
  76. /**
  77. * Implements hook_admin_settings_validat().
  78. */
  79. function mailjet_admin_settings_validate($form, &$form_state) {
  80. if (!valid_email_address($form_state['values']['mailjet_from'])) {
  81. form_set_error('mailjet_from', t('The provided from e-mail address is not valid.'));
  82. }
  83. if ($form_state['values']['mailjet_test'] && !valid_email_address($form_state['values']['mailjet_test_address'])) {
  84. form_set_error('mailjet_test_address', t('The provided test e-mail address is not valid.'));
  85. }
  86. $configs = array(array('ssl://', 465),
  87. array('tls://', 587),
  88. array('', 587),
  89. array('', 588),
  90. array('tls://', 25),
  91. array('', 25));
  92. $host = variable_get('mailjet_host', 'in.mailjet.com');
  93. $connected = FALSE;
  94. for ($i = 0; $i < count($configs); ++$i) {
  95. $soc = @ fsockopen($configs[$i][0] . $host, $configs[$i][1], $errno, $errstr, 5);
  96. if ($soc) {
  97. fClose($soc);
  98. $connected = TRUE;
  99. break;
  100. }
  101. }
  102. if ($connected) {
  103. if ('ssl://' == $configs[$i][0]) {
  104. variable_set('mailjet_protocol', 'ssl');
  105. }
  106. elseif ('tls://' == $configs[$i][0]) {
  107. variable_set('mailjet_protocol', 'tls');
  108. }
  109. else {
  110. variable_set('mailjet_protocol', 'standard');
  111. }
  112. variable_set('mailjet_port', $configs[$i][1]);
  113. }
  114. else {
  115. form_set_error('mailjet_on', t('Please contact Mailjet support to sort this out.<br /><br />Error @errno - @errstr', array ('@errno' => $errno, '@errstr' => $errstr)));
  116. }
  117. }