You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 line
1.6KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace Core;
  4. use PHPMailer\PHPMailer\PHPMailer;
  5. use PHPMailer\PHPMailer\SMTP;
  6. use PHPMailer\PHPMailer\Exception as MailerException;
  7. class Mailer
  8. {
  9. private array $config;
  10. public function __construct()
  11. {
  12. $this->config = require __DIR__ . '/../config/mail.php';
  13. }
  14. /**
  15. * Send an email.
  16. *
  17. * @param string $toEmail
  18. * @param string $toName
  19. * @param string $subject
  20. * @param string $htmlBody
  21. * @param string $textBody Plain-text fallback (optional)
  22. * @throws MailerException
  23. */
  24. public function send(
  25. string $toEmail,
  26. string $toName,
  27. string $subject,
  28. string $htmlBody,
  29. string $textBody = ''
  30. ): void {
  31. $mail = new PHPMailer(true);
  32. $mail->isSMTP();
  33. $mail->Host = $this->config['host'];
  34. $mail->SMTPAuth = true;
  35. $mail->Username = $this->config['username'];
  36. $mail->Password = $this->config['password'];
  37. $mail->SMTPSecure = $this->config['encryption'] === 'ssl' ? PHPMailer::ENCRYPTION_SMTPS : PHPMailer::ENCRYPTION_STARTTLS;
  38. $mail->Port = $this->config['port'];
  39. $mail->SMTPDebug = $this->config['debug'];
  40. $mail->setFrom($this->config['from_email'], $this->config['from_name']);
  41. $mail->addAddress($toEmail, $toName);
  42. $mail->isHTML(true);
  43. $mail->Subject = $subject;
  44. $mail->Body = $htmlBody;
  45. $mail->AltBody = $textBody ?: strip_tags($htmlBody);
  46. $mail->send();
  47. }
  48. }

Powered by TurnKey Linux.