How to Send an Email using PHP?[With Source Code]
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:
- Downloading and Configurating Mail Routing Software
- 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:
[sociallocker]
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); $hearder = "from".$from; $subject = "from submission"; mail($to, $subject, $message, $hearder); echo("email send"); } } } //} ?> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <form action="emailone.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><br><br> </tr> <tr> <td>To:</td> <td><input type="email" name ="temail" placeholder="example@example.com"</td> </tr> <tr> <td>From:</td> <td><input type="email" name ="femail" placeholder="example@example.com"</td> </tr> <tr> <td>Message</td> <td><textarea name="message" rows="5" cols="40" placeholder="enter your useful message here"></textarea></td> </tr> <tr> <td></td> <td><input type="submit" name="submit" value="Send"</td> </tr> </table> </form> </body> </html>
that’s it.
[/sociallocker]
Watch the video: