mail.lib.inc.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. require(api_get_path(INCLUDE_PATH).'lib/phpmailer/class.phpmailer.php');
  3. //regular expression to test for valid email address
  4. $regexp = "^[0-9a-z_\.-]+@(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z][0-9a-z-]*[0-9a-z]\.)+[a-z]{2,3})$";
  5. /**
  6. * Sends email using the phpmailer class
  7. * Sender name and email can be specified, if not specified
  8. * name and email of the platform admin are used
  9. *
  10. * @author Bert Vanderkimpen ICT&O UGent
  11. *
  12. * @param recipient_name name of recipient
  13. * @param recipient_email email of recipient
  14. * @param message email body
  15. * @param subject email subject
  16. * @return returns true if mail was sent
  17. * @see class.phpmailer.php
  18. */
  19. function api_mail($recipient_name, $recipient_email, $subject, $message, $sender_name="", $sender_email="", $extra_headers="") {
  20. global $regexp;
  21. global $platform_email;
  22. $mail = new PHPMailer();
  23. $mail->Mailer = $platform_email['SMTP_MAILER'];
  24. $mail->Host = $platform_email['SMTP_HOST'];
  25. $mail->Port = $platform_email['SMTP_PORT'];
  26. if($platform_email['SMTP_AUTH'])
  27. {
  28. $mail->SMTPAuth = 1;
  29. $mail->Username = $platform_email['SMTP_USER'];
  30. $mail->Password = $platform_email['SMTP_PASS'];
  31. }
  32. $mail->Priority = 3; // 5=low, 1=high
  33. $mail->AddCustomHeader("Errors-To: ".$platform_email['SMTP_FROM_EMAIL']."");
  34. $mail->IsHTML(0);
  35. $mail->SMTPKeepAlive = true;
  36. // attachments
  37. // $mail->AddAttachment($path);
  38. // $mail->AddAttachment($path,$filename);
  39. if ($sender_email!="")
  40. {
  41. $mail->From = $sender_email;
  42. $mail->Sender = $sender_email;
  43. //$mail->ConfirmReadingTo = $sender_email; //Disposition-Notification
  44. }
  45. else
  46. {
  47. $mail->From = $platform_email['SMTP_FROM_EMAIL'];
  48. $mail->Sender = $platform_email['SMTP_FROM_EMAIL'];
  49. //$mail->ConfirmReadingTo = $platform_email['SMTP_FROM_EMAIL']; //Disposition-Notification
  50. }
  51. if ($sender_name!="")
  52. {
  53. $mail->FromName = $sender_name;
  54. }
  55. else
  56. {
  57. $mail->FromName = $platform_email['SMTP_FROM_NAME'];
  58. }
  59. $mail->Subject = $subject;
  60. $mail->Body = $message;
  61. //only valid address
  62. if(eregi( $regexp, $recipient_email ))
  63. {
  64. $mail->AddAddress($recipient_email, $recipient_name);
  65. }
  66. if ($extra_headers != ""){
  67. $mail->AddCustomHeader($extra_headers);
  68. }
  69. //send mail
  70. if (!$mail->Send())
  71. {
  72. //echo "ERROR: mail not sent to ".$recipient_name." (".$recipient_email.") because of ".$mail->ErrorInfo."<br>";
  73. return 0;
  74. }
  75. // Clear all addresses
  76. $mail->ClearAddresses();
  77. return 1;
  78. }
  79. ?>