mail.lib.inc.php 6.5 KB

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