How to send an email in PHP with PHPMailer: a simple and fast guide

Do you want to send an email with PHP professionally and securely? PHPMailer is one of the most used libraries for sending messages from your website. In this guide, we explain how to configure it in a few steps, with working code examples and practical tips to avoid the most common errors.

Programmazione PHP
Programmazione PHP

Do you want to send emails from your PHP site securely, professionally, and in a personalized way? PHPMailer is one of the most effective solutions for doing so. In this guide, we will show you step by step how to send an email in PHP with PHPMailer, with working code examples, clear explanations, and tips to avoid common errors. A useful resource for developers, freelancers, and those managing small dynamic sites.

What is PHPMailer and why use it

PHPMailer is an open-source PHP library that allows sending emails via SMTP, with support for HTML, attachments, authentication, and much more. It is considered more secure and flexible than PHP’s native ”
mail() function.

Advantages of PHPMailer:

  • Compatibility with SMTP servers (like Gmail, Sendinblue, Mailgun, etc.)
  • Support for HTML content and attachments
  • Greater control over message structure
  • Secure SMTP authentication (TLS, SSL)

Requirements and installation

To use PHPMailer, you must first include it in your project. The easiest way is with Composer:

composer require phpmailer/phpmailer

If you don’t use Composer, you can download the library from the official GitHub repository.

First example: sending a simple email

Here is a basic example for sending an email in PHP with PHPMailer:

use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    // Configurazione server SMTP
    $mail->isSMTP();
    $mail->Host = 'smtp.example.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'tuo@email.com';
    $mail->Password = 'tuapassword';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;

    // Mittente e destinatario
    $mail->setFrom('tuo@email.com', 'Il tuo nome');
    $mail->addAddress('destinatario@email.com', 'Nome destinatario');

    // Contenuto del messaggio
    $mail->isHTML(true);
    $mail->Subject = 'Oggetto della mail';
    $mail->Body    = 'Questo è il <b>messaggio</b> HTML';
    $mail->AltBody = 'Questo è il messaggio in testo semplice';

    $mail->send();
    echo 'Messaggio inviato con successo';
} catch (Exception $e) {
    echo "Errore nell'invio: {$mail->ErrorInfo}";
}

Tips for professional use

  • Protect SMTP credentials: do not enter username and password directly into the code. Use environment variables.
  • Validate user input: if you use PHPMailer for contact forms, always filter and validate the data.
  • Handle errors with logs: instead of showing errors on screen, write them to a log file.
  • Verify SPF and DKIM: to improve deliverability, correctly configure these records on your domain.

Alternatives to PHPMailer

If you want to explore other options, consider:

  • SwiftMailer (now deprecated but still in use)
  • Symfony Mailer (recommended for modern projects)
  • SendGrid SDK (for those using external platforms)

Conclusion

PHPMailer is an essential tool for those developing with PHP who want to manage email sending in a secure, flexible, and professional manner. Whether you are building a contact form, a notification system, or a newsletter, this library can make a difference. Follow the guide, adapt the code to your project, and immediately improve your site’s email communication.

Pubblicato in

Se vuoi rimanere aggiornato su How to send an email in PHP with PHPMailer: a simple and fast guide iscriviti alla nostra newsletter settimanale

Be the first to comment

Leave a Reply

Your email address will not be published.


*