Skip to content

Commit

Permalink
[Mail] Enhance code logic + Better architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
pH-7 committed Apr 18, 2022
1 parent 38bf934 commit 99e16af
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 19 deletions.
21 changes: 12 additions & 9 deletions _protected/framework/Mail/Mail.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@

defined('PH7') or exit('Restricted access');

use PH7\Framework\Error\Logger;
use PH7\Framework\Mvc\Model\DbConfig;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\Transport\SendmailTransport;
use Symfony\Component\Mime\Address;
use PH7\HtmlToText\Convert as Html2Text;
use Symfony\Component\Mime\Email as EmailMessage;

class Mail implements Mailable
{
/**
* Send an email with Symfony Mailer library engine.
*
* @return int Number of recipients who were accepted for delivery.
*/
public function send(array $aInfo, string $sContents, int $iFormatType = Mailable::ALL_FORMATS): int
public function send(array $aInfo, string $sContents, int $iFormatType = Mailable::ALL_FORMATS): bool
{
/*** Default values ***/
$sFromMail = empty($aInfo['from']) ? DbConfig::getSetting('returnEmail') : escape($aInfo['from'], true);
Expand All @@ -52,33 +52,36 @@ public function send(array $aInfo, string $sContents, int $iFormatType = Mailabl
$oMessage->subject($sSubject);

if ($iFormatType === Mailable::TEXT_FORMAT || $iFormatType === Mailable::ALL_FORMATS) {
$oMessage->text($sContents);
$html2Text = new Html2Text($sContents);
$oMessage->text($html2Text->getText());
}

if ($iFormatType === Mailable::HTML_FORMAT || $iFormatType === Mailable::ALL_FORMATS) {
$oMessage->html($sContents);
}

$iResult = $oMailer->send($oMessage);
$oMailer->send($oMessage);
$bResult = true;
} catch (TransportExceptionInterface $oE) {
$iResult = 0;
(new Logger())->msg('Error while sending email with Symfony Mailer. ' . $oE->getMessage());
$bResult = false;
}

/*
* Check if Symfony Mailer is able to send message, otherwise we use the traditional native PHP mail() function
* as on some hosts config, Symfony Mailer doesn't work.
*/
if (!$iResult) {
if (!$bResult) {
$aData = [
'from' => $sFromMail,
'to' => $sToMail,
'subject' => $sSubject,
'body' => $sContents
];
$iResult = (int)$this->phpMail($aData);
$bResult = $this->phpMail($aData);
}

return $iResult;
return $bResult;
}

/**
Expand Down
9 changes: 1 addition & 8 deletions _protected/framework/Mail/Mailable.interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,5 @@ interface Mailable
const TEXT_FORMAT = 2;
const ALL_FORMATS = 3;

/**
* @param array $aInfo
* @param string $sContents
* @param int $iFormatType
*
* @return int Number of recipients who were accepted for delivery.
*/
public function send(array $aInfo, string $sContents, int $iFormatType): int;
public function send(array $aInfo, string $sContents, int $iFormatType): bool;
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@
"guzzlehttp/guzzle": "^7.4",
"ph-7/cute-link-names": "^2.0",
"ph-7/datatype": "^1.0",
"ph-7/html-to-text": "^1.0",
"ph-7/just-http-status-codes": "^1.1",
"ph-7/passcode-password-generator": "^2.0",
"ph-7/ph2gravatar": "^2.0",
"ph-7/qrcode-generator-php-class": "^2.1",
"robthree/twofactorauth": "^1.6",
"stripe/stripe-php": "^5.9",
"symfony/console": "^5.3",
"symfony/console": "^5.4",
"symfony/mailer": "^5.4",
"twbs/bootstrap": "^3.4",
"twilio/sdk": "^5.28"
Expand Down
65 changes: 64 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 99e16af

Please sign in to comment.