Free cookie consent management tool by TermsFeed Generator PHP Send SMTP Emails with PHPMailer | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Send SMTP Emails with PHPMailer

PHP Send SMTP Emails with PHPMailer

Last Updated on Mar 22, 2023

Introduction

Sending emails is a very useful feature. We can send emails for:

  • Contact us form
  • Newsletter
  • Thank you for your purchase
  • Status updates
  • And many other scenarios

Sending emails with PHP is very easy. We can use the built-in mail function or we can use a package.

The built-in mail function is good but it does not provide any assistance for making use of popular features such as encryption, authentication, HTML messages, and attachments.

That’s why I recommend using a package. And today we are going to send an email with PHPMailer package.

Do you remember when we talked about Dependency management?

Ok now let’s get started.

PHPMailer

Let’s require that in our project by running:

composer require phpmailer/phpmailer

Great now inside the vendor folder all your dependencies for PHPMailer are added. Now let’s send our email with php.

In my index.php file first I need to use PHPMailer and require the autoload file

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require './vendor/autoload.php';

Now let’s create our instance

$mailer = new PHPMailer();

Configs

First we tell it that we want to send an smtp email

$mailer->isSMTP();

Great now let’s add our smtp settings

// this can be gmail,yandex,etc or your own server
$mailer->Host = 'smtp.example.com';
//Enable SMTP authentication
$mailer->SMTPAuth = true;
// user name and password
$mailer->Username = 'test@example.com';
$mailer->Password = 'password';

//Enable TLS encryption
$mailer->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
// set the port
// use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
$mailer->Port = 465;

Sender of the Email

Let’s set who is sending the email and who would receive the replies

$mailer->setFrom('test@example.com', 'Test Mail Sender');
$mailer->addReplyTo('test@example.com', 'Test Mail Sender');

Add Addresses

Now let’s add the recipients (one or many)

$mailer->addAddress('email@test.com', 'User A');
// if you want to add more addresses simply add them by running this again
$mailer->addAddress('another.email@test.com', 'User B');

CC & BCC

We can also add cc and bcc

$mailer->addCC('tech@example.com');
$mailer->addBCC('ceo@example.com');

HTML Emails

Now let’s get to the content. We can write HTML content or just text content. OR even better, we can write an html email and then add an alternative text content so if the html was not supported for our client we would be sending them the text email

$mailer->isHTML(true);
$mailer->Subject = 'Hello this is a test Email';
$mailer->Body    = "I am sending you this email form PHP</b>we are using PHPMailer</br>isn't it cool?!";
// and the alternative text body
$mailer->AltBody = "You're not accepting HTML emails! I am sending you this email form PHP.we are using PHPMailer.isn't it cool?!";

Add Attachments

If you have attachments you can easily send them as well. The first argument is the path to the file and the second argument is the name which is optional

$mailer->addAttachment('path/to/the/file','image.jpg');

Sending the email

Now we can easily send this email

$mailer->send();

The full code looks like this

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require './vendor/autoload.php';

$mailer = new PHPMailer();
$mailer->isSMTP();

// this can be gmail,yandex,etc or your own server
$mailer->Host = 'smtp.example.com';
//Enable SMTP authentication
$mailer->SMTPAuth = true;
// user name and password
$mailer->Username = 'test@example.com';
$mailer->Password = 'password';

//Enable TLS encryption
$mailer->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
// set the port
// use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
$mailer->Port = 465;

$mailer->setFrom('test@example.com', 'Test Mail Sender');
$mailer->addReplyTo('test@example.com', 'Test Mail Sender');

$mailer->addAddress('email@test.com', 'User A');
$mailer->addAddress('another.email@test.com', 'User B');

$mailer->addCC('tech@example.com');
$mailer->addBCC('ceo@example.com');

$mailer->isHTML(true);
$mailer->Subject = 'Hello this is a test Email';
$mailer->Body    = "I am sending you this email form PHP</br>we are using PHPMailer</br>isn't it cool?!";
// and the alternative text body
$mailer->AltBody = "You're not accepting HTML emails! I am sending you this email form PHP.we are using PHPMailer.isn't it cool?!";

//$mailer->addAttachment('path/to/the/file','image.jpg');
$mailer->send();

If you want to learn more details about PHPMailer you can read PHPMailer's documentations.

The alternative to PHPMailer package is Swiftmailer which is no longer maintained

Symfony mailer is the other alternative which you can read more about on their website

With the power of PHP and these amazing packages, really the sky's the limit for your web applications.

https://youtu.be/1kBtYvfl8p0

Conclusion

Now you know about sending SMTP emails in PHP with PHPMailer.

I recommend you to open a PHP and start sending some simple emails to yourself and see the results.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • introduction to PHPMailer package
  • how to send Emails in PHP
  • how to send HTML emails
  • Configurations for SMTP emails
  • How to add attachments to your emails

Category: programming

Tags: #php #tips and tricks

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

How to use Git Alias to increase your productivity (video)
Nov 27, 2022 programming

How to use Git Alias to increase your productivity (video)

Today we are going to talk about Git Alias and how to increase your productivity by adding shorter aliases for commands that we use everyday. ...

8 Min Read Read More
PHP Arrays
Mar 21, 2023 programming

PHP Arrays

Today we are going to learn about arrays in PHP. Array is a datatype that can hold more than one value so instead of having multiple variables you can hold all those values in one variable. ...

9 Min Read Read More
PHP compact
Feb 14, 2023 programming

PHP compact

Today we are going to talk about compact function in PHP. Compact is one of those functions that helps the readability of your code a lot and also reduces the lines of code. ...

4 Min Read Read More
PHP Beginner to Advanced 2025: The Complete Guide to Mastering PHP and Building Modern Web Applications
Jul 17, 2025 programming

PHP Beginner to Advanced 2025: The Complete Guide to Mastering PHP and Building Modern Web Applications

PHP is often called the engine room of the modern web. Despite debates about newer technologies like Node.js, Go, or Python’s Django, PHP has remained a consistent favorite among developers. In fact, as of 2025, PHP powers over 75% of all websites that use a known server-side programming language. ...

9 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...