How To Send Email In PHP – 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!

ⓘ 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.

 

 

REAL QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes PHP Mail Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

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.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer 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.

 

 

PHP MAIL EXAMPLES

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

 

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.

 

 

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.

 

 

3) PHP MAILER LIBRARY

3-phpmailer.php
<?php
// (A) INSTALL PHP MAILER FIRST!
// 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.

 

 

USEFUL 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.

 

INFOGRAPHIC CHEAT SHEET

Basic Send Email In PHP (click to enlarge)

 

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!

Leave a Comment

Your email address will not be published. Required fields are marked *