Guest User

Untitled

a guest
Nov 6th, 2017
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. public function testSendMail() {
  2. $this->sendmail();
  3. }
  4.  
  5. function sendmail() {
  6. $from = 'fromaddress@gmail.com';
  7. $to = 'toaddress@gmail.com';
  8. $message = 'testcase mail';
  9. $language = 'en';
  10. $module = "test_mails";
  11. $key = "notification_mail";
  12. $send = TRUE;
  13. $subject = t("subject line goes here", array(NULL),array('langcode'=>'en'));
  14. $params = array(
  15. 'body' => 'hello from krishna',
  16. 'subject' => $subject
  17. );
  18. $result = drupal_mail($module, $key, $to, $language, $params, $from, $send);
  19. return 'Mail sent succesfully';
  20.  
  21. }
  22.  
  23. function test_mails_mail($key, &$message, $params) {
  24. switch ($key) {
  25. case 'notification_mail' :
  26. $message['subject'] = $params['subject'];
  27. //the email body is here, inside the $message array
  28. $message['body'][] = $params['body'];
  29. break;
  30. }
  31. }
  32.  
  33. name = sendmailtest
  34. description = sendmailtest
  35. core = 7.x
  36.  
  37. <?php
  38.  
  39. function sendmailtest_menu() {
  40. $items['sendmailtest'] = array(
  41. 'title' => 'send mail test',
  42. 'page callback' => 'drupal_get_form',
  43. 'page arguments' => array('sendmailtest'),
  44. 'access callback' => TRUE
  45. );
  46. return $items;
  47. }
  48.  
  49.  
  50. function sendmailtest($form, &$form_state){
  51.  
  52.  
  53. $form['submit'] = array(
  54. '#value' => 'Send Mail Test',
  55. '#type' => 'submit',
  56. );
  57. //Fundamental devolver el objeto-array form
  58. return $form;
  59. }
  60.  
  61. function sendmailtest_submit($form, &$form_state){
  62.  
  63. drupal_set_message('Test ok');
  64.  
  65. sendmailtest_send($form_state['values']);
  66.  
  67. }
  68.  
  69. function sendmailtest_send($form_values) {
  70. // All system mails need to specify the module and template key (mirrored from
  71. // hook_mail()) that the message they want to send comes from.
  72. $module = 'sendmailtest';
  73. $key = 'sendmailtest';
  74.  
  75. // Specify 'to' and 'from' addresses.
  76. //$to = $form_values['email'];
  77. $to = 'jeremiselxi@gmail.com';
  78. $from = variable_get('site_mail', 'admin@example.com');
  79.  
  80. // "params" loads in additional context for email content completion in
  81. // hook_mail(). In this case, we want to pass in the values the user entered
  82. // into the form, which include the message body in $form_values['message'].
  83. $params = $form_values;
  84.  
  85. // The language of the e-mail. This will one of three values:
  86. // - user_preferred_language(): Used for sending mail to a particular website
  87. // user, so that the mail appears in their preferred language.
  88. // - global $language: Used when sending a mail back to the user currently
  89. // viewing the site. This will send it in the language they're currently
  90. // using.
  91. // - language_default(): Used when sending mail to a pre-existing, 'neutral'
  92. // address, such as the system e-mail address, or when you're unsure of the
  93. // language preferences of the intended recipient.
  94. //
  95. // Since in our case, we are sending a message to a random e-mail address that
  96. // is not necessarily tied to a user account, we will use the site's default
  97. // language.
  98. $language = language_default();
  99.  
  100. // Whether or not to automatically send the mail when drupal_mail() is
  101. // called. This defaults to TRUE, and is normally what you want unless you
  102. // need to do additional processing before drupal_mail_send() is called.
  103. $send = TRUE;
  104. // Send the mail, and check for success. Note that this does not guarantee
  105. // message delivery; only that there were no PHP-related issues encountered
  106. // while sending.
  107. $result = drupal_mail($module, $key, $to, $language, $params, $from, $send);
  108. if ($result['result'] == TRUE) {
  109. drupal_set_message(t('Your message has been sent.'));
  110. }
  111. else {
  112. drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error');
  113. }
  114.  
  115. }
  116.  
  117.  
  118. function sendmailtest_mail($key, &$message, $params) {
  119. switch ($key) {
  120. // Send a simple message from the contact form.
  121. case 'sendmailtest':
  122.  
  123.  
  124. $message['subject'] = 'Test subject';
  125.  
  126. $body= 'Test Body';
  127. $message['body'][] = check_plain($body);
  128. break;
  129. }
  130. }
Add Comment
Please, Sign In to add comment