Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Send Emails Using Swift Mailer

DZone's Guide to

How to Send Emails Using Swift Mailer

Swift Mailer supports SMTP authentication, HTML emails and attachments, bulk emails, and requests for read reciepts and greeting messages. It's easy to integrate and use.

· Mobile Zone
Free Resource

Discover how to focus on operators for Reactive Programming and how they are essential to react to data in your application.  Brought to you in partnership with Wakanda

Ever wonder how you can send newsletters, confirmation emails, order details, and other emails to your customers or subscribers using PHP? Given the recent discovery of security vulnerability in PHPMailer, the PHP development community is on the lookout for alternatives of this extremely useful library.

A very likely option is Swift Mailer, a PHP library created by Chris Corbyn and maintained by Fabien Potencier. Swift Mailer is a Symfony component that could also be used independently in PHP projects. In this article, I will introduce Swift Mailer and will highlight a simple use case of the library in which I will send an email using Swift Mailer.

Why You Shouldn't Use mail()

At this point, you might wonder why I am not talking about the default PHP mail() method. mail() is an extremely basic method and does not support authentication. Additionally, you cannot send attachments through this method. However, the main problem with the method is that it cannot send emails in a loop because it opens and closes sockets every time an email is sent.

In contrast, Swift Mailer offers:

  • SMTP authentication.

  • HTML emails and attachments.

  • Bulk emails.

  • Read receipts/greeting messages.

  • Ease for integrating and using.

How To Install Swift Mailer

Composer is a prerequisite for installing Swift Mailer. Thus, you must first get Composer from the official website and install it. In order to avoid all this hassle, I opted for a PHP application hosted on a Cloudways server. Since Composer comes pre-installed on the Cloudways Platform, I could easily skip this step and move on to the next step.

Once this is done, you can install Swift Mailer by simply running the following command:

$ composer require swiftmailer/swiftmailer


This will install the latest stable release of Swift Mailer in your project. You can also clone it from GitHub. To do this, first go into your project folder and run this command:

git clone https://github.com/swiftmailer/swiftmailer.git


After installing Swift Mailer, you need to load it in your file. I will explain this later on when I make a class and methods for Swift Mailer.

Create A Simple HTML Form

First of all, I will create a simple form to get the four basic information items (subject, sender’s email address, recipient’s email address, and message body) about the emails.

For this, I will create an index.php file and add a simple form in the body of the HTML.

<form class="form-horizontal" action="index.php" method="post" enctype="multipart/form-data">
<fieldset>

<!-- Form Name -->
<legend>Form Name</legend>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="textinput">Add Subject:</label>  
  <div class="col-md-4">
  <input id="textinput" name="subject" type="text" placeholder="add subject" class="form-control input-md">
  <span class="help-block">help</span>  
  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="textinput">Send To:</label>  
  <div class="col-md-4">
  <input type="email" id="textinput" name="email" type="text" placeholder="add recipient email" class="form-control input-md">
  <span class="help-block">help</span>  
  </div>
</div>

<!-- Textarea -->
<div class="form-group">
  <label class="col-md-4 control-label" for="textarea">Message</label>
  <div class="col-md-4">                     
    <textarea class="form-control" id="textarea" name="message">default text</textarea>
  </div>
</div>

<!-- File Button --> 
<div class="form-group">
  <label class="col-md-4 control-label" for="filebutton">Attach file</label>
  <div class="col-md-4">
    <input name="file" class="input-file" type="file">
  </div>
</div>

<!-- Button -->
<div class="form-group">
  <label class="col-md-4 control-label" for="singlebutton">Click to Send Mail</label>
  <div class="col-md-4">
    <button type="submit" name="submit" class="btn btn-primary">Send</button>
  </div>
</div>

</fieldset>
</form>

The form action is set to index.php itself and the method must be post. The form is very barebones and only collects the essential information from the user. It also contains basic input fields to gather data from the users.

Create the Swift Mailer Class

The next step is to create a MailClass to get the email response. All the arguments are passed from index.php. I will first create the MailClass and then define a sendMail method in the class.

The MailClass contains a simple function with four arguments: $subject$sendto$body$targetpath.

<?php

class MailClass
{

