ublaboo / mailing
Extension for Nette Framework: Easy & object-oriented way of sending & logging mails
Installs: 143 855
Dependents: 1
Suggesters: 0
Security: 0
Stars: 8
Watchers: 6
Forks: 5
Open Issues: 9
Requires
- php: >= 7.1
- latte/latte: ~2.4 || ~3.0
- nette/application: ~2.4 || ~3.0
- nette/di: ~2.4 || ~3.0
- nette/mail: ~2.4 || ~3.0
- nette/utils: ~2.4 || ~3.0
Requires (Dev)
- mockery/mockery: ~1.0
- nette/tester: ~2.0
- tracy/tracy: ^2.4
This package is auto-updated.
Last update: 2024-11-19 09:18:02 UTC
README
Mailing
Extension for Nette Framework: Easy & object-oriented way of sending & logging mails
Mailing extension lets you send/log emails in the object oriented world.
Overview
Downloading Mailing
Mailing is available through composer package ublaboo/mailing:
composer require ublaboo/mailing
MailFactory
MailFactory gives you a way to create your mails instances. In the background it sets some parameters, decides whether to log and email or not, tries to find mail template etc.
Mail is the base class you will extend in each of your email cases (classes). In your particular mail class you will set email senders/recipients/cc/.., basepath (if you want to send inline images), attach files, etc. There are also available config parameters (if you have put them in there through config.neon).
You will send it via Mail::send() method.
Create mail class
Once you have registered mailing extension, you can create new mail class and then get MailFactory from DIC to send it:
namespace App\Mailing; use Ublaboo\Mailing\IMessageData; class ContactMailData implements IMessageData { public function __construct( public readonly string $recipient, ) { } }
namespace App\Mailing; use InvalidArgumentException; use Nette\Mail\Message; use Ublaboo\Mailing\Mail; use Ublaboo\Mailing\IComposableMail; use Ublaboo\Mailing\IMessageData; class ContactMail extends Mail implements IComposableMail { public function compose(Message $message, ?IMessageData $mailData): void { if (!$mailData instanceof ContactMailData) { throw new InvalidArgumentException(); } $message->setFrom($this->mailAddresses['defaultSender']); $message->addTo($mailData->recipient); } }
namespace App\Presenters; use App\Mailing\ContactMail; use App\Mailing\ContactMailData; use Nette\Application\UI\Presenter; use Nette\DI\Attributes\Inject; use Ublaboo\Mailing\MailFactory; class HomepagePresenter extends Presenter { #[Inject] public MailFactory $mailFactory; public function actionDefault(): void { $mail = $this->mailFactory->createByType( ContactMail::class, new ContactMailData( recipient: 'hello@hello.hello' ), ); $mail->send(); } }
Example mail template:
<!DOCTYPE html> <html> <head> <title>Contact mail</title> </head> <body> Helo, {$mailData->name} <br> Your email is: {$mailData->recipient} </body> </html>
Mail templates
Now, there is some convention in directory structure and you should stick to it. It doesn't matter where you put your mails, but the Mail class (which your mails will inherit from) will look for template latte files in <same_directory_as_your_mails_are</templates
. The name of particular template has to be in camel_case naming convention. E.g.:
app/
Mailing/
ContactMail.php
templates/
ContactMail.latte
But that is only a recommendation. You can always change your template file path by Mail::setTemplateFile()
. Eg:
# ... public function compose(Message $message, ?IMessageData $mailData): void { # ... $this->setTemplateFile(__DIR__ . '/templates/ContactMail.latte'); }
Or from the outside:
# ... $mail = $mailFactory->createByType(ContactMail::class, new ContactMailData(recipient: 'hello@hello.hello'])); $mail->setTemplateFile('super_awesome_template.latte');
No templates
Of course you don't have to send mails with templates, you can just use plaintext mail body. You would do that probably in your mail class:
# ... public function compose(Message $message, ?IMessageData $mailData): void { # ... $message->setBody('Hello'); }
Configuration
There are some configuration options available like whether to log (or send, or both), where to log, where to find inline images from, etc
Start with registering extension in config.neon:
extensions:
mailing: Ublaboo\Mailing\DI\MailingExtension
There are several config options:
mailing:
do: both # log|send|both
logDirectory: '%appDir%/../log/mails' # this is default option
mailImagesBasePath: %wwwDir% # this is default option
mails: []
Let's discuss each of these options:
do
In this option you may choose between these three directives:
- log means all mails will be just stored on local disk in log directory. All mails are saved in .eml format (with possible images and attachments)
- send will only send all mails, but not log
- both will do both
logDirectory
That one is pretty obvious. Directory, where mail files (.eml
) will be stored.
mailImagesBasePath
This is the path, where Nette\Mail\Message
will look for all images that can be inline embedded in mail.
mails
This array filled with your parameters (probably mail addresses - like recipients, senders, etc) will be available in instance of each of your mail class (read more) so that you can easily set sender and recipient, bcc, cc or whatever you need for particular mail.
E.g.:
mailing:
mails: [
defaultSender: foo@bar.baz
]
Log
By default, MailLogger is logging all sent Mails in log directory in format ///<mail_name.eml>. As you can see, the .eml extension is used to easily open an email file in all desktop clients.