Updated the mailing system

This commit is contained in:
Pinga 2023-11-16 11:32:12 +02:00
parent 3a9d39da38
commit b91fa31bf3
4 changed files with 63 additions and 40 deletions

View file

@ -1,21 +1,44 @@
<?php namespace App\Lib;
<?php
use PHPMailer\PHPMailer\Exception;
namespace App\Lib;
use PHPMailer\PHPMailer\Exception as PHPMailerException;
use PHPMailer\PHPMailer\PHPMailer;
/**
* Mail
*
* @author Hezekiah O. <support@hezecom.com>
*/
use Utopia\Messaging\Messages\Email;
use Utopia\Messaging\Adapters\Email\SendGrid;
use Utopia\Messaging\Adapters\Email\Mailgun;
class Mail
{
public static function send($subject, $body, $from=[], $to=[], $info=[])
{
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 0;
if(envi('MAIL_DRIVER')=='smtp') {
if (envi('MAIL_DRIVER') == 'utopia') {
try {
$message = new Email(
from: [$from['email']],
to: [$to['email']],
subject: $subject,
content: $body
);
// Send email
if (envi('MAIL_API_PROVIDER') == 'sendgrid') {
$messaging = new Sendgrid(envi('MAIL_API_KEY'));
$messaging->send($message);
return true;
} else {
$messaging = new Mailgun(envi('MAIL_API_KEY'), envi('APP_DOMAIN'));
$messaging->send($message);
return true;
}
} catch (\Exception $e) {
echo "Message could not be sent. Error: {$e->getMessage()}";
return false;
}
} else if (envi('MAIL_DRIVER') == 'smtp') {
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = envi('MAIL_HOST');
$mail->SMTPAuth = true;
@ -23,27 +46,20 @@ class Mail
$mail->Password = envi('MAIL_PASSWORD');
$mail->SMTPSecure = envi('MAIL_ENCRYPTION');
$mail->Port = envi('MAIL_PORT');
}
elseif(envi('MAIL_DRIVER')=='sendmail') {
$mail->isSendmail();
}
else{
$mail->isMail();
}
$mail->setFrom($from['email'], $from['name']);
$mail->addAddress($to['email'], $to['name']);
//$mail->addAttachment('path/to/invoice1.pdf', 'invoice1.pdf');
$mail->setFrom($from['email'], $from['name']);
$mail->addAddress($to['email'], $to['name']);
//$mail->addAttachment('path/to/invoice1.pdf', 'invoice1.pdf');
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
//echo 'Message has been sent';
return false;
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->send();
return true;
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
return false;
}
}
}
}
}