Send Email Using Swift Mailer

Send Email Using Swift Mailer

It provides all the steps required to send emails using Swift Mailer.

December 26, 2019

In certain conditions, we might be required to send emails using PHP without installing the email server. We can do so by installing and configuring Swift Mailer. This tutorial provides all the steps required to install and configure Swift Mailer using Composer to send emails using PHP.

Install Swift Mailer

In case your project is already using the composer, you just need to add this line within the require section of your composer.json file. If you are new to Composer, you can follow How To Install Composer On Windows to install Composer and start using it as a project dependency manager.

"require" : {
....
....
"swiftmailer/swiftmailer": "6.2.*",
....
....
}

After updating the file, simply run the composer update command to install Swift Mailer.

You can also install Swift Mailer using the command within the project root directory as shown below.

# Install Swift Mailer

composer require swiftmailer/swiftmailer

# OR

composer require swiftmailer/swiftmailer:6.2.*

# Output

Using version ^6.2 for swiftmailer/swiftmailer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 7 installs, 0 updates, 0 removals
- Installing symfony/polyfill-php72 (v1.13.1): Loading from cache
- Installing symfony/polyfill-mbstring (v1.13.1): Loading from cache
- Installing symfony/polyfill-intl-idn (v1.13.1): Loading from cache
- Installing symfony/polyfill-iconv (v1.13.1): Loading from cache
- Installing doctrine/lexer (1.0.2): Downloading (100%)
- Installing egulias/email-validator (2.1.12): Loading from cache
- Installing swiftmailer/swiftmailer (v6.2.3): Loading from cache
swiftmailer/swiftmailer suggests installing true/punycode (Needed to support internationalized email addresses, if ext-intl is not installed)
Writing lock file
Generating autoload files

Send Mail Using Swift Mailer

In this step, we will use Swift Mailer to trigger emails. The below-mentioned PHP script can be used to trigger emails using Swift Mailer using Sendmail. This won't work on Windows since the Sendmail transport is available only on Linux systems. It goes to infinite wait on Windows using the fake Sendmail.

<?php
require __DIR__ . '/vendor/autoload.php';

try {

// Create SendMail Transport
$transport = new Swift_SendmailTransport( '/usr/sbin/sendmail -bs' );

// Mailer
$mailer = new Swift_Mailer( $transport );

// Message
$message = new Swift_Message();

// Subject
$message->setSubject( 'Welcome Swift Mailer' );

// Sender
$message->setFrom( [ 'sender@example.com' => 'Sender Name' ] );

// Recipients
$message->addTo( 'recipient@example.com', 'Recipient Name' );

// CC - Optional
$message->addCc( 'cc@example.com', 'CC Name' );

// BCC - Optional
$message->addBcc( 'bcc@example.com', 'BCC Name' );

// Body
$message->setBody( "Mail sent using <b>Swift Mailer</b>.", 'text/html' );

// Send the message
$result = $mailer->send( $message );
}
catch( Exception $exc ) {

echo $exc->getMessage();
}

Make sure that the appropriate Email server is installed on the system, php.ini and sendmail.ini are configured to use either localhost or SMTP server.

We can also configure Swift Mailer to use an SMTP server as shown below.

<?php
require __DIR__ . '/vendor/autoload.php';

try {

// Create SMTP Transport
//$transport = new Swift_SmtpTransport( 'tls://smtp.gmail.com', 587, 'tls' );

// Authentication
//$transport->setUsername( 'yourname@gmail.com' );
//$transport->setPassword( 'password' );

// Mailer
$mailer = new Swift_Mailer( $transport );

// Message
$message = new Swift_Message();

// Subject
$message->setSubject( 'Welcome Swift Mailer' );

// Sender
$message->setFrom( [ 'yourname@gmail.com' => 'Sender Name' ] );

// Recipients
$message->addTo( 'recipient@gmail.com', 'Recipient Name' );

// CC - Optional
$message->addCc( 'cc@gmail.com', 'CC Name' );

// BCC - Optional
$message->addBcc( 'bcc@gmail.com', 'BCC Name' );

// Body
$message->setBody( "Mail sent using <b>Swift Mailer</b>.", 'text/html' );

// Send the message
$result = $mailer->send( $message );
}
catch( Exception $exc ) {

echo $exc->getMessage();
}

Additionals

We can also add an attachment by configuring the Swift Mailer as shown below.

// Attachment
$attachment = Swift_Attachment::fromPath( '/path/to/attachment' );

$attachment->setFilename( 'attachment-name' );
$message->attach( $attachment );

An inline attachment might be required to attach the images to be displayed in the email message. It can be done as shown below.

// Embed Attachment
$attachment = Swift_Image::fromPath( '/path/to/image' );

$image = $message->embed( $attachment );

// Append to Body
$message->addPart( 'Attach image: <br><img src="' . $image . '" width="350"><br>', 'text/html' );

Summary

In this tutorial, we have installed Swift Mailer and used it to send emails by configuring it.

You can also post your comments to join the discussion. The other relevant tutorials for PHP includes How To Send Email Using PHP, Send Email Using PHPMailer, How To Use Sendmail On Windows To Send Email Using PHP, How To Configure PHP OpenSSL Module On Windows, How To Install XAMPP On Windows, How To Install WampServer on Windows, How To Install PHP 7 On Ubuntu 18.04 LTS, and How To Install PHP 7 On Windows.

Write a Comment
Click the captcha image to get new code.
Discussion Forum by DISQUS