mimemail.rules.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. 'subject' => array(
  59. 'type' => 'text',
  60. 'label' => t('Subject'),
  61. 'description' => t("The mail's subject."),
  62. 'translatable' => TRUE,
  63. ),
  64. 'body' => array(
  65. 'type' => 'text',
  66. 'label' => t('Body'),
  67. 'description' => t("The mail's message HTML body."),
  68. 'sanitize' => TRUE,
  69. 'optional' => TRUE,
  70. 'translatable' => TRUE,
  71. ),
  72. 'plaintext' => array(
  73. 'type' => 'text',
  74. 'label' => t('Plain text body'),
  75. 'description' => t("The mail's message plaintext body."),
  76. 'optional' => TRUE,
  77. 'translatable' => TRUE,
  78. ),
  79. 'attachments' => array(
  80. 'type' => 'text',
  81. 'label' => t('Attachments'),
  82. 'description' => t("The mail's attachments, one file per line e.g. \"files/images/mypic.png\" without quotes."),
  83. 'optional' => TRUE,
  84. ),
  85. 'language' => array(
  86. 'type' => 'token',
  87. 'label' => t('Language'),
  88. 'description' => t('If specified, the language used for getting the mail message and subject.'),
  89. 'options list' => 'entity_metadata_language_list',
  90. 'optional' => TRUE,
  91. 'default value' => LANGUAGE_NONE,
  92. 'default mode' => 'selector',
  93. ),
  94. ),
  95. 'base' => 'rules_action_mimemail',
  96. 'access callback' => 'rules_system_integration_access',
  97. ),
  98. 'mimemail_to_users_of_role' => array(
  99. 'label' => t('Send HTML mail to all users of a role'),
  100. 'group' => t('System'),
  101. 'parameter' => array(
  102. 'key' => array(
  103. 'type' => 'text',
  104. 'label' => t('Key'),
  105. 'description' => t('A key to identify the e-mail sent.'),
  106. ),
  107. 'roles' => array(
  108. 'type' => 'list<integer>',
  109. 'label' => t('Roles'),
  110. 'options list' => 'entity_metadata_user_roles',
  111. 'description' => t('Select the roles whose users should receive the mail.'),
  112. ),
  113. 'active' => array(
  114. 'type' => 'boolean',
  115. 'label' =>('Send to active users'),
  116. 'description' => t('Send mail only to active users.'),
  117. ),
  118. 'from_name' => array(
  119. 'type' => 'text',
  120. 'label' => t('Sender name'),
  121. 'description' => t("The sender's name. Leave it empty to use the site-wide configured name."),
  122. 'optional' => TRUE,
  123. ),
  124. 'from_mail' => array(
  125. 'type' => 'text',
  126. 'label' => t('Sender e-mail address'),
  127. 'description' => t("The sender's address. Leave it empty to use the site-wide configured address."),
  128. 'optional' => TRUE,
  129. ),
  130. 'subject' => array(
  131. 'type' => 'text',
  132. 'label' => t('Subject'),
  133. 'description' => t("The mail's subject."),
  134. 'translatable' => TRUE,
  135. ),
  136. 'body' => array(
  137. 'type' => 'text',
  138. 'label' => t('Body'),
  139. 'description' => t("The mail's message HTML body."),
  140. 'optional' => TRUE,
  141. 'translatable' => TRUE,
  142. ),
  143. 'plaintext' => array(
  144. 'type' => 'text',
  145. 'label' => t('Plaintext body'),
  146. 'description' => t("The mail's message plaintext body."),
  147. 'optional' => TRUE,
  148. 'translatable' => TRUE,
  149. ),
  150. 'attachments' => array(
  151. 'type' => 'text',
  152. 'label' => t('Attachments'),
  153. 'description' => t("The mail's attachments, one file per line e.g. \"files/images/mypic.png\" without quotes."),
  154. 'optional' => TRUE,
  155. ),
  156. 'language_user' => array(
  157. 'type' => 'boolean',
  158. 'label' => t("Send mail in each recipient's language"),
  159. '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>"),
  160. ),
  161. 'language' => array(
  162. 'type' => 'token',
  163. 'label' => t('Fixed language'),
  164. 'description' => t('If specified, the fixed language used for getting the mail message and subject.'),
  165. 'options list' => 'entity_metadata_language_list',
  166. 'optional' => TRUE,
  167. 'default value' => LANGUAGE_NONE,
  168. 'default mode' => 'selector',
  169. ),
  170. ),
  171. 'base' => 'rules_action_mimemail_to_users_of_role',
  172. 'access callback' => 'rules_system_integration_access',
  173. ),
  174. );
  175. }
  176. /**
  177. * Implements hook_rules_action_base_upgrade_map_name().
  178. */
  179. function mimemail_rules_action_mail_upgrade_map_name($element) {
  180. return 'mimemail';
  181. }
  182. /**
  183. * Implements hook_rules_action_base_upgrade_map_name().
  184. */
  185. function mimemail_rules_action_mail_to_user_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_users_of_role_upgrade_map_name($element) {
  192. return 'mimemail_to_users_of_role';
  193. }
  194. /**
  195. * Implements hook_rules_action_base_upgrade().
  196. */
  197. function mimemail_rules_action_mail_upgrade($element, RulesPlugin $target) {
  198. $target->settings['key'] = $element['#settings']['key'];
  199. $target->settings['from_name'] = $element['#settings']['sender'];
  200. $target->settings['from_mail'] = $element['#settings']['from'];
  201. $target->settings['body'] = $element['#settings']['message_html'];
  202. $target->settings['plaintext'] = $element['#settings']['message_plaintext'];
  203. }
  204. /**
  205. * Implements hook_rules_action_base_upgrade().
  206. */
  207. function mimemail_rules_action_mail_to_user_upgrade($element, RulesPlugin $target) {
  208. switch ($element['#settings']['#argument map']['user']) {
  209. case 'author':
  210. $token = 'node:author';
  211. break;
  212. case 'author_unchanged':
  213. $token = 'node-unchanged:author';
  214. break;
  215. case 'user':
  216. $token = 'site:current-user';
  217. break;
  218. }
  219. $target->settings['to:select'] = $token . ':mail';
  220. mimemail_rules_action_mail_upgrade($element, $target);
  221. }
  222. /**
  223. * Implements hook_rules_action_base_upgrade().
  224. */
  225. function mimemail_rules_action_mail_to_users_of_role_upgrade($element, RulesPlugin $target) {
  226. $target->settings['roles'] = $element['#settings']['recipients'];
  227. mimemail_rules_action_mail_upgrade($element, $target);
  228. }
  229. /**
  230. * Action Implementation: Send HTML mail.
  231. */
  232. function rules_action_mimemail($key, $to, $cc = NULL, $bcc = NULL, $from_name = NULL, $from_mail = NULL, $reply_to = NULL, $subject, $body, $plaintext = NULL, $attachments = array(), $langcode, $settings, RulesState $state, RulesPlugin $element) {
  233. module_load_include('inc', 'mimemail');
  234. // Set the sender name and from address.
  235. if (empty($from_mail)) {
  236. $from = NULL;
  237. }
  238. else {
  239. $from = array(
  240. 'name' => $from_name,
  241. 'mail' => $from_mail,
  242. );
  243. // Create an address string.
  244. $from = mimemail_address($from);
  245. }
  246. // Figure out the language to use - fallback is the system default.
  247. $languages = language_list();
  248. $language = isset($languages[$langcode]) ? $languages[$langcode] : language_default();
  249. $params = array(
  250. 'context' => array(
  251. 'subject' => $subject,
  252. 'body' => $body,
  253. 'action' => $element,
  254. 'state' => $state,
  255. ),
  256. 'cc' => $cc,
  257. 'bcc' => $bcc,
  258. 'reply-to' => $reply_to,
  259. 'plaintext' => $plaintext,
  260. 'attachments' => $attachments,
  261. );
  262. drupal_mail('mimemail', $key, $to, $language, $params, $from);
  263. }
  264. /**
  265. * Action: Send HTML mail to all users of a specific role group(s).
  266. */
  267. 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) {
  268. module_load_include('inc', 'mimemail');
  269. // Set the sender name and from address.
  270. if (empty($from_mail)) {
  271. $from = NULL;
  272. }
  273. else {
  274. $from = array(
  275. 'name' => $from_name,
  276. 'mail' => $from_mail,
  277. );
  278. // Create an address string.
  279. $from = mimemail_address($from);
  280. }
  281. $query = db_select('users', 'u');
  282. $query->fields('u', array('mail', 'language'));
  283. if ($active) {
  284. $query->condition('u.status', 1, '=');
  285. }
  286. if (in_array(DRUPAL_AUTHENTICATED_RID, $roles)) {
  287. $query->condition('u.uid', 0, '>');
  288. }
  289. else {
  290. $query->join('users_roles', 'r', 'u.uid = r.uid');
  291. $query->condition('r.rid', $roles, 'IN');
  292. $query->distinct();
  293. }
  294. $result = $query->execute();
  295. $params = array(
  296. 'context' => array(
  297. 'subject' => $subject,
  298. 'body' => $body,
  299. 'action' => $element,
  300. 'state' => $state,
  301. ),
  302. 'plaintext' => $plaintext,
  303. 'attachments' => $attachments,
  304. );
  305. // Create language list before initializing foreach.
  306. $languages = language_list();
  307. $message = array('result' => TRUE);
  308. foreach ($result as $row) {
  309. // Decide which language to use.
  310. if (!$use_userlang || empty($row->language) || !isset($languages[$row->language])) {
  311. $language = isset($languages[$langcode]) ? $languages[$langcode] : language_default();
  312. }
  313. else {
  314. $language = $languages[$row->language];
  315. }
  316. $message = drupal_mail('mimemail', $key, $row->mail, $language, $params, $from);
  317. if (!$message['result']) {
  318. break;
  319. }
  320. }
  321. if ($message['result']) {
  322. $role_names = array_intersect_key(user_roles(TRUE), array_flip($roles));
  323. watchdog('rules', 'Successfully sent HTML email to the role(s) %roles.', array('%roles' => implode(', ', $role_names)));
  324. }
  325. }
  326. /**
  327. * @}
  328. */