FbeenMailerBundle
This bundle is a tiny layer on top of the Swift Mailer Bundle that is included in each standard Symfony framework, (*1)
Features include:
- Mailer helper class
- less configuration
- standard twig layout with optional logo
- easy configuration for used mailaddresses
- method to render example views
Installation
Using composer:, (*2)
1) Add "fbeen/mailerbundle": "dev-master"
to the require section of your composer.json project file., (*3)
"require": {
...
"fbeen/mailerbundle": "dev-master"
},
2) run composer update:, (*4)
$ composer update
3) Add the bundle to the app/AppKernel.php:, (*5)
$bundles = array(
...
new Fbeen\MailerBundle\FbeenMailerBundle(),
);
Configuration
Take a look at this configuration example:, (*6)
fbeen_mailer:
company_name: "php-programmer.nl"
company_logo: "https://www.php-programmer.nl/images/logo.png"
mailaddresses:
noreply:
email: no-reply@example.com # required
name: example.com # optional
general:
email: info@example.com # required
name: example.com # optional
admins:
- {email: 'admin1@gmail.com', name: 'Admin1'} # at least one required, name is optional
- {email: 'admin2@gmail.com'}
If the company_logo is a full URL then it will be used directly. Otherwise The mailer class will use treat it as an asset., (*7)
noreply: This is the From that the receiver will see in his email., (*8)
general: This is the Reply-to that will be set by default. if the receiver replies than you will receive his mail on this mailaddress, (*9)
admins: This is the To of the receiver(s) set by default., (*10)
using names gives less spamscore, (*11)
How to use
This can be easily shown with a few examples:, (*12)
To send an email to the admins you don't need to use setTo(), (*13)
/*
* send an email to the admins
*/
$this->get('fbeen_mailer')
->setSubject('New contact request!')
->setTemplate('email/contact_request.html.twig')
->setData(array(
'name' => $name,
'email' => $email,
'message' => $message,
))
->sendMail()
;
To send an email to a user you will have to use setTo(), (*14)
/*
* send an email to the user
*/
$this->get('fbeen_mailer')
->setTo($user->getEmail())
->setSubject('Welcome on board!')
->setTemplate('email/welcome.html.twig')
->setData(array(
'user' => $user,
))
->sendMail()
;
The mailer class will add addional data to the array before calling twig:
* companyName (the name of your company from the configuration)
* companyLogo (the path or full URL to an image of your choice)
* urlHomepage (the full URL to the "/" path of your website)
* subject (The subject that has been set by setSubject(); ), (*15)
So for example 2 you could have a pair of templates as below., (*16)
layout.html.twig:, (*17)
<table style="font-family: Arial, Helvetica, sans-serif;">
<tr>
<td><a href="{{ urlHomepage }}">{% if companyLogo %}<img src="{{ companyLogo }}">{% else %}{{ companyName }}{% endif %}</a></td>
</tr>
<tr>
<td>
{% block body %}{% endblock %}
</td>
</tr>
<tr>
<td style="font-size: 12px;">
Sincerely,<br>
<br>
{{ companyName }}
</td>
</tr>
</table>
welcome.html.twig:, (*18)
{% extends 'email/layout.html.twig' %}
{% block body %}
{{ subject }} |
Hi {{ user.name }}, Welcome to our community. |
{% endblock %}
To render a page with an example content of the email:, (*19)
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends Controller
{
public function emailAction()
{
$user = new User();
$user->setName('Frank Beentjes');
$user->setEmail('frank@example.org');
return new Response(
$this->get('fbeen_mailer')
->setSubject('Welcome on board!')
->setTemplate('email/welcome.html.twig')
->setTemplate('email/'.$blockname.'.html.twig')
->setData(array(
'user' => $user
))
->renderView()
);
}
}