2017 © Pedro Peláez
 

library mailing

Extension for Nette Framework: Easy & object-oriented way of sending & logging mails

image

ublaboo/mailing

Extension for Nette Framework: Easy & object-oriented way of sending & logging mails

  • Monday, June 4, 2018
  • by paveljanda
  • Repository
  • 4 Watchers
  • 5 Stars
  • 13,835 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 3 Forks
  • 5 Open issues
  • 13 Versions
  • 10 % Grown

The README.md

Build Status Scrutinizer Code Quality Latest Stable Version License Total Downloads Gitter, (*1)

Mailing

Extension for Nette Framework: Easy & object-oriented way of sending & logging mails, (*2)

Mailing extension lets you send/log emails in the object oriented world., (*3)

Overview

Downloading Mailing

Mailing is available through composer package ublaboo/mailing:, (*4)

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., (*5)

Mail

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)., (*6)

You will send it via Mail::send() method., (*7)

Create mail class

Once you have registered mailing extension, you can create new mail class and then get MailFactory from DIC to send it:, (*8)

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:, (*9)

<!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.:, (*10)

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:, (*11)

# ...

public function compose(Message $message, ?IMessageData $mailData): void
{
    # ...

    $this->setTemplateFile(__DIR__ . '/templates/ContactMail.latte');
}

Or from the outside:, (*12)

# ...

$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:, (*13)

# ...

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, (*14)

Start with registering extension in config.neon:, (*15)

extensions:
    mailing: Ublaboo\Mailing\DI\MailingExtension

There are several config options:, (*16)

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:, (*17)

do, (*18)

In this option you may choose between these three directives:, (*19)

  • 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, (*20)

That one is pretty obvious. Directory, where mail files (.eml) will be stored., (*21)

mailImagesBasePath, (*22)

This is the path, where Nette\Mail\Message will look for all images that can be inline embedded in mail., (*23)

mails, (*24)

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., (*25)

E.g.:, (*26)

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., (*27)

The Versions

04/06 2018

dev-master

9999999-dev https://ublaboo.org/mailing

Extension for Nette Framework: Easy & object-oriented way of sending & logging mails

  Sources   Download

MIT

The Requires

 

The Development Requires

log mail extension nette mailing

02/12 2017

v1.1.7

1.1.7.0 https://ublaboo.org/mailing

Extension for Nette Framework: Easy & object-oriented way of sending & logging mails

  Sources   Download

MIT

The Requires

 

The Development Requires

log mail extension nette mailing

01/12 2017

v1.1.6

1.1.6.0 https://ublaboo.org/mailing

Extension for Nette Framework: Easy & object-oriented way of sending & logging mails

  Sources   Download

MIT

The Requires

 

The Development Requires

log mail extension nette mailing

14/08 2016

v1.1.5

1.1.5.0 http://ublaboo.paveljanda.com/mailing

Extension for Nette Framework: Easy & object-oriented way of sending & logging mails

  Sources   Download

MIT

The Requires

 

The Development Requires

log mail extension nette mailing

02/07 2016

v1.1.4

1.1.4.0 http://ublaboo.paveljanda.com/mailing

Extension for Nette Framework: Easy & object-oriented way of sending & logging mails

  Sources   Download

MIT

The Requires

 

The Development Requires

log mail extension nette mailing

19/03 2016

v1.1.3

1.1.3.0 http://ublaboo.paveljanda.com/mailing

Extension for Nette Framework: Easy & object-oriented way of sending & logging mails

  Sources   Download

MIT

The Requires

 

The Development Requires

log mail extension nette mailing

13/03 2016

v1.1.2

1.1.2.0 http://ublaboo.paveljanda.com/mailing

Extension for Nette Framework: Easy & object-oriented way of sending & logging mails

  Sources   Download

MIT

The Requires

 

The Development Requires

log mail extension nette mailing

03/03 2016

v1.1.1

1.1.1.0 http://ublaboo.paveljanda.com/mailing

Extension for Nette Framework: Easy & object-oriented way of sending & logging mails

  Sources   Download

MIT

The Requires

 

The Development Requires

log mail extension nette mailing

03/03 2016

v1.1.0

1.1.0.0 http://ublaboo.paveljanda.com/mailing

Extension for Nette Framework: Easy & object-oriented way of sending & logging mails

  Sources   Download

MIT

The Requires

 

The Development Requires

log mail extension nette mailing

01/02 2016

dev-scrutinizer-patch-1

dev-scrutinizer-patch-1 http://ublaboo.paveljanda.com/mailing

Extension for Nette Framework: Easy & object-oriented way of sending & logging mails

  Sources   Download

MIT

The Requires

 

The Development Requires

log mail extension nette mailing

30/01 2016

dev-feature/emails_templates

dev-feature/emails_templates http://ublaboo.paveljanda.com/mailing

Extension for Nette Framework: Easy & object-oriented way of sending & logging mails

  Sources   Download

MIT

The Requires

 

The Development Requires

log mail extension nette mailing

20/01 2016

dev-develop

dev-develop http://ublaboo.paveljanda.com/mailing

Extension for Nette Framework: Easy & object-oriented way of sending & logging mails

  Sources   Download

MIT

The Requires

 

The Development Requires

log mail extension nette mailing

20/01 2016

v1.0.0

1.0.0.0 http://ublaboo.paveljanda.com/mailing

Extension for Nette Framework: Easy & object-oriented way of sending & logging mails

  Sources   Download

MIT

The Requires

 

The Development Requires

log mail extension nette mailing