PHP Send Emails

Here you will learn about how you can easily send email using PHP. You can also use PHP to send bulk email at once.

What to do Before Sending Email using PHP

Before sending any email using PHP, firstly you must have to setup your environment so that you can send email using PHP without any distruption.

Let's see how to setup environment for XAMPP users to send email from localhost using PHP.

PHP XAMPP Setup to Send Email

Using PHP, you are free to send email from your system with the help of XAMPP.

But before sending email using XAMPP from your computer system, you have to open the php.ini and sendmail.ini file and make some changes.

You can find php.ini file in the directory C:\xampp\php\ and sendmail.ini file in the directory C:\xampp\sendmail\.

Let's first edit the php.ini file as per the instruction given below.

Search for SMTP, smtp_port, sendmail_from, and sendmail_path, and then initialize, 587,, and "\"C:\xampp\sendmail\sendmail.exe\" -t" to these as shown in the following code:
sendmail_path="\"C:\xampp\sendmail\sendmail.exe\" -t"

Now let's edit the file named sendmail.ini as per the instruction given below:

This time search for smtp_server, smtp_port, error_logfile, debug_logfile, auth_username, auth_password, and force_sender, and then initialize, 587, error.log, debug.log,, mypassword, and to these as shown in the following sample code:

You only have to replace with your own email id (this email id will be used to send the emails to anyone) and mypassword with your own password.

After doing all the settings that are provided above, you have to restart your XAMPP server then you can go further to write your email sending code using PHP to start sending email using the code. Let's see how to do this.

Note - If you will find some error after successfully setup according to the above instructions then you can easily find and fix the error using the file present in the directory C:\xampp\sendmail\. In this directory search for the file named error.txt and open that file to find and fix any of your error.

PHP Send Email Example

Let's take an example to understand how to write code to send emails using PHP.

   // below variable contains the email address of the person
   // who will get the email using this example of sending email
   $sendEmailTo = "";
   // below variable contains the subject value
   $sendEmailSubject = "Hello partner, greeting email for you";
   // now put the message in a php variable
   $sendEmailMessage = "How are you my partner";
   <title>Sending Emails using PHP Example -</title>
   // now set the content-type before sending html email
   $headers = "MIME-Version: 1.0 \r\n";
   $headers = $headers . "Content-type:text/html;charset=UTF-8 \r\n";
   $headers = $headers . " \r\n";
   $checkMail = mail($sendEmailTo, $sendEmailSubject, $sendEmailMessage, $headers);
   if($checkMail == true)
      echo "Your email sent successfully to your friend.";
      echo "Error occurred while sending the email, try again";
      echo "<br/>exiting...";

Here is the sample output produced by the above email sending example using PHP after sending the email successfully.

php send email

And here is the output you will see if some error occurred while sending the email using the above email sending example:

sending email using php example

PHP Online Test

« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook

Subscribe Us on YouTube