Welcome to a quick tutorial and examples of how to send an email in PHP. New to PHP and need to send out emails? It is actually very straightforward.
There are 2 common ways to send emails in PHP.
- Use the native mail function –
mail("email@example.com", "Subject", "Message");
- Use the PHPMailer library.
$mail = new PHPMailer;
$mail->Subject = "Subject";
$mail->Body = "Message";
That covers the basics, but read on for more details and examples!
ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
TLDR – QUICK SLIDES
Fullscreen Mode – Click Here
TABLE OF CONTENTS
DOWNLOAD & NOTES
Firstly, here is the download link to the example code as promised.
QUICK NOTESIf you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.
EXAMPLE CODE DOWNLOAD
Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
PHP MAIL EXAMPLES
All right, let us now get into the examples of sending mail in PHP.
EXAMPLE 1) NATIVE MAIL FUNCTION
<?php // (A) SET MAIL DATA $to = "firstname.lastname@example.org"; $subject = "Test Email"; $message = "This is a test email message."; // (B) SEND! echo mail($to, $subject, $message) ? "OK" : "ERROR" ;
Yep, PHP comes with a native
mail() function, and it is self-explanatory. Just plug in the recipient, subject, and message. That’s it. Just take note that it returns a boolean
true/false to indicate if the mail has been successfully sent.
EXAMPLE 2) MAIL WITH MORE OPTIONS
<?php // (A) SET MAIL DATA $to = "email@example.com"; $subject = "Test Email"; $message = "This is a test email message."; // (B) NOT-SO-FRIENDLY WHEN SETTING FROM, CC, BCC, HTML MAIL, ETC... $header = implode("\r\n", [ "MIME-Version: 1.0", "Content-type: text/plain; charset=utf-8", "From: YOUR NAME <firstname.lastname@example.org>", "Cc: email@example.com", "Bcc: firstname.lastname@example.org" ]); // (C) SEND! echo mail($to, $subject, $message, $header) ? "OK" : "ERROR" ;
While the basics of the
mail() function is simple, things can get pretty hairy when it comes to setting things like the Cc, Bcc, and creating an HTML mail – We can only do so by manually setting the email headers… It is even worse when it comes to attachments.
EXAMPLE 3) PHP MAILER LIBRARY
<?php // (A) INSTALL PHP MAILER FIRST! // https://github.com/PHPMailer/PHPMailer // https://getcomposer.org/ // composer require phpmailer/phpmailer // (B) LOAD PHP MAILER use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception; require "vendor/autoload.php"; // (C) SET MAIL $mail = new PHPMailer; $mail->setFrom("email@example.com"); $mail->addAddress("firstname.lastname@example.org"); $mail->addCC("email@example.com"); $mail->addBCC("firstname.lastname@example.org"); $mail->Subject = "Test Email"; $mail->Body = "This is a test email message."; // (D) SEND! echo $mail->send() ? "Mail sent" : "Error sending mail" . $mail->ErrorInfo ;
That brings us to the final example, sending mail with a popular library called PHPMailer. For you guys who are new, I will highly recommend the easy way of installation:
- Download and install a package manager called Composer.
- Open the command prompt (or terminal), navigate to your project folder.
- Run the command
composer require phpmailer/phpmailerand that’s it. Composer will automatically download the latest version into the
- In your PHP script, just
require vendor/autoload.phpto load PHPMailer. Composer can also be used to manage a thousand other packages for your project this way.
I think the rest is Captain Obvious. PHPMailer may seem complicated at first, but it removes the need to manually formulate the email headers. We can even set it to connect to Gmail, Outlook, and other mail providers easily. Will leave a link to the documentation below if you want to learn more.
EXTRA BITS & LINKS
That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.
MAIL IS NOT SENDING OUT!
You need an SMTP server for PHP to send emails. Follow up with my other guide if you are having trouble:
LINKS & REFERENCES
- Mail – PHP
- PHPMailer Documentation – GitHub
- PHP Send HTML & UTF8 Email (For Non-English Languages) – Code Boxx
- PHP Set Multiple Email Recipients – Code Boxx
- Send Email Attachments In PHP – Code Boxx
- PHP Send Email With HTML Template – Code Boxx
INFOGRAPHIC CHEAT SHEET
Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!