mail.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Send an email to a specific address, using the Moodle system.
  4. * This function is heavily based on "email_to_user()" from Moodle's libraries.
  5. *
  6. * @uses $CFG
  7. * @uses $FULLME
  8. * @param string $to The address to send the email to
  9. * @param string $subject Plain text subject line of the email
  10. * @param string $messagetext Plain text of the message
  11. * @return bool Returns true if mail was sent OK, or false otherwise
  12. */
  13. function sloodle_text_email($to, $subject, $messagetext)
  14. {
  15. global $CFG, $FULLME;
  16. // Fetch the PHP mailing functionality
  17. include_once($CFG->libdir .'/phpmailer/class.phpmailer.php');
  18. // We are going to use textlib services here
  19. $textlib = textlib_get_instance();
  20. // Construct a new PHP mailer
  21. $mail = new phpmailer;
  22. $mail->Version = 'Moodle '. $CFG->version; // mailer version
  23. $mail->PluginDir = $CFG->libdir .'/phpmailer/'; // plugin directory (eg smtp plugin)
  24. // We will use Unicode UTF8
  25. $mail->CharSet = 'UTF-8';
  26. // Determine which mail system to use
  27. if ($CFG->smtphosts == 'qmail') {
  28. $mail->IsQmail(); // use Qmail system
  29. } else if (empty($CFG->smtphosts)) {
  30. $mail->IsMail(); // use PHP mail() = sendmail
  31. } else {
  32. $mail->IsSMTP(); // use SMTP directly
  33. if (!empty($CFG->debugsmtp)) {
  34. echo '<pre>' . "\n";
  35. $mail->SMTPDebug = true;
  36. }
  37. $mail->Host = $CFG->smtphosts; // specify main and backup servers
  38. if ($CFG->smtpuser) { // Use SMTP authentication
  39. $mail->SMTPAuth = true;
  40. $mail->Username = $CFG->smtpuser;
  41. $mail->Password = $CFG->smtppass;
  42. }
  43. }
  44. // Use the admin's address for the Sender field
  45. $adminuser = get_admin();
  46. $mail->Sender = $adminuser->email;
  47. // Use the 'noreply' address
  48. $mail->From = $CFG->noreplyaddress;
  49. $mail->FromName = $CFG->wwwroot;
  50. // Setup the other headers
  51. $mail->Subject = substr(stripslashes($subject), 0, 900);
  52. $mail->AddAddress(stripslashes($to), 'Sloodle' );
  53. //$mail->WordWrap = 79; // We don't want to do a wordwrap
  54. // Add our message text
  55. $mail->IsHTML(false);
  56. $mail->Body = $messagetext;
  57. // Attempt to send the email
  58. if ($mail->Send()) {
  59. $mail->IsSMTP(); // use SMTP directly
  60. if (!empty($CFG->debugsmtp)) {
  61. echo '</pre>';
  62. }
  63. return true;
  64. } else {
  65. mtrace('ERROR: '. $mail->ErrorInfo);
  66. add_to_log(SITEID, 'library', 'mailer', $FULLME, 'ERROR: '. $mail->ErrorInfo);
  67. if (!empty($CFG->debugsmtp)) {
  68. echo '</pre>';
  69. }
  70. return false;
  71. }
  72. }
  73. /**
  74. * Send an email to an object in SL.
  75. *
  76. * @param string $uuid The UUID of the object to send the email to
  77. * @param string $subject Plain text subject line of the email
  78. * @param string $messagetext Plain text of the message
  79. * @return bool Returns true if mail was sent OK, or false otherwise
  80. */
  81. function sloodle_text_email_sl($uuid, $subject, $messagetext)
  82. {
  83. return sloodle_text_email($uuid.'@lsl.secondlife.com', $subject, $messagetext);
  84. }
  85. ?>