mimemail_action.module 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * @file
  4. * Provide actions for Mime Mail.
  5. */
  6. /**
  7. * Implements hook_action_info().
  8. */
  9. function mimemail_action_info() {
  10. return array(
  11. 'mimemail_send_email_action' => array(
  12. 'type' => 'system',
  13. 'label' => t('Send HTML e-mail'),
  14. 'configurable' => TRUE,
  15. 'triggers' => array('any'),
  16. ),
  17. );
  18. }
  19. /**
  20. * Implements a configurable Drupal action. Sends an email.
  21. */
  22. function mimemail_send_email_action($entity, $context) {
  23. if (empty($context['node'])) {
  24. if (get_class($entity) == 'OgMembership') {
  25. $context['user'] = user_load($entity->etid);
  26. } else {
  27. $context['node'] = $entity;
  28. }
  29. }
  30. $to = token_replace($context['to'], $context);
  31. // If the recipient is a registered user with a language preference, use
  32. // the recipient's preferred language. Otherwise, use the system default
  33. // language.
  34. $account = user_load_by_mail($to);
  35. if ($account) {
  36. $language = user_preferred_language($account);
  37. }
  38. else {
  39. $language = language_default();
  40. }
  41. $params = array(
  42. 'context' => array(
  43. 'subject' => token_replace($context['subject'], $context),
  44. 'body' => token_replace($context['body'], $context),
  45. ),
  46. 'key' => $context['key'],
  47. 'cc' => $context['cc'],
  48. 'bcc' => $context['bcc'],
  49. 'reply-to' => $context['reply-to'],
  50. 'plaintext' => token_replace($context['plaintext'], $context),
  51. 'attachments' => $context['attachments'],
  52. );
  53. drupal_mail('mimemail', $context['key'], $to, $language, $params);
  54. }
  55. /**
  56. * Form for configurable Drupal action to send an HTML mail.
  57. */
  58. function mimemail_send_email_action_form($context) {
  59. $context += array(
  60. 'key' => '',
  61. 'to' => '',
  62. 'cc' => '',
  63. 'bcc' => '',
  64. 'reply-to' => '',
  65. 'subject' => '',
  66. 'body' => '',
  67. 'format' => filter_fallback_format(),
  68. 'plaintext' => '',
  69. 'attachments' => ''
  70. );
  71. $form['key'] = array(
  72. '#type' => 'textfield',
  73. '#title' => t('Key'),
  74. '#default_value' => $context['key'],
  75. '#description' => t('A key to identify the e-mail sent.'),
  76. '#required' => TRUE,
  77. );
  78. $form['to'] = array(
  79. '#type' => 'textfield',
  80. '#title' => t('Recipient'),
  81. '#default_value' => $context['to'],
  82. '#maxlength' => 254,
  83. '#description' => t('The email address to which the message should be sent OR enter [node:author:mail], [comment:author:mail], etc. if you would like to send an e-mail to the author of the original post.'),
  84. '#required' => TRUE,
  85. );
  86. $form['cc'] = array(
  87. '#type' => 'textfield',
  88. '#title' => t('CC Recipient'),
  89. '#default_value' => $context['cc'],
  90. '#description' => t("The mail's carbon copy address. You may separate multiple addresses with comma."),
  91. '#required' => FALSE,
  92. );
  93. $form['bcc'] = array(
  94. '#type' => 'textfield',
  95. '#title' => t('BCC Recipient'),
  96. '#default_value' => $context['bcc'],
  97. '#description' => t("The mail's blind carbon copy address. You may separate multiple addresses with comma."),
  98. '#required' => FALSE,
  99. );
  100. $form['reply-to'] = array(
  101. '#type' => 'textfield',
  102. '#title' => t('Reply e-mail address'),
  103. '#default_value' => $context['reply-to'],
  104. '#description' => t("The address to reply to. Leave it empty to use the sender's address."),
  105. '#required' => FALSE,
  106. );
  107. $form['subject'] = array(
  108. '#type' => 'textfield',
  109. '#title' => t('Subject'),
  110. '#maxlength' => 254,
  111. '#default_value' => $context['subject'],
  112. '#description' => t("The subject of the message."),
  113. );
  114. $form['body'] = array(
  115. '#type' => 'text_format',
  116. '#title' => t('Body'),
  117. '#default_value' => $context['body'],
  118. '#format' => $context['format'],
  119. '#description' => t('The HTML message that should be sent. You may include placeholders like [node:title], [user:name], and [comment:body] to represent data that will be different each time message is sent. Not all placeholders will be available in all contexts.'),
  120. );
  121. $form['plaintext'] = array(
  122. '#type' => 'textarea',
  123. '#title' => t('Plain text body'),
  124. '#default_value' => $context['plaintext'],
  125. '#description' => t('Optional plaintext portion of a multipart message. You may include placeholders like [node:title], [user:name], and [comment:body] to represent data that will be different each time message is sent. Not all placeholders will be available in all contexts.'),
  126. );
  127. $form['attachments'] = array(
  128. '#type' => 'textarea',
  129. '#title' => t('Attachments'),
  130. '#default_value' => $context['attachments'],
  131. '#description' => t('A list of attachments, one file per line e.g. "files/images/mypic.png" without quotes.'),
  132. );
  133. return $form;
  134. }
  135. /**
  136. * Validate the action form.
  137. */
  138. function mimemail_send_email_action_validate($form, $form_state) {
  139. $to = trim($form_state['values']['to']);
  140. if (!valid_email_address($to) && strpos($to, ':mail') === FALSE) {
  141. form_set_error('to', t('Enter a valid email address or use a token e-mail address such as %author.', array('%author' => '[node:author:mail]')));
  142. }
  143. $cc = explode(',', $form_state['values']['cc']);
  144. foreach ($cc as $recipient) {
  145. $recipient = trim($recipient);
  146. if (!empty($recipient) && !valid_email_address($recipient) && strpos($recipient, ':mail') === FALSE) {
  147. form_set_error('cc', t('Enter a valid email address or use a token e-mail address such as %author.', array('%author' => '[node:author:mail]')));
  148. }
  149. }
  150. $bcc = explode(',', $form_state['values']['bcc']);
  151. foreach ($bcc as $recipient) {
  152. $recipient = trim($recipient);
  153. if (!empty($recipient) && !valid_email_address($recipient) && strpos($recipient, ':mail') === FALSE) {
  154. form_set_error('bcc', t('Enter a valid email address or use a token e-mail address such as %author.', array('%author' => '[node:author:mail]')));
  155. }
  156. }
  157. $reply_to = trim($form_state['values']['reply-to']);
  158. if (!empty($reply_to) && !valid_email_address($reply_to) && strpos($reply_to, ':mail') === FALSE) {
  159. form_set_error('reply-to', t('Enter a valid email address or use a token e-mail address such as %author.', array('%author' => '[node:author:mail]')));
  160. }
  161. }
  162. /**
  163. * Handle submission of the action form.
  164. */
  165. function mimemail_send_email_action_submit($form, $form_state) {
  166. $form_values = $form_state['values'];
  167. $params = array(
  168. 'key' => $form_values['key'],
  169. 'to' => $form_values['to'],
  170. 'cc' => $form_values['cc'],
  171. 'bcc' => $form_values['bcc'],
  172. 'reply-to' => $form_values['reply-to'],
  173. 'subject' => $form_values['subject'],
  174. 'body' => $form_values['body']['value'],
  175. 'format' => $form_values['body']['format'],
  176. 'plaintext' => $form_values['plaintext'],
  177. 'attachments' => $form_values['attachments'],
  178. );
  179. return $params;
  180. }