Skip to content

disjfa/mail-bundle

Repository files navigation

Mail bundle

Check on packagist MIT License

Watch on GitHub Star on GitHub Tweet

Why is this bundle here

In every project i need to build a way to send emails. Now symfony has released the Mime Component and the Mailer Component. Lets see what we can do to make live easier for us people who send emails.

Instalation

composer require disjfa/mail-bundle

Setup the interface

Setup the routes in config/routes/disjfa_mail.yaml. Here you can edit emails setup in your application.

disjfa_mail:
    resource: '@DisjfaMailBundle/Controller/'
    type: annotation
    prefix: '/admin'

Make you own template

Create a class that extends the MailInterface. Implement the name, subject and content as you want. You can inject the Translator to add simple translations or the twig Environment to render out templates.

<?php
namespace Disjfa\MailBundle\Mail;

interface MailInterface
{
    public function getName();
    public function getSubject();
    public function getContent();
}

In your templates there should be no twig variables. Escape those, like this: {{ '{{' }} email {{ '}}' }}. All the variables used are parsed and collected. The original variables are the only ones that should be used.

If you made a class it will be autoload in the MailCollection collection. And so editable if you have set up some routes for the interface.

Sending emails

Next up is sending emails. In your code just make a message or function that sets up the email.

use Disjfa\MailBundle\Mail\MailFactory;
use Disjfa\MailBundle\Mail\MailService;

function myFunction(MailFactory $mailFactory, MailService $mailService)
{
    $mail = $mailFactory->findByName('name');
    $mailService->send($mail, [
         'param1' => 'value',
         'param2' => 'value',
    ], '[email protected]');
}

And done! Mail sent. Now it is time to setup emails and make more in your application.

Extend the templates

You can manage the templates as is. But you probably want to integrate the files in your own system. Just create a file in your application in templates/bundles/DisjfaMailBundle/layout.html.twig and add a body block.

<!doctype html>
<html>
<head>
...
</head>
<body>
{% block body %}

{% endblock %}
</body>
</html>

And you are good to go. Or you can just extend your own template. Just make sure you use a block named body. You can also just extend the rest of the files as you wish. Just name them like we set up the files.

One thing missing

One thing missing is sending the emails. We do not have to set up the mailing bit of the application. You can do that yourself. Check the transports on how to set up your own mailer as you wish.

And that is about it.

Now you can make your own emails. Set them up. Create a method to send emails. And when you have set up your favorite mailer you can send them!

Help

This bundle is a nice way to extend your workflow. But it can be improved. If you have any ideas or solutions to do so don't be shy and tell us! We can only make stuff better in the end.

Enjoy!

Use the bundle. Check what the bundle does. Fork. Make your own. This is here just to make live easier for us all. Make something beautiful.