Send Mail using PHP

October 27, 2014 // In: PHP

Sending mail is very necessary & required feature in almost every website/web application. There are two ways to send mail using PHP. After reading this article you will be able to send mail using both methods.

  1. Built-in method
  2. Third party library like phpmailer (recommended)

I will recommend you to use phpmailer instead of built-in method. You can say, phpmailer is extended version of built-in method.

sendmail_using_php_mailer

Built-in Method

mail() is built-in method which is very easy and straight forward.

mail ( $receiver_email , $subject , $message);

Example:

You can also add header information to mail.

mail ( $receiver_email , $subject , $message, $header_data);

Example:

Send Mail using PHPMailer

First you need to download phpmailer library from https://github.com/Synchro/PHPMailer

To use phpmailer, First you need to include class.phpmailer.php file to your PHP file and then create PHPMailer class object, and set some required values (sender email, receiver email, subject, message etc.) and that’s it.

Example:

Attach Document (Attach any file in mail)

If you want to attach any document or file in email you just need to add

HTML Formatted Mail

If you want to send mail in HTML format you need to set IsHTML to true

Add BCC/CC

That’s It, Any Question?

Ajax PHP Login with jQuery Shake effect