Sending emails is a classic task for any web application. Whether application is E-Commerce, Business App, Social Media Website, Application always uses email notifications for Customer Support, Marketing, to increase Customer Engagement and more. In this Tutorial we will learn How to send Email using various methods of Swiftmailer in Symfony and will show some Examples of SwiftMailer

Send-Email-using-swiftmailer-symfony

Send Email with Attachment Using SwiftMailer

Symfony is one of the most popular PHP Frameworks. It includes a large set of command line tools which are helpful during application development process. Symfony Framework uses Swiftmailer Bundle to send Email which comes with Symfony Standard Edition.





Configuration

We will use Symfony 2.8 Framework version SwiftMailer comes with Symfony Standard Edition so you do not need to install it using composer. Ensure that SwiftMailer is already installed in your application by verifying below bundle in composer.json under “require” stored in root folder of your application.

Learn more About Composer – Dependency manager

config.yml

Add below swiftmailer configuration in config.yml file stored in “app/config” folder of Symfony Application.

spool : It will store emails into the memory, they will get sent right before the symfony kernel terminates.
delivery_address: Set your email address where emails to be sent in Development Environment. While Testing email will sent to this email address instead of actual email address in Development Environment.

params.yml

add below swiftmailer configuration in params.yml file stored in “app/config” folder of Symfony Application. We are using SMTP Protocol to send email you can also use other protocols that Swiftmailer.

Configuration is ready to send email from your Symfony Application.

Sending Email from Controller

\Swift_Message::newInstance() will create new instance of SwiftMailer.

Sending Emails Command Line

Run below command from Command Line/Terminal to Send Email to specific email address

Sending Emails with Attachment

Symfony also allows sending email from command line. To send file with Email Attachment, set File path in attach method of SwiftMailer. That’s It. It will Attachment with Email. You can send multiple files/documents in Attachment as well.



 

Read More:

Composer – Dependency manager for PHP

Create a RESTful Web Service API with Slim

How To Integrate Stripe Payment Gateway Using PHP and JavaScript

sFileTypeChecker – JQuery Plugin To Validate Input File

sSwitch – JQuery Toggle Button Plugin For Sliding Toggle Switches

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">