mimemail_example.module 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * @file
  4. * Core and contrib hook implementations for Mime Mail Example module.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function mimemail_example_menu() {
  10. $items['example/mimemail_example'] = array(
  11. 'title' => 'Mime Mail Example',
  12. 'page callback' => 'drupal_get_form',
  13. 'page arguments' => array('mimemail_example_form'),
  14. 'access arguments' => array('access content'),
  15. );
  16. return $items;
  17. }
  18. /**
  19. * Implements hook_mail().
  20. */
  21. function mimemail_example_mail($key, &$message, $params) {
  22. $message['subject'] = $params['subject'];
  23. $message['body'][] = $params['body'];
  24. }
  25. /**
  26. * The example email contact form.
  27. */
  28. function mimemail_example_form() {
  29. global $user;
  30. $form['intro'] = array(
  31. '#markup' => t('Use this form to send a HTML message to an e-mail address. No spamming!'),
  32. );
  33. $form['key'] = array(
  34. '#type' => 'textfield',
  35. '#title' => t('Key'),
  36. '#default_value' => 'test',
  37. '#required' => TRUE,
  38. );
  39. $form['to'] = array(
  40. '#type' => 'textfield',
  41. '#title' => t('To'),
  42. '#default_value' => $user->mail,
  43. '#required' => TRUE,
  44. );
  45. $form['from'] = array(
  46. '#type' => 'textfield',
  47. '#title' => t('Sender name'),
  48. );
  49. $form['from_mail'] = array(
  50. '#type' => 'textfield',
  51. '#title' => t('Sender e-mail address'),
  52. );
  53. $form['params'] = array(
  54. '#tree' => TRUE,
  55. 'headers' => array(
  56. 'Cc' => array(
  57. '#type' => 'textfield',
  58. '#title' => t('Cc'),
  59. ),
  60. 'Bcc' => array(
  61. '#type' => 'textfield',
  62. '#title' => t('Bcc'),
  63. ),
  64. 'Reply-to' => array(
  65. '#type' => 'textfield',
  66. '#title' => t('Reply to'),
  67. ),
  68. 'List-unsubscribe' => array(
  69. '#type' => 'textfield',
  70. '#title' => t('List-unsubscribe'),
  71. ),
  72. ),
  73. 'subject' => array(
  74. '#type' => 'textfield',
  75. '#title' => t('Subject'),
  76. ),
  77. 'body' => array(
  78. '#type' => 'textarea',
  79. '#title' => t('HTML message'),
  80. ),
  81. 'plain' => array(
  82. '#type' => 'hidden',
  83. '#states' => array(
  84. 'value' => array(
  85. ':input[name="body"]' => array('value' => ''),
  86. ),
  87. ),
  88. ),
  89. 'plaintext' => array(
  90. '#type' => 'textarea',
  91. '#title' => t('Plain text message'),
  92. ),
  93. 'attachments' => array(
  94. '#name' => 'files[attachment]',
  95. '#type' => 'file',
  96. '#title' => t('Choose a file to send as attachment'),
  97. ),
  98. );
  99. $form['submit'] = array(
  100. '#type' => 'submit',
  101. '#value' => t('Send message'),
  102. );
  103. return $form;
  104. }
  105. /**
  106. * Form validation logic for the email contact form.
  107. */
  108. function mimemail_example_form_validate($form, &$form_state) {
  109. $values = &$form_state['values'];
  110. if (!valid_email_address($values['to'])) {
  111. form_set_error('to', t('That e-mail address is not valid.'));
  112. }
  113. $file = file_save_upload('attachment');
  114. if ($file) {
  115. $file = file_move($file, 'public://');
  116. $values['params']['attachments'][] = array(
  117. 'filepath' => $file->uri,
  118. );
  119. }
  120. }
  121. /**
  122. * Form submission logic for the email contact form.
  123. */
  124. function mimemail_example_form_submit($form, &$form_state) {
  125. $values = $form_state['values'];
  126. $module = 'mimemail_example';
  127. $key = $values['key'];
  128. $to = $values['to'];
  129. $language = language_default();
  130. $params = $values['params'];
  131. if (!empty($values['from_mail'])) {
  132. module_load_include('inc', 'mimemail');
  133. $from = mimemail_address(array(
  134. 'name' => $values['from'],
  135. 'mail' => $values['from_mail'],
  136. ));
  137. }
  138. else {
  139. $from = $values['from'];
  140. }
  141. $send = TRUE;
  142. $result = drupal_mail($module, $key, $to, $language, $params, $from, $send);
  143. if ($result['result'] == TRUE) {
  144. drupal_set_message(t('Your message has been sent.'));
  145. }
  146. else {
  147. drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error');
  148. }
  149. }