mail.lib.inc.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 = array(), $data_file = array(), $embedded_image = false)
  55. {
  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. if (isset($extra_headers['reply_to'])) {
  75. $mail->AddReplyTo($extra_headers['reply_to']['mail'], $extra_headers['reply_to']['name']);
  76. }
  77. // Attachments
  78. // $mail->AddAttachment($path);
  79. // $mail->AddAttachment($path, $filename);
  80. if ($sender_email != '') {
  81. $mail->From = $sender_email;
  82. $mail->Sender = $sender_email;
  83. //$mail->ConfirmReadingTo = $sender_email; // Disposition-Notification
  84. } else {
  85. $mail->From = $platform_email['SMTP_FROM_EMAIL'];
  86. $mail->Sender = $platform_email['SMTP_FROM_EMAIL'];
  87. //$mail->ConfirmReadingTo = $platform_email['SMTP_FROM_EMAIL']; // Disposition-Notification
  88. }
  89. if ($sender_name != '') {
  90. $mail->FromName = $sender_name;
  91. } else {
  92. $mail->FromName = $platform_email['SMTP_FROM_NAME'];
  93. }
  94. $mail->Subject = $subject;
  95. $mail->AltBody = strip_tags(str_replace('<br />',"\n", api_html_entity_decode($message)));
  96. // Send embedded image.
  97. if ($embedded_image) {
  98. // Get all images html inside content.
  99. preg_match_all("/<img\s+.*?src=[\"\']?([^\"\' >]*)[\"\']?[^>]*>/i", $message, $m);
  100. // Prepare new tag images.
  101. $new_images_html = array();
  102. $i = 1;
  103. if (!empty($m[1])) {
  104. foreach ($m[1] as $image_path) {
  105. $real_path = realpath($image_path);
  106. $filename = basename($image_path);
  107. $image_cid = $filename.'_'.$i;
  108. $encoding = 'base64';
  109. $image_type = mime_content_type($real_path);
  110. $mail->AddEmbeddedImage($real_path, $image_cid, $filename, $encoding, $image_type);
  111. $new_images_html[] = '<img src="cid:'.$image_cid.'" />';
  112. $i++;
  113. }
  114. }
  115. // Replace origin image for new embedded image html.
  116. $x = 0;
  117. if (!empty($m[0])) {
  118. foreach ($m[0] as $orig_img) {
  119. $message = str_replace($orig_img, $new_images_html[$x], $message);
  120. $x++;
  121. }
  122. }
  123. }
  124. $message = str_replace(array("\n\r", "\n", "\r"), '<br />', $message);
  125. $mail->Body = '<html><head></head><body>'.$message.'</body></html>';
  126. // Attachment ...
  127. if (!empty($data_file)) {
  128. $mail->AddAttachment($data_file['path'], $data_file['filename']);
  129. }
  130. // Only valid addresses are accepted.
  131. if (is_array($recipient_email)) {
  132. foreach ($recipient_email as $dest) {
  133. if (api_valid_email($dest)) {
  134. $mail->AddAddress($dest, $recipient_name);
  135. //$mail->AddAddress($dest, ($i > 1 ? '' : $recipient_name));
  136. }
  137. }
  138. } else {
  139. if (api_valid_email($recipient_email)) {
  140. $mail->AddAddress($recipient_email, $recipient_name);
  141. } else {
  142. return 0;
  143. }
  144. }
  145. if (is_array($extra_headers) && count($extra_headers) > 0) {
  146. foreach ($extra_headers as $key => $value) {
  147. switch (strtolower($key)) {
  148. case 'encoding':
  149. case 'content-transfer-encoding':
  150. $mail->Encoding = $value;
  151. break;
  152. case 'charset':
  153. $mail->Charset = $value;
  154. break;
  155. case 'contenttype':
  156. case 'content-type':
  157. $mail->ContentType = $value;
  158. break;
  159. case 'reply_to':
  160. if (isset($value['mail']) && isset($value['name'])) {
  161. $mail->AddReplyTo($value['mail'], $value['name']);
  162. }
  163. break;
  164. default:
  165. $mail->AddCustomHeader($key.':'.$value);
  166. break;
  167. }
  168. }
  169. } else {
  170. if (!empty($extra_headers)) {
  171. $mail->AddCustomHeader($extra_headers);
  172. }
  173. }
  174. // WordWrap the html body (phpMailer only fixes AltBody) FS#2988
  175. $mail->Body = $mail->WrapText($mail->Body, $mail->WordWrap);
  176. // Send the mail message.
  177. if (!$mail->Send()) {
  178. //echo 'ERROR: mail not sent to '.$recipient_name.' ('.$recipient_email.') because of '.$mail->ErrorInfo.'<br />';
  179. error_log('ERROR: mail not sent to '.$recipient_name.' ('.$recipient_email.') because of '.$mail->ErrorInfo.'<br />');
  180. return 0;
  181. }
  182. // Clear all the addresses.
  183. $mail->ClearAddresses();
  184. return 1;
  185. }