    function sendMail($subject,$sendto,$body,$targetpath = null)
    {
        try{

$transport = \Swift_SmtpTransport::newInstance();

$message = Swift_Message::newInstance();
$message->setTo($sendto);
$message->setSubject($subject);
$message->setBody($body);
$message->setFrom("shahroznawaz156@gmail.com", "Shahroze Nawaz");
if(!empty($targetpath)){
$message->attach(Swift_Attachment::fromPath($targetpath));
}


$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);
    if ($result) {
        echo "Number of emails sent: $result";
    } else {
        echo "Couldn't send email";


Swift_SmtpTransport is used to create a transport instance. Swift_Message is the object of email, setTo() method holds the recipient email address, setSubject holds the subject of the email, setBody() contains the whole message of the email, and lastly, setFrom() contains the sender email. In the next line, I set a condition to check whether the target path is empty and then attach the files to the email. Finally, the email will be sent by the send() method.

Please note that I have set up an optional argument $targetpath = null because even if you do not attach files to an email, you will still be able to send it.

Initialize Swift Mailer and Send The Email

Now, coming back to index.php file, I will initialize Swift Mailer by requiring the autoload.php file. In addition, I will require mailclass.php file. On clicking Send, all the values will be passed in PHP variables and the file upload will be handled by defining what extensions are allowed (for instance, PDF, TXT, or PNG). Finally, I will instantiate MailClass and pass all the values to sendMail() function to send the email.

Simply copy the following code and paste it at the top of index.php.

<?php
require 'vendor/autoload.php';
require 'mailclass.php';
require_once 'vendor/swiftmailer/swiftmailer/lib/swift_required.php';

if(isset($_POST['submit'])){

$subject = $_POST['subject'];
$sendto = $_POST['email'];
$body = $_POST['message'];  
$file = $_FILES["file"];
$file_name = $file["name"];
$file_tmp = $file["tmp_name"];
$file_ext = explode(".", $file_name);
$file_ext = strtolower(end($file_ext));
$allowed = array("txt", "pdf", "jpg" , "png" , "xlsx" , "docx");
$target_dir = null;
if(in_array($file_ext, $allowed)){
$target_dir = "attachement/" . $file_name;
move_uploaded_file($file_tmp,$target_dir);
}

$mailClient = new MailClass();
$swiftmail = $mailClient->sendMail($subject,$sendto,$body,$target_dir);

}

?>


Now open the application in the browser and test it by sending an email with and without attachment. Enter all the details and click the Send button. Image title

Swift Mailer keeps track of the number of emails being sent. I have set a condition in the sendMail() method that increments the variable $result. Simply echo this variable:

$result = $mailer->send($message);
    if ($result) {
        echo "Number of emails sent: $result";
    } else {
        echo "Couldn't send email";
    }


This snippet will result in the following result:

Image titleOf course, you should check your inbox to verify that you have, indeed, received the email!

Image title

You can test the live application demo here

SMTP Transport Settings

You can also use other SMTP services, such as Gmail, Hotmail, SendGrid, etc. to send emails from the associated account. You just need to put in SMTP credentials in transport instance.

Try replacing the default transport instance with the following:

Mandrill

$transport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com','tls', 587);
$transport->setUsername('mandrill_username');
$transport->setPassword('password');

Gmail

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 'tls', 587);
$transport->setUsername('gmail_username@gmail.com');
$transport->setPassword('password');

Note: For sending emails from Gmail SMTP, you need to Turn On the access of less secure apps from account settings. Next, go to this URL and allow Google to access your application.

Hotmail

$transport = Swift_SmtpTransport::newInstance('smtp-mail.outlook.com', 'tls', 587);
$transport->setUsername('hotmail_user@outlook.com');
$transport->setPassword('password');

Conclusion

In this article, I introduced Swift Mailer and created a simple class and methods to send an email. You could easily implement advanced features and automate the process of sending system generated emails. Swiftmailer is a secure email library. I personally recommend that you send emails using Swift Mailer in all your future PHP projects.

If you have a question or would like to contribute to the discussion, do leave a comment below!

Learn how divergent branches can appear in your repository and how to better understand why they are called “branches".  Brought to you in partnership with Wakanda

Topics:
php 7 ,email ,tutorial ,swift

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}