mail.lib.inc.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. require(api_get_path(INCLUDE_PATH).'lib/phpmailer/class.phpmailer.php');
  3. require_once(api_get_path(INCLUDE_PATH).'/conf/mail.conf.php');
  4. //regular expression to test for valid email address
  5. $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})$";
  6. /**
  7. * Sends email using the phpmailer class
  8. * Sender name and email can be specified, if not specified
  9. * name and email of the platform admin are used
  10. *
  11. * @author Bert Vanderkimpen ICT&O UGent
  12. *
  13. * @param recipient_name name of recipient
  14. * @param recipient_email email of recipient
  15. * @param message email body
  16. * @param subject email subject
  17. * @return returns true if mail was sent
  18. * @see class.phpmailer.php
  19. */
  20. function api_mail($recipient_name, $recipient_email, $subject, $message, $sender_name="", $sender_email="", $extra_headers="") {
  21. global $regexp;
  22. global $platform_email;
  23. $mail = new PHPMailer();
  24. $mail->Mailer = $platform_email['SMTP_MAILER'];
  25. $mail->Host = $platform_email['SMTP_HOST'];
  26. $mail->Port = $platform_email['SMTP_PORT'];
  27. if($platform_email['SMTP_AUTH'])
  28. {
  29. $mail->SMTPAuth = 1;
  30. $mail->Username = $platform_email['SMTP_USER'];
  31. $mail->Password = $platform_email['SMTP_PASS'];
  32. }
  33. $mail->Priority = 3; // 5=low, 1=high
  34. $mail->AddCustomHeader("Errors-To: ".$platform_email['SMTP_FROM_EMAIL']."");
  35. $mail->IsHTML(0);
  36. $mail->SMTPKeepAlive = true;
  37. // attachments
  38. // $mail->AddAttachment($path);
  39. // $mail->AddAttachment($path,$filename);
  40. if ($sender_email!="")
  41. {
  42. $mail->From = $sender_email;
  43. $mail->Sender = $sender_email;
  44. //$mail->ConfirmReadingTo = $sender_email; //Disposition-Notification
  45. }
  46. else
  47. {
  48. $mail->From = $platform_email['SMTP_FROM_EMAIL'];
  49. $mail->Sender = $platform_email['SMTP_FROM_EMAIL'];
  50. //$mail->ConfirmReadingTo = $platform_email['SMTP_FROM_EMAIL']; //Disposition-Notification
  51. }
  52. if ($sender_name!="")
  53. {
  54. $mail->FromName = $sender_name;
  55. }
  56. else
  57. {
  58. $mail->FromName = $platform_email['SMTP_FROM_NAME'];
  59. }
  60. $mail->Subject = $subject;
  61. $mail->Body = $message;
  62. //only valid address
  63. if(eregi( $regexp, $recipient_email ))
  64. {
  65. $mail->AddAddress($recipient_email, $recipient_name);
  66. }
  67. if ($extra_headers != ""){
  68. $mail->AddCustomHeader($extra_headers);
  69. }
  70. //send mail
  71. if (!$mail->Send())
  72. {
  73. //echo "ERROR: mail not sent to ".$recipient_name." (".$recipient_email.") because of ".$mail->ErrorInfo."<br>";
  74. return 0;
  75. }
  76. // Clear all addresses
  77. $mail->ClearAddresses();
  78. return 1;
  79. }
  80. /**
  81. * Sends an HTML email using the phpmailer class (and multipart/alternative to downgrade gracefully)
  82. * Sender name and email can be specified, if not specified
  83. * name and email of the platform admin are used
  84. *
  85. * @author Bert Vanderkimpen ICT&O UGent
  86. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  87. *
  88. * @param string name of recipient
  89. * @param string email of recipient
  90. * @param string email subject
  91. * @param string email body
  92. * @param string sender name
  93. * @param string sender e-mail
  94. * @param array extra headers in form $headers = array($name => $value) to allow parsing
  95. * @return returns true if mail was sent
  96. * @see class.phpmailer.php
  97. */
  98. function api_mail_html($recipient_name, $recipient_email, $subject, $message, $sender_name="", $sender_email="", $extra_headers=null) {
  99. global $regexp;
  100. global $platform_email;
  101. $mail = new PHPMailer();
  102. $mail->Mailer = $platform_email['SMTP_MAILER'];
  103. $mail->Host = $platform_email['SMTP_HOST'];
  104. $mail->Port = $platform_email['SMTP_PORT'];
  105. if($platform_email['SMTP_AUTH'])
  106. {
  107. $mail->SMTPAuth = 1;
  108. $mail->Username = $platform_email['SMTP_USER'];
  109. $mail->Password = $platform_email['SMTP_PASS'];
  110. }
  111. $mail->Priority = 3; // 5=low, 1=high
  112. $mail->AddCustomHeader("Errors-To: ".$platform_email['SMTP_FROM_EMAIL']."");
  113. $mail->IsHTML(0);
  114. $mail->SMTPKeepAlive = true;
  115. // attachments
  116. // $mail->AddAttachment($path);
  117. // $mail->AddAttachment($path,$filename);
  118. if ($sender_email!="")
  119. {
  120. $mail->From = $sender_email;
  121. $mail->Sender = $sender_email;
  122. //$mail->ConfirmReadingTo = $sender_email; //Disposition-Notification
  123. }
  124. else
  125. {
  126. $mail->From = $platform_email['SMTP_FROM_EMAIL'];
  127. $mail->Sender = $platform_email['SMTP_FROM_EMAIL'];
  128. //$mail->ConfirmReadingTo = $platform_email['SMTP_FROM_EMAIL']; //Disposition-Notification
  129. }
  130. if ($sender_name!="")
  131. {
  132. $mail->FromName = $sender_name;
  133. }
  134. else
  135. {
  136. $mail->FromName = $platform_email['SMTP_FROM_NAME'];
  137. }
  138. $mail->Subject = $subject;
  139. $mail->AltBody = strip_tags(str_replace('<br />',"\n",$message));
  140. $mail->Body = '<html><head></head><body>'.$message.'</body></html>';
  141. //only valid address
  142. if(is_array($recipient_email))
  143. {
  144. $i = 0;
  145. foreach($recipient_email as $dest)
  146. {
  147. if(eregi( $regexp, $dest ))
  148. {
  149. $mail->AddAddress($dest, ($i>1?'':$recipient_name));
  150. }
  151. $i++;
  152. }
  153. }
  154. else
  155. {
  156. if(eregi( $regexp, $recipient_email ))
  157. {
  158. $mail->AddAddress($recipient_email, $recipient_name);
  159. }
  160. }
  161. if (is_array($extra_headers) && count($extra_headers)>0){
  162. foreach($extra_headers as $key=>$value)
  163. {
  164. switch(strtolower($key)){
  165. case 'encoding':
  166. case 'content-transfer-encoding':
  167. $mail->Encoding = $value;
  168. break;
  169. case 'charset':
  170. $mail->Charset = $value;
  171. break;
  172. case 'contenttype':
  173. case 'content-type':
  174. $mail->ContentType = $value;
  175. break;
  176. default:
  177. $mail->AddCustomHeader($key.':'.$value);
  178. break;
  179. }
  180. }
  181. }
  182. //send mail
  183. if (!$mail->Send())
  184. {
  185. //echo "ERROR: mail not sent to ".$recipient_name." (".$recipient_email.") because of ".$mail->ErrorInfo."<br>";
  186. return 0;
  187. }
  188. // Clear all addresses
  189. $mail->ClearAddresses();
  190. return 1;
  191. }
  192. ?>