Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
"laminas/laminas-text": "^2.9",
"swagger-api/swagger-ui": "^4.14",
"zircote/swagger-php": "^4.4",
"psr/simple-cache": "^1.0",
"sendinblue/api-v3-sdk": "8.x.x"
"psr/simple-cache": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
Expand Down
45 changes: 45 additions & 0 deletions src/Libraries/Mailer/Adapters/MailgunAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* Quantum PHP Framework
*
* An open source software development framework for PHP
*
* @package Quantum
* @author Arman Ag. <arman.ag@softberg.org>
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
* @link http://quantum.softberg.org/
* @since 2.9.0
*/

namespace Quantum\Libraries\Mailer\Adapters;

use Quantum\Libraries\Mailer\MailerInterface;
use Quantum\Libraries\Curl\HttpClient;

class MailgunAdapter implements MailerInterface
{
/**
* Send mail by using Mailgun
* @param string $data
* @return bool
*/
public function sendMail($data)
{
try {
$httpClient = new HttpClient();
$httpClient->createMultiRequest()
->createRequest('https://api.mailgun.net/v3/' . config()->get('mailer.mailgun.domain') . '/messages')
->setMethod('POST')
->setHeaders([
"Authorization" => "Basic " . base64_encode("api:" . config()->get('mailer.mailgun.api_key')),
"Content-Type" => "application/x-www-form-urlencoded"
])
->setData($data)
->start();
return true;
} catch (\Exception $e) {
return false;
}
}
}
44 changes: 44 additions & 0 deletions src/Libraries/Mailer/Adapters/MandrillAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* Quantum PHP Framework
*
* An open source software development framework for PHP
*
* @package Quantum
* @author Arman Ag. <arman.ag@softberg.org>
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
* @link http://quantum.softberg.org/
* @since 2.9.0
*/

namespace Quantum\Libraries\Mailer\Adapters;

use Quantum\Libraries\Mailer\MailerInterface;
use Quantum\Libraries\Curl\HttpClient;

class MandrillAdapter implements MailerInterface
{
/**
* Send mail by using Mandrill
* @param string $data
* @return bool
*/
public function sendMail($data)
{
try {
$data["key"] = config()->get('mailer.mandrill.api_key');

$httpClient = new HttpClient();
$httpClient->createMultiRequest()
->createRequest("https://mandrillapp.com/api/1.0/messages/send.json")
->setMethod("POST")
->setData($data)
->start();

return true;
} catch (\Exception $e) {
return false;
}
}
}
45 changes: 45 additions & 0 deletions src/Libraries/Mailer/Adapters/SendgridAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* Quantum PHP Framework
*
* An open source software development framework for PHP
*
* @package Quantum
* @author Arman Ag. <arman.ag@softberg.org>
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
* @link http://quantum.softberg.org/
* @since 2.9.0
*/

namespace Quantum\Libraries\Mailer\Adapters;

use Quantum\Libraries\Mailer\MailerInterface;
use Quantum\Libraries\Curl\HttpClient;

class SendgridAdapter implements MailerInterface
{
/**
* Send mail by using Sendgrid
* @param string $data
* @return bool
*/
public function sendMail($data)
{
try {
$httpClient = new HttpClient();
$httpClient->createMultiRequest()
->createRequest('https://api.sendgrid.com/v3/mail/send')
->setMethod('POST')
->setHeaders([
'Authorization: Bearer ' . config()->get('mailer.sendgrid.api_key'),
'Content-Type: application/json'
])
->setData($data)
->start();
return true;
} catch (\Exception $e) {
return false;
}
}
}
84 changes: 60 additions & 24 deletions src/Libraries/Mailer/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@

namespace Quantum\Libraries\Mailer;

use SendinBlue\Client\Api\TransactionalEmailsApi;
use SendinBlue\Client\Model\SendSmtpEmail;
use Quantum\Libraries\Storage\FileSystem;
use SendinBlue\Client\Configuration;
use PHPMailer\PHPMailer\PHPMailer;
use Quantum\Debugger\Debugger;
use Quantum\Logger\FileLogger;
use PHPMailer\PHPMailer\SMTP;
use Quantum\Loader\Setup;
use GuzzleHttp\Client;
use Psr\Log\LogLevel;
use Quantum\Di\Di;

Expand Down Expand Up @@ -83,6 +79,12 @@ class Mailer
*/
private $message = null;

/**
* Email data
* @var string|array
*/
private $data = null;

/**
* Email attachments
* @var array
Expand Down Expand Up @@ -396,36 +398,70 @@ public function send(?array $from = null, ?array $address = null, ?string $messa

$this->setOptions($options);

if (config()->get('mailer.current') == 'smtp') {
$this->prepare();

if (config()->get('mailer.mail_trap')) {
$sent = $this->mailer->preSend();
$this->saveMessage($this->mailer->getLastMessageID(), $this->mailer->getSentMIMEMessage());
return $sent;
$this->prepare();
if ($this->message) {
if ($this->templatePath) {
$body = $this->createFromTemplate();
} else {
return $this->mailer->send();
}
} else if (config()->get('mailer.current') == 'sendinblue') {
if ($this->message) {
if ($this->templatePath) {
$body = $this->createFromTemplate();
} else {
$body = is_array($this->message) ? implode($this->message) : $this->message;
}

$this->htmlContent = $body;
$body = is_array($this->message) ? implode($this->message) : $this->message;
}

$data = '{
$this->htmlContent = $body;
}
if (config()->get('mailer.mail_trap')) {
$sent = $this->mailer->preSend();
$this->saveMessage($this->mailer->getLastMessageID(), $this->mailer->getSentMIMEMessage());
return $sent;
}
if (config()->get('mailer.current') == 'smtp') {
return $this->mailer->send();
} else if (config()->get('mailer.current') == 'sendinblue') {
$this->data = '{
"sender":' . json_encode($this->from) . ',
"to":' . json_encode($this->addresses) . ',
"subject":"' . $this->subject . '",
"htmlContent":"' . trim(str_replace("\n", "", $this->htmlContent)) . '"
}';
} else if (config()->get('mailer.current') == 'mailgun') {
$to = [];
foreach ($this->addresses as $key => $value) {
array_push($to, $value['email']);
}

return $this->mailerAdapter->sendMail($data);
$this->data = [
"from" => $this->from['name'] . " " . $this->from['email'],
"to" => implode(',', $to),
"subject" => $this->subject,
"html" => trim(str_replace("\n", "", $this->htmlContent))
];
} else if (config()->get('mailer.current') == 'mandrill') {
$this->data = [
'message' => [
'subject' => $this->subject,
'html' => trim(str_replace("\n", "", $this->htmlContent)),
'from_email' => $this->from['email'],
'from_name' => $this->from['name'],
'to' => $this->addresses,
]
];
} else if (config()->get('mailer.current') == 'sendgrid') {
$this->data = [
'personalizations' => [
['to' => $this->addresses]
],
'from' => [
'email' => $this->from['email']
],
'subject' => $this->subject,
'content' => [
[
'type' => 'text/html',
'value' => trim(str_replace("\n", "", $this->htmlContent))
]
]
];
}
return $this->mailerAdapter->sendMail($this->data);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/_root/shared/config/mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@
'sendinblue' => [
'api_key' => 'sendinblue_api_key',
],

'sendgrid' => [
'api_key' => 'sendgrid_api_key',
],

'mailgun' => [
'api_key' => 'mailgun_api_key',
'domain' => 'mailgun_domain',
],
];