Email Sending in PHP: PHPMailer, SwiftMailer, and mail() — Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Sending Email in PHP: Overview of Libraries and Tools



Email sending is an integral part of any web application: user registration, password reset, notifications, marketing campaigns. In PHP, there are several ways to send emails: from the built-in mail() function to specialized libraries such as PHPMailer and SwiftMailer. In this article, we will thoroughly review each tool, its advantages, disadvantages, and provide practical code examples.



1. The Built-in mail() Function



The simplest way is to use the native PHP function mail(). It does not require installing additional libraries, but has serious limitations.



Syntax

mail($to, $subject, $message, $headers, $parameters);


Example of Sending a Simple Text Email

<?php$to = 'user@example.com';$subject = 'Greeting';$message = 'Hello! This is a test email.';$headers = 'From: noreply@pythonlib.ru' . "\\r" .           'Reply-To: support@pythonlib.ru' . "\\r" .           'X-Mailer: PHP/' . phpversion();

if(mail($to, $subject, $message, $headers)) { echo 'Email sent.';} else { echo 'Sending error.';}?>


Disadvantages of mail()

  • Depends on server settings (sendmail, postfix, etc.) — often ends up in spam.
  • Does not support HTML emails and attachments without manually crafting MIME headers.
  • Lacks SMTP authentication support.
  • Difficult to debug errors.


Due to these limitations, it is recommended to use specialized libraries for serious projects.



2. PHPMailer — The Most Popular Library



PHPMailer is a powerful and convenient library for sending email in PHP. It supports HTML, attachments, SMTP authentication, TLS/SSL encryption, and many other features.



Installation

composer require phpmailer/phpmailer

Or download it manually from GitHub.



Example of Sending an HTML Email via Gmail SMTP

<?phpuse PHPMailer\\PHPMailer\\PHPMailer;use PHPMailer\\PHPMailer\\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try { // Server settings $mail->isSMTP(); $mail->Host = 'smtp.gmail.com'; $mail->SMTPAuth = true; $mail->Username = 'your-email@gmail.com'; $mail->Password = 'your-app-password'; $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; $mail->Port = 587;

// Sender and recipient $mail->setFrom('from@example.com', 'Your Name'); $mail->addAddress('user@example.com', 'Recipient');

// Attachments $mail->addAttachment('/path/to/file.pdf', 'document.pdf');

// Content $mail->isHTML(true); $mail->Subject = 'Greetings from PHPMailer'; $mail->Body = '<h1>Heading</h1><p>This is an <b>HTML</b> email.</p>'; $mail->AltBody = 'Text version for email clients without HTML';

$mail->send(); echo 'Email sent.';} catch (Exception $e) { echo "Error: {$mail->ErrorInfo}";}?>


Advantages of PHPMailer

  • Simple and clear API.
  • Support for all modern email protocols.
  • Automatic encoding of attachments.
  • Good documentation and a large community.


3. SwiftMailer — A Flexible Alternative



SwiftMailer is another popular library that provides an object-oriented interface for sending emails. Unlike PHPMailer, it uses the concept of "Transports" and "Messages".



Installation

composer re                    

Recommendations