mail.lib.inc.php 7.6 KB

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