How to send mail using PHP

AuthorHariom Prajapati

Pubish Date26 Jun 2022

categoryPHP

In this article we will learn how to send email using PHP and MySQL from localhost.

Now follow all the below steps to send email in PHP.

Step 1- Open Xampp Directory.

Step 2- Go to C:\xampp\php and open the php.ini file.

Step 3- Press ctrl+f  and find mail function.

Step 4-  Change the following details like below code .

SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = hpxyz123@gmail.com
sendmail_path = "\"C:\xampp\sendmail\
sendmail.exe\" -t"

Step 5- Go to C:\xampp\sendmail .

Step 6- Open sendmail.ini

Step 7- Press ctrl+f  and find sendmail.

Step 8-  Change the following detail like below code

smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=auto
error_logfile=error.log
debug_logfile=debug.log
auth_username= hpxyz123@gmail.com
auth_password= hpxyz@123
force_sender= hpxyz123@gmail.com

 

Step 9- Create new file sendmail.php in C:\xampp\htdocs.

Step 10- Simply paste this code in sendmail.php and change the value according to you.

<?php
$to_email = "edu@teknowize.com";  //Reciepient mail 
$subject = "Test mail";
$body = "hello, this is  test mail ";
$from_mail = "hpxyz123@gmail.com";  // your mail  

if (mail($to_email, $subject, $body, $from_mail)) {
    echo "Email successfully sent to $to_email...";
} else {
    echo "Email sending failed...";
}

?> 

 

Step 11- Go to myaccount.google.com/ security then On the less secure app access setting. If this  is on then skip this step.

 

Step 12 - Now, Run send_mail.php file in your browser.

 

Result–

Comments 0

Leave a comment