codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Follow publication

Send Emails with the PHP Mailer

Any decent web application has to communicate with users on multiple levels. Simply displaying information is not enough, web apps should also be able to send emails, SMS messages, and more, to ensure good user engagement.

The PHP Mailer is a full-featured email creation and transfer class for PHP. It is an open-source library — available on Github — that is easy to install and even easier to use. We can use it in a web application to easily send out emails using a configured SMTP server.

This article walks through the steps for using the PHP Mailer to send emails using AWS SES, as well as Gmail.

Installation

Let’s start with the installation. You should have a XAMPP or LAMP installed on your server.

With this in place, create a new folder project in the PHP scripts folder (/var/www/html/) or (C:/xampp/htdocs). Let’s call this folder “email”. You can install the PHP Mailer module inside this folder.

Composer

Before we start installing the PHP Mailer, we need the Composer (this is kind of a package manager for PHP).

To install the Composer, follow these simple steps

  • Download the installer script
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
  • Verify the downloaded installer
php -r "if (hash_file('sha384', 'composer-setup.php') === '795f976fe0ebd8b75f26a6dd68f78fd3453ce79f32ecb33e7fd087d39bfeb978342fb73ac986cd4f54edd0dc902601dc') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
  • Run the installer script to install the Composer
php composer-setup.php
  • Finally remove the installer, to free up space
php -r "unlink('composer-setup.php');"

The installer script will take care of almost everything you need. It will verify settings in the php.ini, and also set up the composer.phar file in the same folder.

PHP Mailer

Now, it is a simple task to install the PHP Mailer.

composer require phpmailer/phpmailer

This composer command will install and add the new line in the composer.json, and take care of the complete installation. If you would like to skip the Composer install then you can just download the code from its Github repository, and save it in a modules folder — in the include_path. That should be enough to setup the PHP Mailer

Code

With those things in place, it is a simple task to write the necessary code. Add the following snippets in the required PHP script:

Include

Provide the appropriate paths for the require statements.

SMTP

Next, create an mailer object

Note that I have used Gmail in this example, but you can use any SMTP server that you like. If you use Gmail as well, note that this requires some settings to be altered within Gmail

Mail Content

Now we can compose the mail with the specific contents:

We can also add any attachments. There are two ways to add attachments. We can attach a file on the server or we can also push in base64 encoded content along with it:

Send Mail

Finally, we invoke the method to send the mail

Now go and check your mailbox! You should have a mail there. If the mail does not reach, or lands in the SPAM, you can verify some settings above.

Original post: https://thewiz.net/send_emails_with_php_mailer_cc07.html

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response