Giter Site home page Giter Site logo

ingeniuz / emailtemplatebundle Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mikeyudin/emailtemplatebundle

0.0 1.0 0.0 143 KB

This bundle helps you to deal with emails in your Symfony2 app. Render emails from Twig or Database, it's easy!.

PHP 100.00%

emailtemplatebundle's Introduction

Email Templates Bundle for Symfony2

This bundle can be useful when you need to send diffrent kind of emails from your app, for eg. user registration or forgot password email. Read usage section.

Build Status

Installation

Add bundle via composer (Symfony 2.1)

php composer.phar require sfk/email-template-bundle:dev-master

Add bundle to your application kernel

// app/AppKernel.php
public function registerBundles() 
{
    $bundles = array(
        // ...
        new Sfk\EmailTemplateBundle\SfkEmailTemplateBundle(),
    );
}

Usage

  • Create registration email template in your bundle
src/Acme/DemoBundle/Resources/views/[Emails]/user_registered.html.twig
  • Edit template
// src/Acme/DemoBundle/Resources/views/[Emails]/user_registered.html.twig
{% extends 'SfkEmailTemplateBundle::email.html.twig' %}

{% block from -%}
[email protected]
{%- endblock %}

{% block subject -%}
Thanks for registering {{ first_name }}!
{%- endblock %}

{% block body -%}
Hello {{ first_name }},
<br />
<br />
Thank you for registering at our website! below your account details:
<br />
<br />
First Name: {{ first_name }}<br />
Last Name: {{ last_name }}<br />
Email: {{ email }}<br />
<br />
Thanks
{%- endblock %}
  • Now you can send it from your controller
<?php
// ...
class UserController extends Controller {
    public function registerAction() {
        // ...
        if ($form->isValid()) {
            //.. some actions here
            $formData = array(
                'email' => '[email protected]',
                'first_name' => 'John',
                'last_name' => 'Doe',
            );
            $template = $this->get('sfk_email_template.loader')
                ->load('AcmeDemoBundle:Emails:user_registered.html.twig', $formData)
            ;
            $message = \Swift_Message::newInstance()
                ->setSubject($template->getSubject())
                ->setFrom($template->getFrom())
                ->setBody($template->getBody(), 'text/html')
                ->setTo($formData['email'])
            ;
            // send email
            $this->get('mailer')->send($message);
        }
    }
}

Thats's it! John Doe will receive an email as below:

Hello John,
Thank you for registering at our website! below your account details:

First Name: John
Last Name: Doe
Email: [email protected]

Thanks

Advanced Usage

Credits

This bundle was inspired by Rendering emails with Twig in Symfony2 post. Many thanks to its author.

emailtemplatebundle's People

Contributors

getme avatar ingeniuz avatar

Watchers

 avatar

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.