c975l / email-bundle
Bundle to manage sending emails and store in a database
Fund package maintenance!
Patreon
Open Collective
buymeacoff.ee/laurentmarquet
Installs: 762
Dependents: 8
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 1
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=8.0
- c975l/config-bundle: *
- c975l/includelibrary-bundle: *
- c975l/services-bundle: *
- c975l/toolbar-bundle: *
- doctrine/doctrine-bundle: *
- doctrine/orm: *
- knplabs/knp-paginator-bundle: *
- symfony/form: *
- symfony/mailer: *
- symfony/security-bundle: *
- symfony/twig-bundle: *
- twig/cssinliner-extra: *
- twig/extra-bundle: *
- twig/intl-extra: *
- dev-main
- v7.4
- v7.3
- v7.2.3
- v7.2.2
- v7.2.1
- v7.2
- v7.1.2
- v7.1.1
- v7.1
- v7.0.1
- v7.0
- 6.x-dev
- v6.0.2
- v6.0.1
- v6.0
- 5.x-dev
- v5.0.1
- v5.0
- 4.x-dev
- v4.0
- 3.x-dev
- v3.4
- v3.3.2.1
- v3.3.2
- v3.3.1
- v3.3
- v3.2
- v3.1
- v3.0.1
- v3.0
- 2.x-dev
- v2.2.0.2
- v2.2.0.1
- v2.2
- v2.1.2.2
- v2.1.2.1
- v2.1.2
- v2.1.1
- v2.1
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0
- 1.x-dev
- v1.16.6
- v1.16.5
- v1.16.4
- v1.16.3
- v1.16.2.3
- v1.16.2.2
- v1.16.2.1
- v1.16.2
- v1.16.1
- v1.16
- v1.15.6.2
- v1.15.6.1
- v1.15.6
- v1.15.5
- v1.15.4
- v1.15.3.1
- v1.15.3
- v1.15.2
- v1.15.1
- v1.15
- v1.14.2
- v1.14.1
- v1.14
- v1.13
- v1.12.2
- v1.12.1
- v1.12
- v1.11
- v1.10
- v1.9
- v1.8
- v1.7
- v1.6.4
- v1.6.3
- v1.6.2
- v1.6.1
- v1.6
- v1.5.1
- v1.5
- v1.4.2
- v1.4.1
- v1.4
- v1.3
- v1.2
- v1.1.2.1
- v1.1.2
- v1.1.1
- v1.1
- v1.0.1
- dev-dev
This package is auto-updated.
Last update: 2024-11-15 16:39:46 UTC
README
EmailBundle does the following:
- Stores email in a database as an option,
- Sends email using Symfony Mailer,
- Allows user with good ROLE to see emails sent,
- Defines a template for emails that should be overriden to integrate fully with website,
- Allows to attach one or multiple files.
EmailBundle dedicated web page.
EmailBundle API documentation.
Bundle installation
Step 1: Download the Bundle
Use Composer to install the library
composer require c975l/email-bundle
Step 2: Enable the Bundle
Then, enable the bundle by adding it to the list of registered bundles in the app/AppKernel.php
file of your project:
<?php class AppKernel extends Kernel { public function registerBundles() { $bundles = [ // ... new c975L\EmailBundle\c975LEmailBundle(), ]; } }
Step 3: Configure the Bundle
Check dependencies for their configuration:
c975LEmailBundle uses c975L/ConfigBundle to manage configuration parameters. Use the Route "/email/config" with the proper user role to modify them.
If you are NOT using Messenger remember to disable the contents in config/packages/messenger.yaml or configure it properly.
Step 4: Enable the Routes
Then, enable the routes by adding them to the app/config/routing.yml
file of your project:
c975_l_email: resource: "@c975LEmailBundle/Controller/" type: annotation prefix: / #Multilingual website use the following #prefix: /{_locale} #defaults: { _locale: '%locale%' } #requirements: # _locale: en|fr|es
Step 4: Create MySql table
You can use php bin/console make:migration
to create the migration file as documented in Symfony's Doctrine docs OR use /Resources/sql/emails.sql
to create the tables emails
and emails_archives
. The DROP TABLE
are commented to avoid dropping by mistake. It will also create a stored procedure sp_EmailsArchive()
and an event e_monthly_archives
to archives emails older than 90 days. If you don't want to use this feature, just remove them.
Step 5: Create MySql table
Have a look at the following links if you wish to use Symfony Messenger to dispatch messages with Doctrine. If you want to use async you may also have a look at this answer on StackOverflow.
How to use
Create a Twig template i.e. templates/emails/description.html.twig
with this content:
{# If you want to use the template provided by c975LEmailBundle you have to extend its layout #} {% extends "@c975LEmail/emails/layout.html.twig" %} {% block email_content %} <p> {{ 'label.description'|trans }} : <strong>{{ object.description }}</strong> </p> {# You can include files #} {% include 'YOUR_FILE_PATH' %} {% endblock %}
Then in your Controller, add this code to create, insert in DB and send your email:
<?php // src/Controller/AnyController.php use c975L\EmailBundle\Service\EmailServiceInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class AnyController extends AbstractController { public function anyFunction(Request $request, EmailServiceInterface $emailService) { // ... //Build your email $body = $this->renderView('emails/description.html.twig', array( //Data needed for your template )); $emailData = array( 'subject' => 'YOUR_SUBJECT', 'sentFrom' => $emailService->getParameter('c975LEmail.sentFrom'), 'sentTo' => 'contact@example.com', 'sentCc' => 'contact@example.com', //optional 'sentBcc' => 'contact@example.com', //optional 'replyTo' => 'contact@example.com', //optional 'body' => $body, 'attach' => array( array($filePath, $filename, $contentType), ), //optional 'ip' => $request->getClientIp(), //optional ); //Sends email $emailSent = $emailService->send($emailData, [saveDatabase ? true|false(default)]); //You can test if email has been sent if ($emailSent) { //Do what you need... } else { //Do what you need... } // ... } }
Email messages templates
If you wish to override/disable a block defined in the fullLayout.html.twig
template, create your templates/bundles/c975LEmailBundle/emails/layout.html.twig
and use the following code:
{% extends "@c975LEmail/emails/fullLayout.html.twig" %} {# Overide a block #} {% block noSpam %} {# You can also use {{ parent() }} #} {# YOUR_OWN_TEXT #} {% endblock %} {# Disable a block #} {% block logo %} {% endblock %}
Have a look at templates/emails/fullLayout.html.twig
, to see all available blocks.
Footer template
You should override the template templates/emails/footer.html.twig
in your templates/bundles/c975LEmailBundle/emails/footer.html.twig
and indicate there all the data you need to display at the bottom of sent email.
Use of dashboard and display messages sent
You can see the emails sent via the dashboard.
For this, simply, create the following structure templates/bundles/c975LEmailBundle/
in your app and then duplicate the file layout.html.twig
in it, to override the existing Bundle files, then apply your needed changes.
In layout.html.twig
, it will mainly consist to extend your layout and define specific variables, i.e. :
{% extends 'layout.html.twig' %} {# or extends 'emails/layout.html.twig' #} {# Defines specific variables #} {% set title = 'Email (' ~ title ~ ')' %} {% block content %} {% block email_content %} {% endblock %} {% endblock %}
If this project help you to reduce time to develop, you can sponsor me via the "Sponsor" button at the top :)