Email_lib.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace KarmaFW\Lib\Email;
  3. use \PHPMailer\PHPMailer\PHPMailer;
  4. use \PHPMailer\PHPMailer\Exception;
  5. class Email_lib
  6. {
  7. public static function sendmail($to, $subject, $message, $from, $from_name=null)
  8. {
  9. $headers = "";
  10. return mail($to, $subject, $message, $headers);
  11. }
  12. public static function sendmailSMTP($to, $subject, $message_html, $message_text='', $from=null, $from_name=null, $options=[]) {
  13. if (defined('SMTP_HOOK') && empty($options['no_hook'])) {
  14. $smtp_hook_func = SMTP_HOOK;
  15. if (is_callable($smtp_hook_func)) {
  16. return $smtp_hook_func($to, $subject, $message_html, $message_text, $from, $from_name);
  17. }
  18. // aucun message envoyé ni hook
  19. return boolval($smtp_hook_func); // permet de mettre "1" pour un retour ok ou "0" pour un retour en erreur
  20. }
  21. if (false) {
  22. $mail = [
  23. 'from' => $from,
  24. 'from_name' => $from_name,
  25. 'to' => $to,
  26. 'subject' => $subject,
  27. 'message_html' => $message_html,
  28. 'message_text' => $message_text,
  29. ];
  30. //echo "Emails désactivés";
  31. //pre($mail, 1);
  32. return false;
  33. }
  34. $mail = new PHPmailer();
  35. $mail->IsSMTP();
  36. $mail->IsHTML(! empty($message_html));
  37. $mail->CharSet = "UTF-8";
  38. if (defined('SMTP_HOST') && ! empty(SMTP_HOST)) {
  39. $mail->SMTPAuth = SMTP_SMTPAUTH;
  40. $mail->Host = SMTP_HOST;
  41. $mail->Port = SMTP_PORT;
  42. $mail->Username = SMTP_USERNAME;
  43. $mail->Password = SMTP_PASSWORD;
  44. } else {
  45. $mail->Host = 'localhost';
  46. $mail->Port = 25;
  47. }
  48. if (empty($from)) {
  49. $processUser = posix_getpwuid(posix_geteuid());
  50. //$from = $processUser['name'] . "@" . gethostname();
  51. $from = $processUser['name'] . "@localhost";
  52. }
  53. $mail->From = $from;
  54. if (! empty($from_name)) {
  55. $mail->FromName = $from_name;
  56. }
  57. $mail->AddAddress($to);
  58. $mail->Subject = $subject;
  59. if (! empty($message_html)) {
  60. $mail->Body = $message_html;
  61. if (! empty($message_text)) {
  62. $mail->AltBody = $message_text;
  63. }
  64. } else {
  65. $mail->Body = $message_text;
  66. }
  67. //$mail->SMTPDebug = 4;
  68. if(!$mail->Send()){
  69. if (false) {
  70. echo $mail->ErrorInfo . PHP_EOL;
  71. print_r($mail);
  72. }
  73. $ok = false;
  74. } else {
  75. //echo 'Mail envoyé avec succès';
  76. $ok = true;
  77. }
  78. $mail->SmtpClose();
  79. unset($mail);
  80. return $ok;
  81. }
  82. }