943,967 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 166406
  • PHP RSS
Jan 27th, 2006
0

PHP Sendmail Tutorial

Expand Post »
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.

html Syntax (Toggle Plain Text)
  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.

php Syntax (Toggle Plain Text)
  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 2:23 am. Reason: Added code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kiwimedia is offline Offline
19 posts
since Jul 2005
Jul 17th, 2006
0

Re: PHP Sendmail Tutorial

Why does the page you have calling the PHP set up to use POST variables (e.g., form method=post) and the PHP script set up to use GET variables (e.g., $_GET[])? This should not work properly.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AaronD12 is offline Offline
1 posts
since Jul 2006
Oct 30th, 2006
0

Re: PHP Sendmail Tutorial

I tried this out....and after hitting submit...the sendmail.php page comes up with the code in it? The code for posting was supposed to go into the sendmail.php page right?
Thanks for your help! Greatly appreciate it
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jessicareese is offline Offline
1 posts
since Oct 2006
Feb 17th, 2007
0

Re: PHP Sendmail Tutorial

i followed this tutorial and ran into an error with the stated sendmail.php code.. when i send information through the feedback form although i recieved an email and the address of my choice i didnt recieve any email content apart from the subject.

after some research i found out you need to use $_post instead of $_get

here is my code for sendmail.php

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $email = $_POST['email'] ;
  3. $message = $_POST['message'] ;
  4.  
  5. mail ("<a href="mailto:imperial@sublime.maantok-ent.com">imperial@sublime.maantok-ent.com</a>", "Feedback form results", $message, "From: $email") ;
  6. print "Congratulations your email has been sent";
  7. ?>

Imperial
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Imppy is offline Offline
1 posts
since Feb 2007
Dec 8th, 2009
0

Two questions:

Reply:

Two Questions:
1. Your html form says POST
Your php file says GET.
Contradiction?

2. Can I "borrow" the rest of the PHP file and if so where can i see it?
Thanks!
richard@ims-stlouis.com



Click to Expand / Collapse  Quote originally posted by kiwimedia ...
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.

html Syntax (Toggle Plain Text)
  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.

php Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
richardgavatin is offline Offline
1 posts
since Dec 2009
Sep 17th, 2011
0
Re: PHP Sendmail Tutorial
~Removed after reading forum rules~
Last edited by KingGold171; Sep 17th, 2011 at 10:02 am. Reason: Removed after reading rules
Reputation Points: 10
Solved Threads: 1
Newbie Poster
KingGold171 is offline Offline
10 posts
since Sep 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: what does this simple regular expression mean?
Next Thread in PHP Forum Timeline: login script returns blank page





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC