how to send email using php – Using PHPmailer

Many PHP programmers facing this issue and hitting same wall. Here in article we shown, how to send email using php. we given code sample with phpmailer.

how to send email using php

how to send email using php - PHP tutorial

how to send email using php – PHP tutorial

PHP itself provides the mail() function to send emails. In this article I will show you the options of sending email using PHP language.

If you are using linux or windows hosting service for running the PHP application. PHP mail() function uses the sendmail mail server to send emails.
If sendmail mail server is configured and running on linux server then only mail() function is able to send the emails.

First we will talk about mail() function. You can pass the following parameters to mail function.


Sample code as follows:

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
$to = ",";
$subject = "Test mail";
$message = "This is a simple email message.
 this is seond line.";
$from = ",";

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $from";

$headers .= 'Cc:' . "\r\n";
$headers .= 'Bcc:' . "\r\n";

echo "Mail Sent.";

If sendmail mail server is not configured on your server, mostly with windows server then there is issue.
But you can send email using anothere PHP programm called PHPmailer.
you can download the PHPmailer from following URL:

Download first PHPmailer and extract the phpmailer folder and in that folder you will find the following three files.
1. class.phpmailer.php
2. class.pop3.php
3. class.smtp.php

copy and paste the following files to your PHP application. and use the following code in case of use POP email

function sendmail_wordpressapi($to,$from,$subject,$body,$id){
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail             = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = ""; // SMTP server
$mail->From       = $from;
$mail->FromName   = $from;
$mail->Subject    = $subject;
$mail->Body       = $body;
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap   = 50; // set word wrap
$mail->AddAddress($to, $to);

if(!$mail->Send()) {
$mail             = new PHPMailer();
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "";  // GMAIL username
$mail->Password   = "YOURPASSWORD";            // GMAIL password
$mail->From       = "";
$mail->FromName   = "SMTP EMAIL-wordpressapi";
$mail->Subject    = "email server is having some issue";
$mail->Body       = "Hi,<br>email server is having some issue,<br> check this out";                      //HTML Body
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap   = 50; // set word wrap
$mail->AddAddress("", "support-wordpressapi");
$mail->IsHTML(true); // send as HTML

} else {
 echo "Message sent!";

I created above function in this function I am using SMTP mail function for sending email but in case mail is not going using SMTP details.
you can also send emails using your gmail account details. you can edit or use this function as your requirement.

You can pass following parameters to sendmail_wordpressapi function:


You can send email to multiple email addresses. I am sure using above code sending emails through PHP is very easy. If you have any quries..write to me.

Add a Comment

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