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!
TLDR – QUICK SLIDES
Fullscreen Mode – Click Here
TABLE OF CONTENTS
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.
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
600+ free tutorials & projects on Code Boxx and still growing. I insist on not turning Code Boxx into a "paid scripts and courses" business, so every little bit of support helps.
Buy Me A Meal Code Boxx eBooks
EXAMPLE CODE DOWNLOAD
Click here for the source code on GitHub gist, just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
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!