mail.lib.inc.php 6.5 KB

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