mail.lib.inc.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. */
  29. function api_mail($recipient_name, $recipient_email, $subject, $message, $sender_name = '', $sender_email = '', $extra_headers = '') {
  30. if (!api_valid_email($recipient_email)) {
  31. return 0;
  32. }
  33. //global $regexp_rfc3696; // Deprecated, 13-OCT-2010.
  34. global $platform_email;
  35. $mail = new PHPMailer();
  36. $mail->Mailer = $platform_email['SMTP_MAILER'];
  37. $mail->Host = $platform_email['SMTP_HOST'];
  38. $mail->Port = $platform_email['SMTP_PORT'];
  39. $mail->CharSet = $platform_email['SMTP_CHARSET'];
  40. $mail->WordWrap = 200; // Stay far below SMTP protocol 980 chars limit.
  41. if ($platform_email['SMTP_AUTH']) {
  42. $mail->SMTPAuth = 1;
  43. $mail->Username = $platform_email['SMTP_USER'];
  44. $mail->Password = $platform_email['SMTP_PASS'];
  45. }
  46. $mail->Priority = 3; // 5 = low, 1 = high
  47. $mail->AddCustomHeader('Errors-To: '.$platform_email['SMTP_FROM_EMAIL']);
  48. $mail->IsHTML(0);
  49. $mail->SMTPKeepAlive = true;
  50. // Attachments
  51. // $mail->AddAttachment($path);
  52. // $mail->AddAttachment($path, $filename);
  53. if ($sender_email != '') {
  54. $mail->From = $sender_email;
  55. $mail->Sender = $sender_email;
  56. //$mail->ConfirmReadingTo = $sender_email; // Disposition-Notification
  57. } else {
  58. $mail->From = $platform_email['SMTP_FROM_EMAIL'];
  59. $mail->Sender = $platform_email['SMTP_FROM_EMAIL'];
  60. //$mail->ConfirmReadingTo = $platform_email['SMTP_FROM_EMAIL']; // Disposition-Notification
  61. }
  62. if ($sender_name != '') {
  63. $mail->FromName = $sender_name;
  64. } else {
  65. $mail->FromName = $platform_email['SMTP_FROM_NAME'];
  66. }
  67. $mail->Subject = $subject;
  68. $mail->Body = $message;
  69. // Only valid addresses are accepted.
  70. //if (eregi($regexp_rfc3696, $recipient_email)) { // Deprecated, 13-OCT-2010.
  71. if (api_valid_email($recipient_email)) {
  72. $mail->AddAddress($recipient_email, $recipient_name);
  73. }
  74. if ($extra_headers != '') {
  75. $mail->AddCustomHeader($extra_headers);
  76. }
  77. // Send the mail message.
  78. if (!$mail->Send()) {
  79. error_log('ERROR: mail not sent to '.$recipient_name.' ('.$recipient_email.') because of '.$mail->ErrorInfo.'<br />');
  80. return 0;
  81. }
  82. // Clear all the addresses.
  83. $mail->ClearAddresses();
  84. return 1;
  85. }
  86. /**
  87. * Sends an HTML email using the phpmailer class (and multipart/alternative to downgrade gracefully)
  88. * Sender name and email can be specified, if not specified
  89. * name and email of the platform admin are used
  90. *
  91. * @author Bert Vanderkimpen ICT&O UGent
  92. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  93. *
  94. * @param string name of recipient
  95. * @param string email of recipient
  96. * @param string email subject
  97. * @param string email body
  98. * @param string sender name
  99. * @param string sender e-mail
  100. * @param array extra headers in form $headers = array($name => $value) to allow parsing
  101. * @param array data file (path and filename)
  102. * @param array data to attach a file (optional)
  103. * @param bool True for attaching a embedded file inside content html (optional)
  104. * @return returns true if mail was sent
  105. * @see class.phpmailer.php
  106. */
  107. function api_mail_html($recipient_name, $recipient_email, $subject, $message, $sender_name = '', $sender_email = '', $extra_headers = null, $data_file = array(), $embedded_image = false) {
  108. //global $regexp_rfc3696; // Deprecated, 13-OCT-2010.
  109. global $platform_email;
  110. $mail = new PHPMailer();
  111. $mail->Mailer = $platform_email['SMTP_MAILER'];
  112. $mail->Host = $platform_email['SMTP_HOST'];
  113. $mail->Port = $platform_email['SMTP_PORT'];
  114. $mail->CharSet = $platform_email['SMTP_CHARSET'];
  115. $mail->WordWrap = 200; // Stay far below SMTP protocol 980 chars limit.
  116. if ($platform_email['SMTP_AUTH']) {
  117. $mail->SMTPAuth = 1;
  118. $mail->Username = $platform_email['SMTP_USER'];
  119. $mail->Password = $platform_email['SMTP_PASS'];
  120. }
  121. $mail->Priority = 3; // 5 = low, 1 = high
  122. $mail->AddCustomHeader('Errors-To: '.$platform_email['SMTP_FROM_EMAIL']);
  123. $mail->IsHTML(0);
  124. $mail->SMTPKeepAlive = true;
  125. if (($sender_email != '') && ($sender_name != '')) {
  126. $mail->AddReplyTo($sender_email, $sender_name);
  127. }
  128. // Attachments
  129. // $mail->AddAttachment($path);
  130. // $mail->AddAttachment($path, $filename);
  131. if ($sender_email != '') {
  132. $mail->From = $sender_email;
  133. $mail->Sender = $sender_email;
  134. //$mail->ConfirmReadingTo = $sender_email; // Disposition-Notification
  135. } else {
  136. $mail->From = $platform_email['SMTP_FROM_EMAIL'];
  137. $mail->Sender = $platform_email['SMTP_FROM_EMAIL'];
  138. //$mail->ConfirmReadingTo = $platform_email['SMTP_FROM_EMAIL']; // Disposition-Notification
  139. }
  140. if ($sender_name != '') {
  141. $mail->FromName = $sender_name;
  142. } else {
  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. // Send embedded image.
  148. if ($embedded_image) {
  149. // Get all images html inside content.
  150. preg_match_all("/<img\s+.*?src=[\"\']?([^\"\' >]*)[\"\']?[^>]*>/i", $message, $m);
  151. // Prepare new tag images.
  152. $new_images_html = array();
  153. $i = 1;
  154. if (!empty($m[1])) {
  155. foreach ($m[1] as $image_path) {
  156. $real_path = realpath($image_path);
  157. $filename = basename($image_path);
  158. $image_cid = $filename.'_'.$i;
  159. $encoding = 'base64';
  160. $image_type = mime_content_type($real_path);
  161. $mail->AddEmbeddedImage($real_path, $image_cid, $filename, $encoding, $image_type);
  162. $new_images_html[] = '<img src="cid:'.$image_cid.'" />';
  163. $i++;
  164. }
  165. }
  166. // Replace origin image for new embedded image html.
  167. $x = 0;
  168. if (!empty($m[0])) {
  169. foreach ($m[0] as $orig_img) {
  170. $message = str_replace($orig_img, $new_images_html[$x], $message);
  171. $x++;
  172. }
  173. }
  174. }
  175. $message = str_replace(array("\n\r", "\n", "\r"), '<br />', $message);
  176. $mail->Body = '<html><head></head><body>'.$message.'</body></html>';
  177. // Attachment ...
  178. if (!empty($data_file)) {
  179. $mail->AddAttachment($data_file['path'], $data_file['filename']);
  180. }
  181. // Only valid addresses are accepted.
  182. if (is_array($recipient_email)) {
  183. foreach ($recipient_email as $dest) {
  184. //if (eregi($regexp_rfc3696, $dest)) { // Deprecated, 13-OCT-2010.
  185. if (api_valid_email($dest)) {
  186. $mail->AddAddress($dest, $recipient_name);
  187. //$mail->AddAddress($dest, ($i > 1 ? '' : $recipient_name));
  188. }
  189. }
  190. } else {
  191. //if (eregi($regexp_rfc3696, $recipient_email)) { // Deprecated, 13-OCT-2010.
  192. if (api_valid_email($recipient_email)) {
  193. $mail->AddAddress($recipient_email, $recipient_name);
  194. } else {
  195. return 0;
  196. }
  197. }
  198. if (is_array($extra_headers) && count($extra_headers) > 0) {
  199. foreach ($extra_headers as $key => $value) {
  200. switch (strtolower($key)) {
  201. case 'encoding':
  202. case 'content-transfer-encoding':
  203. $mail->Encoding = $value;
  204. break;
  205. case 'charset':
  206. $mail->Charset = $value;
  207. break;
  208. case 'contenttype':
  209. case 'content-type':
  210. $mail->ContentType = $value;
  211. break;
  212. default:
  213. $mail->AddCustomHeader($key.':'.$value);
  214. break;
  215. }
  216. }
  217. }
  218. // WordWrap the html body (phpMailer only fixes AltBody) FS#2988
  219. $mail->Body = $mail->WrapText($mail->Body, $mail->WordWrap);
  220. // Send the mail message.
  221. if (!$mail->Send()) {
  222. //echo 'ERROR: mail not sent to '.$recipient_name.' ('.$recipient_email.') because of '.$mail->ErrorInfo.'<br />';
  223. return 0;
  224. }
  225. // Clear all the addresses.
  226. $mail->ClearAddresses();
  227. return 1;
  228. }