How To Send Email with Attachment Using SwiftMailer In Symfony
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 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.
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.
1
|
"symfony/swiftmailer-bundle" : "*"
|
Learn more About Composer – Dependency manager
config.yml
Add below swiftmailer
configuration in config.yml
file stored in “app/config” folder of Symfony Application.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# app/config/config.yml
swiftmailer:
default_mailer: default
delivery_address: <Email Address>
mailers:
default:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
port: "%mailer_port%"
spool: { type: memory }
|
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.
1
2
3
4
5
6
7
8
|
# app/config/params.yml
# swiftmailer credential
mailer_transport: gmail
mailer_host: smtp.gmail.com
mailer_user: <Email ID>
mailer_password: <Email Password>
mailer_port: 465
|
Configuration is ready to send email from your Symfony Application.
\Swift_Message::newInstance()
will create new instance of SwiftMailer.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class DefaultController extends Controller{
public function sendAction(Request $request) {
$fromEmail = "<From Email Address>";
$toEmail = "<To Email Address>";
$body = "<Email Body>";
$compensation_email = \Swift_Message::newInstance()
->setSubject('Email Subject')
->setFrom($fromEmail)
->setTo($toEmail)
->setBody($body)
->setContentType('text/html');
// Send Email
$this->get('mailer')->send($compensation_email);
}
}
|
Run below command from Command Line/Terminal to Send Email to specific email address
1
2
3
4
5
|
php bin/console swiftmailer:email:send
--from="<From Email Address>"
--to="<To Email Address>"
--subject="<Email Subject>"
--body="<Email Body>"
|
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class DefaultController extends Controller{
public function sendAction(Request $request) {
$fromEmail = "<From Email Address>";
$toEmail = "<To Email Address>";
$body = "<Email Body>";
$file = "<Attchment File Path>";
$compensation_email = \Swift_Message::newInstance()
->setSubject('Email Subject')
->setFrom($fromEmail)
->setTo($toEmail)
->setBody($body)
->setContentType('text/html')
->attach(\Swift_Attachment::fromPath($file));
// Send Email
$this->get('mailer')->send($compensation_email);
}
}
|
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