Skip to content

Commit

Permalink
Replace the internal email library with phpmailer for better smtp ser…
Browse files Browse the repository at this point in the history
…ver support
  • Loading branch information
alextselegidis committed Nov 21, 2024
1 parent bf3af80 commit c6d44fe
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 47 deletions.
110 changes: 65 additions & 45 deletions application/libraries/Email_messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
* @since v1.4.0
* ---------------------------------------------------------------------------- */

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

/**
* Email messages library.
*
Expand Down Expand Up @@ -59,6 +63,8 @@ public function __construct()
* @param string $ics_stream ICS file contents.
* @param string|null $timezone Custom timezone.
*
* @throws DateInvalidTimeZoneException
* @throws DateMalformedStringException
* @throws Exception
*/
public function send_appointment_saved(
Expand Down Expand Up @@ -106,27 +112,18 @@ public function send_appointment_saved(
true,
);

$from_name = config('from_name') ?: $settings['company_name'];
$from_address = config('from_address') ?: $settings['company_email'];
$reply_to = config('reply_to') ?: $settings['company_email'];

$this->CI->email->from($from_address, $from_name);

if ($reply_to) {
$this->CI->email->reply_to($reply_to);
}

$this->CI->email->to($recipient_email);
$php_mailer = $this->get_php_mailer();

$this->CI->email->subject($subject);
$php_mailer->addAddress($recipient_email);

$this->CI->email->message($html);
$php_mailer->isHTML();
$php_mailer->Subject = $subject;
$php_mailer->Body = $html;
$php_mailer->AltBody = $html;

$this->CI->email->attach($ics_stream, 'attachment', 'invitation.ics', 'text/vcalendar');
$php_mailer->addStringAttachment($ics_stream, 'invitation.ics', PHPMailer::ENCODING_BASE64, 'text/vcalendar');

if (!$this->CI->email->send(false)) {
throw new RuntimeException('Email was not sent: ' . $this->CI->email->print_debugger());
}
$php_mailer->send();
}

/**
Expand All @@ -141,6 +138,8 @@ public function send_appointment_saved(
* @param string|null $reason Removal reason.
* @param string|null $timezone Custom timezone.
*
* @throws DateInvalidTimeZoneException
* @throws DateMalformedStringException
* @throws Exception
*/
public function send_appointment_deleted(
Expand Down Expand Up @@ -183,25 +182,18 @@ public function send_appointment_deleted(
true,
);

$from_name = config('from_name') ?: $settings['company_name'];
$from_address = config('from_address') ?: $settings['company_email'];
$reply_to = config('reply_to') ?: $settings['company_email'];
$subject = lang('appointment_cancelled_title');

$this->CI->email->from($from_address, $from_name);
$php_mailer = $this->get_php_mailer();

if ($reply_to) {
$this->CI->email->reply_to($reply_to);
}

$this->CI->email->to($recipient_email);

$this->CI->email->subject(lang('appointment_cancelled_title'));
$php_mailer->addAddress($recipient_email);

$this->CI->email->message($html);
$php_mailer->isHTML();
$php_mailer->Subject = $subject;
$php_mailer->Body = $html;
$php_mailer->AltBody = $html;

if (!$this->CI->email->send(false)) {
throw new RuntimeException('Email was not sent: ' . $this->CI->email->print_debugger());
}
$php_mailer->send();
}

/**
Expand All @@ -210,6 +202,8 @@ public function send_appointment_deleted(
* @param string $password New password.
* @param string $recipient_email Recipient email address.
* @param array $settings App settings.
*
* @throws Exception
*/
public function send_password(string $password, string $recipient_email, array $settings): void
{
Expand All @@ -223,24 +217,50 @@ public function send_password(string $password, string $recipient_email, array $
true,
);

$from_name = config('from_name') ?: $settings['company_name'];
$from_address = config('from_address') ?: $settings['company_email'];
$reply_to = config('reply_to') ?: $settings['company_email'];
$subject = lang('new_account_password');

$this->CI->email->from($from_address, $from_name);
$php_mailer = $this->get_php_mailer();

if ($reply_to) {
$this->CI->email->reply_to($reply_to);
}

$this->CI->email->to($recipient_email);
$php_mailer->addAddress($recipient_email);

$this->CI->email->subject(lang('new_account_password'));
$php_mailer->isHTML();
$php_mailer->Subject = $subject;
$php_mailer->Body = $html;
$php_mailer->AltBody = $html;

$this->CI->email->message($html);
$php_mailer->send();
}

if (!$this->CI->email->send(false)) {
throw new RuntimeException('Email was not sent: ' . $this->CI->email->print_debugger());
/**
* Create PHP Mailer instance based on the email configuration.
*
* @return PHPMailer
*
* @throws Exception
*/
private function get_php_mailer(): PHPMailer
{
$php_mailer = new PHPMailer(true);

$php_mailer->SMTPDebug = config('smtp_debug') ? SMTP::DEBUG_SERVER : null;

if (config('protocol') === 'smtp') {
$php_mailer->isSMTP();
$php_mailer->Host = config('smtp_host');
$php_mailer->SMTPAuth = config('smtp_auth');
$php_mailer->Username = config('smtp_user');
$php_mailer->Password = config('smtp_pass');
$php_mailer->SMTPSecure = config('smtp_crypto');
$php_mailer->Port = config('smtp_port');
}

$from_name = config('from_name') ?: setting('company_name');
$from_address = config('from_address') ?: setting('company_email');
$reply_to_address = config('reply_to') ?: setting('company_email');

$php_mailer->setFrom($from_address, $from_name);
$php_mailer->addReplyTo($reply_to_address);

return $php_mailer;
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"guzzlehttp/guzzle": "^7.5.0",
"sabre/vobject": "^4.5",
"ezyang/htmlpurifier": "^4.17",
"symfony/finder": "^6.4"
"symfony/finder": "^6.4",
"phpmailer/phpmailer": "^6.9"
},
"require-dev": {
"roave/security-advisories": "dev-master",
Expand Down
83 changes: 82 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c6d44fe

Please sign in to comment.