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

Courses