php mail using sendmail (using gmail)

Sending emails using php is very simple. In this tutorial we use gmail account to send mails. Configuration will be similar with other email providers too, little changes in hostname and ports.

If you are using xampp package then sendmail comes by default. If you are having individual installation, you have to install sendmail and the configuration will be same.

Step1: Editing the sendmail.ini

You can find the sendmail config file(for xampp) in xampp/sendmail/sendmail.ini. Give the below settings in the config file replacing your email username and password.

#specifying the default account.

account default: gmail

account gmail
tls on
port 587
tls_certcheck off
host smtp.gmail.com
from <your email>
auth on
user <your email>
password <your email account password>

Step2: Editing the php.ini

In php.ini search for sendmail, and update “sendmail_from”

sendmail_from=<email address>

it should match with your sendmail.ini email settings.

Step3: Restart the server

Step4: write php code to send a test mail


<?php
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$to="abc@yahoo.com";

$subj="test mail";

$body=" this is a test mail";

if(mail($to,$subj,$body,$headers))
echo "success";
else
echo "failure";
?>

Step5: Before opening this file in your browser, give a valid recipient email address. Now open the file in your browser. If mail sent successfully it displays “success”, otherwise it displays “failure”.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s