mimemail.rules.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. /**
  3. * @file
  4. * Rules actions for sending Mime-encoded emails.
  5. *
  6. * @addtogroup rules
  7. * @{
  8. */
  9. /**
  10. * Implements hook_rules_action_info().
  11. */
  12. function mimemail_rules_action_info() {
  13. return array(
  14. 'mimemail' => array(
  15. 'label' => t('Send HTML e-mail'),
  16. 'group' => t('System'),
  17. 'parameter' => array(
  18. 'key' => array(
  19. 'type' => 'text',
  20. 'label' => t('Key'),
  21. 'description' => t('A key to identify the e-mail sent.'),
  22. ),
  23. 'to' => array(
  24. 'type' => 'text',
  25. 'label' => t('To'),
  26. 'description' => t("The mail's recipient address. The formatting of this string must comply with RFC 2822."),
  27. ),
  28. 'cc' => array(
  29. 'type' => 'text',
  30. 'label' => t('CC Recipient'),
  31. 'description' => t("The mail's carbon copy address. You may separate multiple addresses with comma."),
  32. 'optional' => TRUE,
  33. ),
  34. 'bcc' => array(
  35. 'type' => 'text',
  36. 'label' => t('BCC Recipient'),
  37. 'description' => t("The mail's blind carbon copy address. You may separate multiple addresses with comma."),
  38. 'optional' => TRUE,
  39. ),
  40. 'from_name' => array(
  41. 'type' => 'text',
  42. 'label' => t('Sender name'),
  43. 'description' => t("The sender's name. Leave it empty to use the site-wide configured name."),
  44. 'optional' => TRUE,
  45. ),
  46. 'from_mail' => array(
  47. 'type' => 'text',
  48. 'label' => t('Sender e-mail address'),
  49. 'description' => t("The sender's address. Leave it empty to use the site-wide configured address."),
  50. 'optional' => TRUE,
  51. ),
  52. 'reply_to' => array(
  53. 'type' => 'text',
  54. 'label' => t('Reply e-mail address'),
  55. 'description' => t("The address to reply to. Leave it empty to use the sender's address."),
  56. 'optional' => TRUE,
  57. ),
  58. 'list_unsubscribe' => array(
  59. 'type' => 'text',
  60. 'label' => t('Unsubscription e-mail and/or URL'),
  61. 'description' => t("An e-mail address and/or a URL which can be used for unsubscription. Values must be enclosed by angel brackets and separated by a comma."),
  62. 'optional' => TRUE,
  63. ),
  64. 'subject' => array(
  65. 'type' => 'text',
  66. 'label' => t('Subject'),
  67. 'description' => t("The mail's subject."),
  68. 'translatable' => TRUE,
  69. ),
  70. 'body' => array(
  71. 'type' => 'text',
  72. 'label' => t('Body'),
  73. 'description' => t('The mail\'s HTML body. Will be formatted using the text format selected on the <a href="@url">settings</a> page.', array('@url' => url('admin/config/system/mimemail'))),
  74. 'sanitize' => TRUE,
  75. 'optional' => TRUE,
  76. 'translatable' => TRUE,
  77. ),
  78. 'plaintext' => array(
  79. 'type' => 'text',
  80. 'label' => t('Plain text body'),
  81. 'description' => t("The mail's plaintext body."),
  82. 'optional' => TRUE,
  83. 'translatable' => TRUE,
  84. ),
  85. 'attachments' => array(
  86. 'type' => 'text',
  87. 'label' => t('Attachments'),
  88. 'description' => t("The mail's attachments, one file per line e.g. \"files/images/mypic.png\" without quotes."),
  89. 'optional' => TRUE,
  90. ),
  91. 'language' => array(
  92. 'type' => 'token',
  93. 'label' => t('Language'),
  94. 'description' => t('If specified, the language used for getting the mail message and subject.'),
  95. 'options list' => 'entity_metadata_language_list',
  96. 'optional' => TRUE,
  97. 'default value' => LANGUAGE_NONE,
  98. 'default mode' => 'selector',
  99. ),
  100. ),
  101. 'base' => 'rules_action_mimemail',
  102. 'access callback' => 'rules_system_integration_access',
  103. ),
  104. 'mimemail_to_users_of_role' => array(
  105. 'label' => t('Send HTML mail to all users of a role'),
  106. 'group' => t('System'),
  107. 'parameter' => array(
  108. 'key' => array(
  109. 'type' => 'text',
  110. 'label' => t('Key'),
  111. 'description' => t('A key to identify the e-mail sent.'),
  112. ),
  113. 'roles' => array(
  114. 'type' => 'list<integer>',
  115. 'label' => t('Roles'),
  116. 'options list' => 'entity_metadata_user_roles',
  117. 'description' => t('Select the roles whose users should receive the mail.'),
  118. ),
  119. 'active' => array(
  120. 'type' => 'boolean',
  121. 'label' =>('Send to active users'),
  122. 'description' => t('Send mail only to active users.'),
  123. ),
  124. 'from_name' => array(
  125. 'type' => 'text',
  126. 'label' => t('Sender name'),
  127. 'description' => t("The sender's name. Leave it empty to use the site-wide configured name."),
  128. 'optional' => TRUE,
  129. ),
  130. 'from_mail' => array(
  131. 'type' => 'text',
  132. 'label' => t('Sender e-mail address'),
  133. 'description' => t("The sender's address. Leave it empty to use the site-wide configured address."),
  134. 'optional' => TRUE,
  135. ),
  136. 'subject' => array(
  137. 'type' => 'text',
  138. 'label' => t('Subject'),
  139. 'description' => t("The mail's subject."),
  140. 'translatable' => TRUE,
  141. ),
  142. 'body' => array(
  143. 'type' => 'text',
  144. 'label' => t('Body'),
  145. 'description' => t("The mail's message HTML body."),
  146. 'optional' => TRUE,
  147. 'translatable' => TRUE,
  148. ),
  149. 'plaintext' => array(
  150. 'type' => 'text',
  151. 'label' => t('Plaintext body'),
  152. 'description' => t("The mail's message plaintext body."),
  153. 'optional' => TRUE,
  154. 'translatable' => TRUE,
  155. ),
  156. 'attachments' => array(
  157. 'type' => 'text',
  158. 'label' => t('Attachments'),
  159. 'description' => t("The mail's attachments, one file per line e.g. \"files/images/mypic.png\" without quotes."),
  160. 'optional' => TRUE,
  161. ),
  162. 'language_user' => array(
  163. 'type' => 'boolean',
  164. 'label' => t("Send mail in each recipient's language"),
  165. 'description' => t("If checked, the mail message and subject will be sent in each user's preferred language. <strong>You can safely leave the language selector below empty if this option is selected.</strong>"),
  166. ),
  167. 'language' => array(
  168. 'type' => 'token',
  169. 'label' => t('Fixed language'),
  170. 'description' => t('If specified, the fixed language used for getting the mail message and subject.'),
  171. 'options list' => 'entity_metadata_language_list',
  172. 'optional' => TRUE,
  173. 'default value' => LANGUAGE_NONE,
  174. 'default mode' => 'selector',
  175. ),
  176. ),
  177. 'base' => 'rules_action_mimemail_to_users_of_role',
  178. 'access callback' => 'rules_system_integration_access',
  179. ),
  180. );
  181. }
  182. /**
  183. * Implements hook_rules_action_base_upgrade_map_name().
  184. */
  185. function mimemail_rules_action_mail_upgrade_map_name($element) {
  186. return 'mimemail';
  187. }
  188. /**
  189. * Implements hook_rules_action_base_upgrade_map_name().
  190. */
  191. function mimemail_rules_action_mail_to_user_upgrade_map_name($element) {
  192. return 'mimemail';
  193. }
  194. /**
  195. * Implements hook_rules_action_base_upgrade_map_name().
  196. */
  197. function mimemail_rules_action_mail_to_users_of_role_upgrade_map_name($element) {
  198. return 'mimemail_to_users_of_role';
  199. }
  200. /**
  201. * Implements hook_rules_action_base_upgrade().
  202. */
  203. function mimemail_rules_action_mail_upgrade($element, RulesPlugin $target) {
  204. $target->settings['key'] = $element['#settings']['key'];
  205. $target->settings['from_name'] = $element['#settings']['sender'];
  206. $target->settings['from_mail'] = $element['#settings']['from'];
  207. $target->settings['body'] = $element['#settings']['message_html'];
  208. $target->settings['plaintext'] = $element['#settings']['message_plaintext'];
  209. }
  210. /**
  211. * Implements hook_rules_action_base_upgrade().
  212. */
  213. function mimemail_rules_action_mail_to_user_upgrade($element, RulesPlugin $target) {
  214. switch ($element['#settings']['#argument map']['user']) {
  215. case 'author':
  216. $token = 'node:author';
  217. break;
  218. case 'author_unchanged':
  219. $token = 'node-unchanged:author';
  220. break;
  221. case 'user':
  222. $token = 'site:current-user';
  223. break;
  224. }
  225. $target->settings['to:select'] = $token . ':mail';
  226. mimemail_rules_action_mail_upgrade($element, $target);
  227. }
  228. /**
  229. * Implements hook_rules_action_base_upgrade().
  230. */
  231. function mimemail_rules_action_mail_to_users_of_role_upgrade($element, RulesPlugin $target) {
  232. $target->settings['roles'] = $element['#settings']['recipients'];
  233. mimemail_rules_action_mail_upgrade($element, $target);
  234. }
  235. /**
  236. * Action Implementation: Send HTML mail.
  237. */
  238. function rules_action_mimemail($key, $to, $cc = NULL, $bcc = NULL, $from_name = NULL, $from_mail = NULL, $reply_to = NULL, $list_unsubscribe = NULL, $subject, $body, $plaintext = NULL, $attachments = array(), $langcode, $settings, RulesState $state, RulesPlugin $element) {
  239. module_load_include('inc', 'mimemail');
  240. // Set the sender name and from address.
  241. if (empty($from_mail)) {
  242. $from = NULL;
  243. }
  244. else {
  245. $from = array(
  246. 'name' => $from_name,
  247. 'mail' => $from_mail,
  248. );
  249. // Create an address string.
  250. $from = mimemail_address($from);
  251. }
  252. // Figure out the language to use - fallback is the system default.
  253. $languages = language_list();
  254. $language = isset($languages[$langcode]) ? $languages[$langcode] : language_default();
  255. $params = array(
  256. 'context' => array(
  257. 'subject' => $subject,
  258. 'body' => $body,
  259. 'action' => $element,
  260. 'state' => $state,
  261. ),
  262. 'cc' => $cc,
  263. 'bcc' => $bcc,
  264. 'reply-to' => $reply_to,
  265. 'list-unsubscribe' => $list_unsubscribe,
  266. 'plaintext' => $plaintext,
  267. 'attachments' => $attachments,
  268. );
  269. drupal_mail('mimemail', $key, $to, $language, $params, $from);
  270. }
  271. /**
  272. * Action: Send HTML mail to all users of a specific role group(s).
  273. */
  274. function rules_action_mimemail_to_users_of_role($key, $roles, $active, $from_name = NULL, $from_mail = NULL, $subject, $body, $plaintext = NULL, $attachments = array(), $use_userlang = FALSE, $langcode= NULL, $settings, RulesState $state, RulesPlugin $element) {
  275. module_load_include('inc', 'mimemail');
  276. // Set the sender name and from address.
  277. if (empty($from_mail)) {
  278. $from = NULL;
  279. }
  280. else {
  281. $from = array(
  282. 'name' => $from_name,
  283. 'mail' => $from_mail,
  284. );
  285. // Create an address string.
  286. $from = mimemail_address($from);
  287. }
  288. $query = db_select('users', 'u');
  289. $query->fields('u', array('mail', 'language'));
  290. if ($active) {
  291. $query->condition('u.status', 1, '=');
  292. }
  293. if (in_array(DRUPAL_AUTHENTICATED_RID, $roles)) {
  294. $query->condition('u.uid', 0, '>');
  295. }
  296. else {
  297. $query->join('users_roles', 'r', 'u.uid = r.uid');
  298. $query->condition('r.rid', $roles, 'IN');
  299. $query->distinct();
  300. }
  301. $result = $query->execute();
  302. $params = array(
  303. 'context' => array(
  304. 'subject' => $subject,
  305. 'body' => $body,
  306. 'action' => $element,
  307. 'state' => $state,
  308. ),
  309. 'plaintext' => $plaintext,
  310. 'attachments' => $attachments,
  311. );
  312. // Create language list before initializing foreach.
  313. $languages = language_list();
  314. $message = array('result' => TRUE);
  315. foreach ($result as $row) {
  316. // Decide which language to use.
  317. if (!$use_userlang || empty($row->language) || !isset($languages[$row->language])) {
  318. $language = isset($languages[$langcode]) ? $languages[$langcode] : language_default();
  319. }
  320. else {
  321. $language = $languages[$row->language];
  322. }
  323. $message = drupal_mail('mimemail', $key, $row->mail, $language, $params, $from);
  324. if (!$message['result']) {
  325. break;
  326. }
  327. }
  328. if ($message['result']) {
  329. $role_names = array_intersect_key(user_roles(TRUE), array_flip($roles));
  330. watchdog('rules', 'Successfully sent HTML email to the role(s) %roles.', array('%roles' => implode(', ', $role_names)));
  331. }
  332. }
  333. /**
  334. * @}
  335. */