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
from <your email>
user <your email>
password <your email account password>
Step2: Editing the php.ini
In php.ini search for sendmail, and update “sendmail_from”
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="email@example.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”.