Swift Mailer
Open Source Component Based PHP 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?
Swift Mailer is a handy library for software developers seeking to build robust PHP applications that handle email messages. By utilizing different MIME entities, Swift Mailer simplifies the process of crafting intricate email messages with minimal hassle. Plus, it’s open source and operates under the MIT License, making it a great tool for your PHP projects.
You can smoothly add the Swift Mailer library to your PHP web application. This library follows a flexible and elegant object-oriented method for sending emails, offering a wide range of features. It handles various email aspects, including configuring different transports and personalizing the messages you send. You can send emails through SMTP, sendmail, postfix, or even a custom Transport setup. The library is very secure and protects from header injection attacks without stripping request data content.
The Swift Mailer library is user-friendly and packed with handy features to help you manage your emails effortlessly. You can do things like include content in your email, attach files from your computer or existing ones, add inline media, use dynamic content, send to multiple recipients, encrypt your email, set message priority, and more.
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());