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.

1mail(to,subject,message,headers,parameters)

Sample code as follows:

01<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
02$to = "support@purabtech.in/files/, wordpressapi@gmail.com";
03$subject = "Test mail";
04$message = "This is a simple email message.
05 this is seond line.";
06$from = "some@example.com, wordpressapi@gmail.com";
07 
08// To send HTML mail, the Content-type header must be set
09$headers  = 'MIME-Version: 1.0' . "\r\n";
10$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
11$headers .= "From: $from";
12 
13$headers .= 'Cc: test@test1.com' . "\r\n";
14$headers .= 'Bcc: test2@test1.com' . "\r\n";
15 
16mail($to,$subject,$message,$headers);
17echo "Mail Sent.";
18?>

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:

http://sourceforge.net/projects/phpmailer/

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

01<?php
02function sendmail_wordpressapi($to,$from,$subject,$body,$id){
03//error_reporting(E_ALL);
04error_reporting(E_STRICT);
05date_default_timezone_set('America/Toronto');
06include_once('class.phpmailer.php');
07//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
08$mail             = new PHPMailer();
09$mail->IsSMTP(); // telling the class to use SMTP
10$mail->Host       = "purabtech.in/files/"; // SMTP server
11$mail->From       = $from;
12$mail->FromName   = $from;
13$mail->Subject    = $subject;
14$mail->Body       = $body;
15$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
16$mail->WordWrap   = 50; // set word wrap
17$mail->MsgHTML($body);
18$mail->AddAddress($to, $to);
19 
20if(!$mail->Send()) {
21$mail             = new PHPMailer();
22$mail->IsSMTP();
23$mail->SMTPAuth   = true;                  // enable SMTP authentication
24$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
25$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
26$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
27$mail->Username   = "WORDPRESSAPI@gmail.com"// GMAIL username
28$mail->Password   = "YOURPASSWORD";            // GMAIL password
29$mail->AddReplyTo("smtp.production@gmail.com","wordpressapi");
30$mail->From       = "support@purabtech.in/files/";
31$mail->FromName   = "SMTP EMAIL-wordpressapi";
32$mail->Subject    = "email server is having some issue";
33$mail->Body       = "Hi,<br>email server is having some issue,<br> check this out";                      //HTML Body
34$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
35$mail->WordWrap   = 50; // set word wrap
36$mail->MsgHTML($body);
37$mail->AddAddress("support@purabtech.in/files/", "support-wordpressapi");
38$mail->IsHTML(true); // send as HTML
39$mail->Send();
40 
41} else {
42 echo "Message sent!";
43}
44}
45?>

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:

sendmail_wordpressapi($to,$from,$subject,$body,$id);

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.