mail.lib.inc.php 7.3 KB

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