473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,406 developers and data experts.

How to Send an email using PHP for Beginners

AutumnsDecay
170 100+
Perhaps you've wanted to create a 'Request a Quote' or a 'Contact Us' form for a client you have, but didn't know how to get the information from the user to your client.

Using PHP's mail() function, we take the user's inputted data and send it to our client's email address with minimal effort.

Let's assume we have a contact form with the following markup:

Expand|Select|Wrap|Line Numbers
  1. <form action="send.php" method="post" name="contactForm">
  2.  
  3.     Name: <input type="text" name="name" />  <br />
  4.     Phone Number: <input type="text" name="phone" />  <br />
  5.     Email Address: <input type="text" name="email" />  <br />
  6.  
  7.     <input type="submit" value="Submit!" />
  8.  
  9. </form>
  10.  
The 'action' attribute in the <form> element is what will take place when the submit button is pressed. In this case, when the form is submitted, it will load 'send.php'.

The 'method' attribute is the way the form's data will be sent to the action page (in our case: send.php). There are two options:

1) POST - All the name value pairs are submitted in the Message Body of the request, which is not seen in the location bar of the browser. POST strings have no limitations in size.

2) GET - All name values pairs are entered as a string into the browser's location bar. GET strings are limited in size and characters available for usage when the form is submitted.

Summarizing, POST is the secure method of sending data from a form. It should be used when handling sensitive information such as passwords, creditcard numbers, email addresses, etc. GET is less secure, but is widely used in CMS designs dealing with products and categories. Typically seen in online store usage when browsing products.

The only other important thing about our form at this time is making sure all of our text fields have unique 'name' attributes. Use relevant naming conventions to keep it simple.

Now, once the form is submitted, the values the user entered into the form are sent to 'send.php' using the 'post' method. Our PHP script will have the following:

Expand|Select|Wrap|Line Numbers
  1. $name = $_POST['name'];
  2. $phone = $_POST['phone'];
  3. $email = $_POST['email'];
  4.  
  5. $subject = "New Contact Request Submission";
  6.  
  7. $message = "Name: " . $name . "\n\n" . 
  8.                      "Phone Number: " . $phone . "\n\n" .
  9.                      "Email Address: " . $email;
  10.  
  11. $to = "youremail@yourdomain.com";
  12. $headers = 'From: ' . $name . '<' . $email . '>' .  "\n" .
  13.                    'Reply-To: ' . $email . "\n" .
  14.                    'X-Mailer: PHP/' . phpversion();
  15.  
  16. mail($to, $subject, $message, $headers);
  17.  
  18. header("Location: thankyou.html"); 
The first thing we do in the 'send.php' file is create variables out of our POST'd content. I find it helps keep things a bit more organized.

The $subject variable contains the message that will appear as the subject of the email.

The $message variable contains what the body of the message will contain. In our case, it will output like this:

-----------------------------------
Name: Johnny Appleseed

Phone Number: 555-555-5555

Email Address: someone@somewhere.com
-----------------------------------

Each \n is a line break, similar to HTML's <br /> tag. YES, the '\n' IS SUPPOSED to go INSIDE the string, not outside of it, which is a common novice mistake with PHP scripters.

The $to variable contains the email address that the email will be sent to.

The $headers variable contains information about how the email was sent, what the Reply To email address is, etc.

Now, we see PHP performing the mail() function. It takes our data and sends it off to the recipient.

Finally, once the script has executed, we redirect our users to a landing page. In this example the will be taken to 'thankyou.html'.

And that's that. I hope this helps some of you gain a better grasp as to some of the fairly basic features of how a static website can gain some dynamic features

Please note that this is a basic tutorial. If you're using this in a large scale environment, or want to practice safety with your scripts, you WILL want to validate all your form's information before it is sent. You can also adjust the send.php script to require a CAPTCHA code to be entered, etc, in order to stop spam.

Regards,
Mathew Cartmill
Dec 27 '10 #1
0 4590

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: aish | last post by:
I want to send email using sql stored procedure.my code is work fine in my local sqlserver account. when I use my online sql server it display this error. EXECUTE permission denied on object...
8
by: Nick Li | last post by:
Hi, I am trying to writing a c program to send email using system() function call on Unix(Sun Solaris). I tried the following: #include <stdio.h> #include <stdio.h> int main(void) {
11
by: Mohammed Abdel-Razzak | last post by:
dear sirs i want to send email using c# but i don`t want to use CDONTS or tell me how can i use it i don`t know any thing about CDONTS thanks
9
by: Bob Jones | last post by:
We have developed a commercial ASP.net application (personal nutrition management and tracking); we want to send smtp email from within it. For our development box, we use WinXP Pro, IIS 5.5,...
2
by: JohnB | last post by:
hi I am using System.Web.Mail.MailMessage to send email. Is there any way to keep the email to "Sent Items" Folder?? I use Microsoft Outlook as my email tool. Thanks
0
by: padmanabhanp | last post by:
Hai, How to send email using jsp
3
by: =?Utf-8?B?SHVnaA==?= | last post by:
Hi There, I use follow code to send email inside VB.NET 2005. It does not work well. Error message of "Failure sending email" would occue. However, email was sent out sometimes. I am confused...
2
usafshah
by: usafshah | last post by:
How can i send email using php through SMTP server ?
0
by: thinkwebs | last post by:
hi guys Just i am trying to send email using syste.web.mail in ASP.Net just i have send mail successfully in the web but i can't send mail with attachment in the web. But my coding works properly...
14
by: Warren Tang | last post by:
Hi I am using the mail function to send a mail like this: $b = mail("my_real_email_address@gmail.com", "Hello from PHP", "Hi, finally sent an email successfully"); But it failed. Could you...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.