if you guyz are searching for the tutorial that shows how you can send email using PHP Script or validating email address in PHP, then you are in the right tutorial. So let’s start.

“This tutorial may be very useful for those who want to integrate email sending features in their project which is developed in PHP”.

For sending email using PHP: Mainly, you have to do two task:

  1. Downloading and Configurating Mail Routing Software
  2. Writing PHP Script to send an email

You need to download this software having the name sendmail click here and copy the file inside your localhost root folder in my case c://wamp64/sendmail and then open that sendmail and find sendmail.ini file and open this file with notepad and  then scroll down and find following term:

auth_username=
auth_password=
force_sender=
hostname=
smtp_server=
smtp_port=

Write your gmail in the place of auth_username, password to auth_password, again your gmail in force_sender, and localhost in hostname same as following:

auth_username=example@example.com //your gmail account
auth_password= ******* //your email password
force_sender=  example@example.com   //your gmail account
hostname=localhost
smtp_server=smtp.gmail.com
smtp_port=587

After completing this save that file.

Again open php.ini file from your server application. in case of wampserver, click the green icon of wamp server from system tray, hover over PHP and then choose php.ini file, open that file in notepad and find mail function. i.e. [mail function] inside this [mail function] find the following parameters:

SMTP=
smtp_port=
sendmail_from =
sendmail_path =

set the following value to them:

SMTP= smtp.gmail.com
smtp_port=587
sendmail_from =example@example.com // your gmail account
sendmail_path =”C:\wamp64\sendmail\sendmail.exe -t”

and save this php.ini file for more details you need to watch this video: CLICK HERE

Now write the following piece of code in any text editor software, i am using Dreamweaver CC and saved this php file in .php extension in your localhost and run (Note: your php file will not execute in the same way you run your html file, because PHP is server side scripting language so you need to have any server application to run this file.  watch this video to configure site and testing server in Dreamweaver CC: https://youtu.be/L4ex3S8BVCo or read here: click here)


<!doctype html>
<?php
if(isset($_POST['submit'])){
$to = $_POST['temail'];
if(!filter_var($to, FILTER_VALIDATE_EMAIL)){
echo("enter the correct email");}
else{
$from = $_POST['femail'];
if(!filter_var($from, FILTER_VALIDATE_EMAIL)){
echo("enter the correct email");}
else {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$message = $fname."".$lname."\n".$_POST['message'];
$message = wordwrap($message, 70);
$headers = "From:" . $from;
$subject = "form submission";
mail($to , $subject, $message, $headers);
echo("mail send");
}
}
}
?>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<form action="email.php" method="post">
<table align="center">
<tr>
<td>Name:</td>
<td><input type="text" name="fname" placeholder="first name">
<input type="text" name="lname" placeholder="last name"></td>
</tr>
<tr><td>To:</td>
<td><input type ="email" name="temail" placeholder="example@example.com"></td>
</tr><br><br>
<tr>
<td>From:</td>
<td><input type="email" name="femail" placeholder="example@example.com"></td></tr>
<tr>
<td>Message:</td>
<td><textarea cols="40" rows="5" placeholder="Enter your useful message here" name="message"></textarea></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="send"></td>
</tr>
</table>
</form>
</body>
</html>

SHARE