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.

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 = "someone@example.com";
  $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!";
  }
  
  //if "email" variable is not filled out, display the form
  else  {
?>

 <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" />
  </form>
  
<?php
  }
?>

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.

Keep in mind, this is a basic tutorial to explain how to use the mail() function in PHP. Using the method, exactly the way it is, can be insecure and should not be used on your website. This tutorial is aiming to provide you the basic of how to use phpmail() and for further use, you may want to look into securing your code to possible hacks.

To learn more about the PHP email function, please see the article on How to create a custom PHP contact form with more information on validation and error checking. If you need further assistance please feel free to ask a question on our support center.

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve our Support Center:
Email Address
Optional, but our team may contact you for more information.
Like this Article?

Comments

Post a comment
n/a Points
2014-04-10 3:26 am

Hi , 

This code is working properly and display ed the msg as Thank you for contacting us but mail is not send to the correspomding mail address.. pls give any suggestions...

Staff
5,587 Points
2014-04-10 4:56 am
Hello Sankar,

It's possible that the message is ending up in a spam folder in the recipient's email address due to the mail coming directly from a PHP script.

If that is the case, you could use something like phpMailer in order to send an SMTP authenticated email.

Either that or there is some incorrect information typed in to your script. The Thank you for contacting us! message is displayed if the email form is submitted, but it doesn't actually check to ensure that the mail was delivered. If you have access to the server's mail logs, you should be able to see if the PHP mail attempt is making it to the local server to even attempt a delivery out.

It looks like we might need to update the example script used in this guide to at least wrap the code with if (!mail( ...)){ echo "Failure" } code to see if the server accepts the PHP mail attempt. However this is more just a proof of concept then something you'd probably want to use on a live contact form for your website.

- Jacob
n/a Points
2014-04-11 12:07 am

Hi Jacob, 

 i was alredy used the phpmailer code it was shown the error of Message was not sent.Mailer error: SMTP connect() failed.. 

Staff
5,587 Points
2014-04-11 12:20 am
Hello Sankar,

If you're getting a SMTP connect() failed error, then this means your PHP script can't establish a SMTP connection with the server you've entered into your script.

Are you possibly trying to connect to a secure mail server by chance? You might need some additional settings in your phpMailer script such as:

$mail->SMTPSecure = 'tls';
$mail->Host = "secure.example.com";


If you're still having issues let us know.

- Jacob

Post a Comment

Name:
Email Address:
Comment:
Are you a bot?
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

Write New!
Do you want to publish a tutorial to our support center?

News / Announcements

SSL Certficate Warnings
Updated 2014-04-14 11:34 am EST
Hits: 2163
Heartbleed 0-day OpenSSL security bug
Updated 2014-04-14 04:43 pm EST
Hits: 5550

Related Questions

Here are a few questions related to this article that our customers have asked:
Emails sent from my website have the server name in them
Would you like to ask a question about this page? If so, click the button below!
Ask a Question

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!