mail.lib.inc.php 7.8 KB

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