You can send mail with Swift Mailer by installing it with Composer, creating a transport, passing that transport to Swift_Mailer, building a Swift_Message, and calling send(). However, Swift Mailer has been unmaintained since the end of November 2021. Use the procedure below for an existing application, but choose Symfony Mailer for new development.
Swift Mailer’s status in 2026
Swift Mailer’s official project page says maintenance stopped at the end of November 2021, and its sending documentation explicitly recommends Symfony Mailer instead. The project documentation lists PHP 7.0 through PHP 8.1 as its historical compatibility range; that is not a current support promise.
That makes Swift Mailer mainly a maintenance concern: keep it running when replacing it immediately would be risky, but plan migration for any application that still depends on it.
Install Swift Mailer with Composer
From the PHP project directory, run the official Composer command:
#1 Best Overall
composer require "swiftmailer/swiftmailer:^6.0"
Composer places the library and its autoloader in the project’s vendor directory. Your PHP script must load that autoloader before creating a transport.
Send a basic message through SMTP
The core pattern is Transport → Mailer → Message → send(). This complete example uses SMTP on port 25:
Rank #2
<?php
require_once '/path/to/vendor/autoload.php';
$transport = (new Swift_SmtpTransport('smtp.example.org', 25))
->setUsername('your username')
->setPassword('your password');
$mailer = new Swift_Mailer($transport);
$message = (new Swift_Message('Wonderful Subject'))
->setFrom(['[email protected]' => 'John Doe'])
->setTo(['[email protected]'])
->setBody('Here is the message itself');
$result = $mailer->send($message);
What each object does
- Transport: Defines how Swift Mailer connects to the mail system.
Swift_SmtpTransportconnects to a remote SMTP server. - Mailer: Uses the transport to submit messages.
- Message: Holds the subject, sender, recipients, and body.
send(): Attempts submission and returns the number of recipients accepted by the transport.
The SMTP connection is established when the first send() call occurs, not when the transport object is constructed.
Configure SMTP authentication and encryption
Set the host and port required by your SMTP provider, then add credentials when the server requires authentication. Swift Mailer supports ssl and tls encryption settings on the SMTP transport. The correct encryption mode and port are provider-specific, so use the values supplied by that provider rather than assuming the example’s port 25 is appropriate.
$transport = (new Swift_SmtpTransport('smtp.example.org', 25, 'tls'))
->setUsername('your username')
->setPassword('your password');
Keep usernames and passwords outside source control, such as in environment variables or your deployment secret store. A wrong hostname, port, credential, or TLS mode commonly prevents the connection before a message can be accepted.
Interpret the return value correctly
$mailer->send($message) returns an integer: the number of recipients the transport accepted. A return value of zero means no recipient was accepted. A positive value confirms SMTP acceptance for those recipients, not final delivery to an inbox; later filtering, rejection, or a recipient-side bounce can still occur.
Rank #4
SMTP, Sendmail, or a provider transport?
The transport choice changes where delivery responsibility sits and how much feedback your application receives.
| Option | Deployment dependency | Authentication and encryption | Feedback | Portability and maintenance |
|---|---|---|---|---|
| SMTP | Remote SMTP server | Supports server authentication and ssl/tls settings |
Swift Mailer’s guide describes useful feedback, including accepted-recipient counts | Portable between hosts when server details and credentials are configured |
| Sendmail | Local Sendmail-compatible executable and mail-transfer agent | Handled by the local mail system rather than a remote Swift SMTP session | Generally less accurate feedback than SMTP | Tied to local server setup; moving the application requires reproducing that executable and MTA configuration |
| Third-party provider API | Provider account and its transport integration | Provider-specific credentials and API security | Depends on the provider integration and its response model | Can reduce mail-server administration; Symfony Mailer supports third-party provider transports |
For most hosted applications, SMTP is the portable Swift Mailer choice because the application connects directly to a remote server and receives useful submission feedback. Sendmail can make sense only when the host already operates the required local MTA.
Free tools Windows power users keep installed
One-click scans. No signup required.
Use Symfony Mailer for a new project
Symfony Mailer is the maintained successor recommended by Swift Mailer’s project and documentation. Install it with Composer:
composer require symfony/mailer
Symfony Mailer uses a DSN to describe the transport. An SMTP DSN follows this form:
smtp://user:[email protected]:port
It also supports SMTP, Sendmail, native, and third-party transports. Encode usernames, passwords, and host values when they contain characters reserved by URI syntax; otherwise the DSN can be parsed incorrectly. Configure the DSN through environment or deployment secrets rather than committing credentials in application code.
Migration approach
- Identify every Swift transport and the message-building code that uses it.
- Record the existing SMTP host, port, encryption mode, authentication requirements, sender identities, and recipient behavior.
- Install
symfony/mailerand configure a DSN for the same server or for the provider transport you intend to adopt. - Replace Swift message construction and sending calls with Symfony Mailer’s message and transport APIs.
- Test accepted-recipient handling, authentication failures, TLS negotiation, HTML or plain-text content, and bounce monitoring in a non-production environment.
- Remove the Swift dependency after production traffic has been verified on the new transport.
Troubleshoot common sending failures
Connection fails immediately
- Verify the SMTP hostname resolves from the application server.
- Confirm the provider’s port and whether it expects TLS or SSL.
- Check outbound firewall rules and whether the hosting platform blocks the selected port.
Authentication is rejected
- Check the username and password independently of the message content.
- Confirm that the account is permitted to submit through the selected SMTP host.
- Make sure secrets were not altered by quoting, whitespace, or environment-variable formatting.
send() returns zero
No recipient was accepted by the transport. Inspect the transport connection and server response, then verify that the recipient address was actually added with setTo() or another recipient method.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Clear out junk files and repair common Windows errors3Fix the driver behind crashes, sound loss and screen glitchesThe call succeeds but the message is not visible
A positive accepted-recipient count is not proof of inbox delivery. Check the recipient’s spam or quarantine folders, provider logs, and bounce notifications before changing application code.
Quick Recap
Practical decision
- Maintaining an old Swift Mailer application: Keep the existing flow stable, document its SMTP settings, and schedule a Symfony Mailer migration.
- Starting a PHP application now: Install Symfony Mailer instead of adding the unmaintained Swift Mailer package.
- Choosing a delivery method: Use SMTP when portability and submission feedback matter, Sendmail only when a dependable local MTA is already managed, and a provider transport when you want provider-managed delivery infrastructure.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




