1. Products
  2.   Email
  3.   PHP
  4.   Swift Mailer

Swift Mailer

 
 

Open Source Component Based PHP Email Library

Free PHP API that Allows Sending Emails using SMTP, Sendmail, Postfix, or a Custom Transport Implementation. Add Content to an Email Message, Attach Files on Disk or Existing Files, Embed Inline Media Files & so on. 

What is Swift Mailer Library?

Crafted for developers building robust PHP applications, Swift Mailer is an essential open-source library designed to streamline the complex task of handling and sending sophisticated email messages. Distributed under the flexible MIT License, it provides a powerful yet accessible foundation for any PHP project. The library excels at simplifying the creation of intricate emails through its intelligent use of MIME entities, allowing developers to construct multi-part messages with minimal hassle. Its flexible and elegant object-oriented architecture ensures seamless integration into your PHP web application, offering a secure and comprehensive approach to email management that actively guards against threats like header injection attacks without compromising your data.

At its core, Swift Mailer is both user-friendly and incredibly powerful, packed with features that make managing every aspect of email effortless. It provides complete control over message personalization, from embedding dynamic content and setting message priority to sending encrypted communications. You can easily include various content types, attach files from local paths or existing sources, and add inline media directly into the email body. The library supports sending to multiple recipients and offers versatile transport configuration, allowing you to dispatch emails via SMTP, sendmail, postfix, or a custom Transport setup of your choice. This combination of security, flexibility, and a rich feature set—including support for complex MIME structures and dynamic content—makes Swift Mailer a trusted and indispensable tool for developers seeking to implement professional-grade email functionality.

Previous Next

Getting Started with Swift Mailer

The recommended way to install the Swift Mailer component is via Composer, please use the following command for easy installation.

Install Swift Mailer via Composer

$ composer require "swiftmailer/swiftmailer:^6.0" 

Create and Send Email Messages via PHP

The open source Swift Mailer library has included support for creating email messages inside your PHP applications. The library has provided several important features to achieve the task such as adding content to your message body, formatting the content, attaching different kinds of files, embedding dynamically generated content, embedding media files into the message, sending an email message to multiple recipients and so. Here is an example that shows how software developers can create and send a new email messages using PHP library.

How to Send Email Messages via PHP Library?

require_once '/path/to/vendor/autoload.php';

// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.example.org', 25))
  ->setUsername('your username')
  ->setPassword('your password')
;

// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);

// Create a message
$message = (new Swift_Message('Wonderful Subject'))
  ->setFrom(['john@doe.com' => 'John Doe'])
  ->setTo(['receiver@domain.org', 'other@domain.org' => 'A name'])
  ->setBody('Here is the message itself')
  ;

// Send the message
$result = $mailer->send($message);

Send Emails with Attachments via PHP

The free library Swift Mailer allows software programmers to send emails message with attachments from inside their PHP applications. You need to instantiate the Swift_Attachment object and use the attach method to include your attachments. Apart from the file attachments, you can also embed images in the email message text. The library also fully supports the embedding of dynamically generated content without having an existing file available. You can just use one-liner code to embed already existing files or use URL.Here is an example that shows how software developers can generate and send email messages with attachments inside it via PHP API.

How to Add Attachments to Email Messages inside PHP Applications?

// Create the attachment
// * Note that you can technically leave the content-type parameter out
$attachment = Swift_Attachment::fromPath('/path/to/image.jpg', 'image/jpeg');

// Attach it to the message
$message->attach($attachment);

// The two statements above could be written in one line instead
$message->attach(Swift_Attachment::fromPath('/path/to/image.jpg'));

// You can attach files from a URL if allow_url_fopen is on in php.ini
$message->attach(Swift_Attachment::fromPath('http://site.tld/logo.png'));

Emails Encoding via PHP API

The Swift Mailer library makes it easy for software developers to add encoding capability to their PHP applications. The library has included support for encoding the body of the MIME part of the email message. It also allows encoding the binary attachments using base64. The text parts are encoded using quoted-printable which is a safe choice and most modern SMTP servers support it.

Using Custom Headers to Your Emails

The open source Swift Mailer library enables software programmers to include message headers using PHP commands. The library supports adding a customized header to an email message as well as modifying an existing one with ease. Modifying the existing header is a very difficult task as there is very little difference between the headers. The Swift Mailer library used different types of MIME headers which are categorized into more general groups, such as text headers, parameterized headers, date headers, ID headers, and path Headers. The following example demonstrates how to add new headers using HeaderSet by using one of the provided add..Header() methods. The added header will appear in the message when it is sent.

How to Add New Header to an Email Messages inside PHP Application?

// Adding a custom header to a message
$message = new Swift_Message();
$headers = $message->getHeaders();
$headers->addTextHeader('X-Mine', 'something here');

// Adding a custom header to an attachment
$attachment = Swift_Attachment::fromPath('/path/to/doc.pdf');
$attachment->getHeaders()->addDateHeader('X-Created-Time', time());

 English