Commit 12b11ee3 authored by Avris's avatar Avris

v3.0

parent 8de95b2c
......@@ -20,7 +20,7 @@ You will be asked for the connection parameters, for example:
username: username@gmail.com
password: apppassword
If you're using GMail, remember to [Allowing less secure apps](https://support.google.com/accounts/answer/6010255?hl=en)
If you're using GMail, remember to [allow less secure apps](https://support.google.com/accounts/answer/6010255?hl=en)
and [create an app password](https://support.google.com/mail/answer/185833?hl=en).
### Manually creating and sending an email ###
......@@ -38,7 +38,7 @@ and [create an app password](https://support.google.com/mail/answer/185833?hl=en
$mail->embedImage('banner', $this->getRootDir() . '/app/Asset/gfx/banner.png');
$mail->addAttachment($this->getRootDir() . '/app/Asset/pdf/registration.pdf');
$this->getService('mailer')->send($mail);
$this->get('mailer')->send($mail);
### Mail Builder ###
......@@ -70,13 +70,13 @@ create such structure:
Now in the controller you can just write:
$mail = $this->getService('mailBuilder')->build('welcome', [
$mail = $this->get('mailBuilder')->build('welcome', [
'%username%' => $user->getUsername(),
'%sitename%' => 'MyAwesomeSite',
]);
$mail->addTo($user); // $user needs to implement AddressInterface
$mail->embedImage('logo', $this->getRootDir() . '/app/Asset/gfx/logo.png');
$this->getService('mailer')->send($mail);
$this->get('mailer')->send($mail);
The builder will automatically translate the subject, body and alternative body.
......@@ -120,4 +120,4 @@ If you want to force sending directly, just execute `$mailer->send($mail, true)`
### Copyright ###
* **Author:** Andrzej Prusinowski [(Avris.it)](https://avris.it)
* **Licence:** [MIT](https://opensource.org/licenses/MIT)
* **Licence:** [MIT](https://mit.avris.it)
......@@ -11,10 +11,11 @@
"homepage": "https://avris.it"
}],
"require": {
"avris/micrus": "^2.2",
"avris/micrus-twig": "^2.2",
"avris/micrus": "^3.0",
"avris/micrus-twig": "^3.0",
"tijsverkoyen/css-to-inline-styles": "^2.2",
"symfony/filesystem": "^3.1"
"symfony/filesystem": "^3.1",
"phpmailer/phpmailer": "^5.2"
},
"autoload": {
"psr-4": { "Avris\\Micrus\\Mailer\\": "src" }
......
......@@ -38,4 +38,9 @@ class Address implements AddressInterface
{
return $this->name;
}
public function __toString()
{
return $this->name ?: $this->email;
}
}
<?php
namespace Avris\Micrus\Mailer;
use Avris\Micrus\Assetic\Manager;
use Avris\Micrus\Assetic\AsseticManager;
use Avris\Micrus\Mailer\Mail\Address;
use Avris\Micrus\Mailer\Mail\Mail;
use Avris\Micrus\ParameterBag;
use Avris\Micrus\Tool\Locale\Localizator;
use Avris\Bag\Bag;
use Avris\Micrus\Localizator\Localizator;
use Avris\Micrus\View\Templater;
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
class MailBuilder
{
/** @var ParameterBag */
/** @var Bag */
protected $config;
/** @var Localizator */
......@@ -20,7 +20,7 @@ class MailBuilder
/** @var Templater */
protected $templater;
/** @var Manager */
/** @var AsseticManager */
protected $asseticManager;
/** @var CssToInlineStyles */
......@@ -30,18 +30,18 @@ class MailBuilder
protected $css;
/**
* @param ParameterBag $config
* @param Bag $config
* @param Localizator $localizator
* @param Templater $templater
* @param Manager $asseticManager
* @param AsseticManager $asseticManager
* @param CssToInlineStyles $styleInliner
* @param string $css
*/
public function __construct(
ParameterBag $config,
Localizator $localizator,
Bag $config,
Localizator $localizator = null,
Templater $templater = null,
Manager $asseticManager = null,
AsseticManager $asseticManager = null,
CssToInlineStyles $styleInliner = null,
$css = null
) {
......@@ -129,7 +129,7 @@ class MailBuilder
try {
return $this->templater->render(array_merge(['_view' => 'Mail/' . $templateName], $vars));
} catch (\Exception $e) {
} catch (\Twig_Error_Loader $e) {
return $this->localizator->get('mail.' . $templateName . '.body', $transVars);
}
}
......@@ -152,13 +152,11 @@ class MailBuilder
return '';
}
$assets = $this->asseticManager->buildAll();
$filename = $this->asseticManager->getAsset('mailCss', true);
if (!isset($assets['mailCss'])) {
return '';
}
return file_get_contents($this->asseticManager->getAsset('mailCss', true));
return $filename === false
? ''
: file_get_contents($filename);
}
/**
......
......@@ -5,10 +5,11 @@ use Avris\Micrus\Exception\ConfigException;
use Avris\Micrus\Mailer\Mail\Mail;
use Avris\Micrus\Mailer\Sender\SenderInterface;
use Avris\Micrus\Mailer\Task\SpoolStatus;
use Avris\Micrus\Tool\Cache\CacheClearEvent;
use Avris\Micrus\Tool\Config\ParametersProvider;
use Avris\Micrus\Tool\RmdirHelper;
use Psr\Log\LoggerInterface;
class Mailer
class Mailer implements ParametersProvider
{
/** @var SenderInterface */
protected $sender;
......@@ -33,11 +34,11 @@ class Mailer
$this->spoolDir = $spoolDir;
}
public function send(Mail $mail, $force = false)
public function send(Mail $mail, $spool = true)
{
if ($force || !$this->spoolDir) {
if (!$spool || !$this->spoolDir) {
try {
$this->sender->send($mail);
return $this->sender->send($mail);
} catch (\Exception $e) {
$this->logger->error($e);
return false;
......@@ -46,7 +47,7 @@ class Mailer
if (!is_dir($this->spoolDir)) {
if (!@mkdir($this->spoolDir, 0777, true)) {
throw new ConfigException('Cannot write to spool in ' . $dir);
throw new ConfigException('Cannot write to spool in ' . $this->spoolDir);
}
}
......@@ -85,7 +86,7 @@ class Mailer
continue;
}
$result = $this->send($mail, true);
$result = $this->send($mail, false);
unlink($file);
......@@ -95,6 +96,18 @@ class Mailer
public function clearSpool()
{
return (new CacheClearEvent(''))->removeDirectory($this->spoolDir);
RmdirHelper::removeDirectory($this->spoolDir, true);
}
/** @return array */
public function getDefaultParameters()
{
return [
'urlForCli' => [
'schema' => 'http',
'host' => 'localhost',
'base' => '',
],
];
}
}
......@@ -2,7 +2,7 @@
namespace Avris\Micrus\Mailer;
use Avris\Micrus\Mailer\Sender\PHPMailerSender;
use Avris\Micrus\Module;
use Avris\Micrus\Bootstrap\Module;
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
class MailerModule implements Module
......@@ -13,17 +13,18 @@ class MailerModule implements Module
'services' => [
'mailer' => [
'class' => Mailer::class,
'parameters' => [
'params' => [
'@mailerSender',
'@logger',
'{@rootDir}/run/spool/{@env}',
],
'tags' => ['defaultParameters'],
],
'mailBuilder' => [
'class' => MailBuilder::class,
'parameters' => [
'params' => [
'@config.mailer',
'@localizator',
'@?localizator',
'@?templater',
'@?asseticManager',
'@?mailerStyleInliner',
......@@ -31,7 +32,7 @@ class MailerModule implements Module
],
'mailerSender' => [
'class' => PHPMailerSender::class,
'parameters' => ['@config.parameters.mailer'],
'params' => ['@config.parameters.mailer'],
'tags' => ['defaultParameters'],
],
'mailerStyleInliner' => [
......
......@@ -2,8 +2,8 @@
namespace Avris\Micrus\Mailer\Sender;
use Avris\Micrus\Mailer\Mail\Mail;
use Avris\Micrus\ParameterBag;
use Avris\Micrus\Tool\ParametersProvider;
use Avris\Bag\Bag;
use Avris\Micrus\Tool\Config\ParametersProvider;
class PHPMailerSender implements SenderInterface, ParametersProvider
{
......@@ -12,7 +12,7 @@ class PHPMailerSender implements SenderInterface, ParametersProvider
public function __construct($params)
{
$options = $params instanceof ParameterBag ? $params : new ParameterBag((array)$params);
$options = $params instanceof Bag ? $params : new Bag((array)$params);
$this->prefab = new \PHPMailer(true);
......
......@@ -18,7 +18,7 @@ class SpoolClearTask extends Task
{
$this
->setName('mailer:spool:clear')
->addOption('force', 'f');
->addOption('force', 'f')
;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment