mimemail.rules.inc 13 KB

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