CONTACT @ Skype : rajdimdung   |   call : +977 9841813550

Basic PHP mail() Function code to send emails from a form

Post by : Raj Dimdung    Date : 2014-12-05 08:51:35

  • Share on Facebook

You can use the PHP mail() function to send an email with PHP. The simplest way to do this is to send a text email. This is one way to handle sending you the results when a visitor to your website fills out a form.

Important! : From localhost this may not send email, So set SMTP URI in php.ini or header of script file.

Basic PHP email() fuction code

Below is the code for the baic email function. We can take the script and actually use a form on our website to set the variables in the script above to send an email.

<?php//if "email" variable is filled out, send email
  if (isset($_REQUEST['email']))  {
  //Email information
  $admin_email = "";
  $email = $_REQUEST['email'];
  $subject = $_REQUEST['subject'];
  $comment = $_REQUEST['comment'];
  //send email
  mail($admin_email, "$subject", $comment, "From:" . $email);
  //Email response
  echo "Thank you for contacting us!";



<form method="post">
  Email: <input name="email" type="text" /><br />
  Subject: <input name="subject" type="text" /><br />
  Message:<br />
  <textarea name="comment" rows="15" cols="40"></textarea><br />
  <input type="submit" value="Submit" />

So let's now review what the form is actually doing.

1. The first part checks to make sure the email input field is filled out. If it is not, then it will display the HTML form on the page. If the email is in fact, set (after the visitor fills out the form), it is ready to send.

2. When the submit button is pressed, after the form is filled out, the page reloads and reads that the email input is set, so it sends the email.

Reference from :


Related Posts