User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 426,336 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,430 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting

PHP Sendmail Tutorial

Join Date: Jul 2005
Posts: 19
Reputation: kiwimedia is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
kiwimedia kiwimedia is offline Offline
Newbie Poster

Tutorial PHP Sendmail Tutorial

  #1  
Jan 27th, 2006
Sending email through PHP

Intro

Thousands of websites use contact forms to communicate with their users. You will have almost certainly seen one if not used one to contact someone. The contact form will take the users information that he or she has filled in then send the data over to our php script for processing. In our case the data will be sent over to us in an email.


Creating the HTML Feedback Form

Below is the HTML code to create our feedback form.

  1. <form method="post" action="sendmail.php">
  2. Email: < input name="email" type="text" />
  3. Message:
  4. <textarea name="message" rows="10" cols="30">
  5. </textarea>
  6. <input type="submit" />
  7. </form>

The form will take the email address and message from the user and sends the information to sendmail.php via the form, action="sendmail.php".

The php script will know which piece of information being sent by the input name="" tag. So we know the email will be typed in the email box by the name tag: name="email"

The PHP code to sendmail

Now we have set the form to POST the data to sendmail.php we need to create a new file named “sendmail.php". Once you have created the file you can enter the following code into your empty php file. Don’t worry I’ll explain each line in a moment.

  1. <?
  2. $email = $_GET['email'] ;
  3. $message = $_GET['message'] ;
  4. mail( "yourname@example.com", "Email Subject", $message, "From: $email" );
  5. print "Congratulations your email has been sent";
  6. ?>

Okay this script will now send email out using php’s sendmail function yeh! Now I’ve got some explaining to do.

Line 2 - 3: These get the data from the form keeping in mind the email form box is named name="email" and the $_GET variable is also called email.

Line 4: This is the clever part that sends the email, The mail() function allows to… specify the email address of the recipient, place the the subject of the email which will appear in the subject line, puts the message of the email which appears in the emails main body and the function allows you to specify the senders email so the recipient can then send a reply if required.

The sendmail function is no way limited to this configuration but as a beginner tutorial this is the minimum information you need to make mail() work.

For more information on the sendmail function then please goto phps sendmail function page.

line 5 - Will display “Congratulations your email has been sent"

I hope the tutorial helps any comments or question as always can be post below.
Last edited by cscgal : Oct 29th, 2006 at 1:23 am. Reason: Added code tags
AddThis Social Bookmark Button
Reply With Quote  
All times are GMT -4. The time now is 11:34 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC