mail.lib.inc.php 9.1 KB

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