October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

Sending Email with Swift Mailer: SMTP Setup, Example, and Symfony Mailer Migration

A practical Swift Mailer SMTP guide with Composer installation, working PHP code, accepted-recipient semantics, transport trade-offs, troubleshooting, and a Symfony Mailer migration path.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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:

<?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_SmtpTransport connects 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
$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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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

  1. Identify every Swift transport and the message-building code that uses it.
  2. Record the existing SMTP host, port, encryption mode, authentication requirements, sender identities, and recipient behavior.
  3. Install symfony/mailer and configure a DSN for the same server or for the provider transport you intend to adopt.
  4. Replace Swift message construction and sending calls with Symfony Mailer’s message and transport APIs.
  5. Test accepted-recipient handling, authentication failures, TLS negotiation, HTML or plain-text content, and bounce monitoring in a non-production environment.
  6. 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The 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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.