How To Send Email In PHP (Very Simple Examples)

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.

  1. Use the native mail function – mail("jon@doe.com", "Subject", "Message");
  2. Use the PHPMailer library.
    • $mail = new PHPMailer;
    • $mail->setFrom("your@email.com");
    • $mail->addAddress("jon@doe.com");
    • $mail->Subject = "Subject";
    • $mail->Body = "Message";
    • $mail->send();

That covers the basics, but read on for more details and examples!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

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.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

PHP MAIL EXAMPLES

All right, let us now get into the examples of sending mail in PHP.

 

EXAMPLE 1) NATIVE MAIL FUNCTION

1-mail.php
<?php
// (A) SET MAIL DATA
$to = "jon@doe.com";
$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

2-mail-header.php
<?php
// (A) SET MAIL DATA
$to = "jon@doe.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 <your@email.com>",
  "Cc: joe@doe.com",
  "Bcc: joy@doe.com"
]);
 
// (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

3-phpmailer.php
<?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("your@email.com");
$mail->addAddress("jon@doe.com");
$mail->addCC("joe@doe.com");
$mail->addBCC("joy@doe.com");
$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/phpmailer and that’s it. Composer will automatically download the latest version into the vendor/ folder.
  • In your PHP script, just require vendor/autoload.php to 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.

 

 

EXTRAS

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:

PHP Mail Not Working Not Sending – How To Fix It!

 

LINKS & REFERENCES

 

THE END

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!