Coder Social home page Coder Social logo

975l / emailbundle Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 231 KB

Bundle to manage sending emails and store in a database

Home Page: https://975l.com/en/pages/email-bundle

License: MIT License

PHP 57.57% CSS 11.74% Twig 30.69%
symfony symfony-bundle swiftmailer

emailbundle's Introduction

EmailBundle

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' => '[email protected]',
            'sentCc' => '[email protected]', //optional
            'sentBcc' => '[email protected]', //optional
            'replyTo' => '[email protected]', //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 :)

emailbundle's People

Contributors

laurentmarquet avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

vijaytravelodge

emailbundle's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.