Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Email Script/form With Php


37 replies to this topic

#1 snlildude87

    Moderator

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 2,325 posts
  • Location:Mawson, Antarctica

Posted 19 April 2005 - 02:27 AM

Today, I'm going to show you how to make an email script using PHP and HTML. Most people usually put <a href="mailto:blahblah@blah.blah">Email me</a> if they want an email link on their site, but there are several cons to this.

First, it's very inconvenient for those people who use web-based mail such as Yahoo!, Hotmail, or Gmail because that darn Micro$oft Outlook program comes up if one accidently click the link.

Second, you are very suseptible to spam bots because they scan your HTML source code for anything with the same pattern as an email address and add it to their database. If you are pretty popular on Google, you can expect mails like "Unlimited Bandw1dth!!1" or "Cows F0und 1n 0utersp4ace!!" in your inbox soon.

The Tutorial

Make a new file called mail.php in Notepad and copy and paste the following code:
<html>
 <head>
  <title>My first email script</title>
 </head>
 <body>
  <?php
  if($content)
  {
  	$from = $_POST["from"];  //gets the name of the person
  	$email = $_POST["email"];  //gets their email address
  	$content = "This message is from $from whose email address is $email.\r\n-----------\r\n ";  //little intro of your email message that you will see in your inbox
  	$content = $content . $_POST["content"]; //concatenates the intro with the real content
  	$content = wordwrap($content, 70); //message must be no longer than 70 characters per line (PHP rule), so we wrap it
  	mail('snlildude87@gmail.com', 'Someone Sent You Something!!!', $content); //first argument = your email address; second argument = subject of email (make it very catchy so you won't miss it); third argument = the content (DO NOT CHANGE THIS!!)
  	echo "Your message has been sent, and if I like you, then you will receive a reply. Thank you."; //what you want to tell the user after sending
  }
  ?>
  <form method="post" action="mail.php">
  	<p>Name: <input type="text" name="from" title="Your name"></p>
  	<p>Email: <input type="text" name="email"></p>
  	<p>Message: <textarea rows="5" cols="15" name="content"></textarea></p>
  	<input type="submit" value="Submit">
  </form>
  </body>
</html>

That's it! If you want to see the email script/form in action, I have set one up on my site here: http://sang.trap17.com/aboutme.htm

Also, when someone sends you something with my script, you have to manually type in their email address. So after someone sends you a message, you will get the following in your email:

Quote

This message is from [the person's name] whose email address is [their email address].
-----------
[content of message]
You will have to copy whatever their email address is, and go to Compose in your mail provider to send them a reply. It's a hassle, but I promise, it won't take more than 1 minute. :o

If you have any questions, concerns, comments, or ideas, feel free to post them below. Thanks and good luck!! :D

#2 scab_dog

    V-Man

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 372 posts
  • Gender:Male
  • Interests:There is lots of stuff, but there isnt any space to fit em all.
  • myCENT:15.01

Posted 19 April 2005 - 04:06 AM

This is great, YDA snill, I was looking for one like this for ages!!

#3 leiaah

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 436 posts
  • Location:Koronadal City, Philippines
  • Interests:music, web stuff, movies, pop culture

Posted 19 April 2005 - 05:47 AM

I've been looking for this! There's one in the trap17 cpanel called mailform/formail but I can't follow the instructions so thanks!

#4 Spectre

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 873 posts

Posted 19 April 2005 - 06:54 AM

Just for the record, you can include extra headers as the fourth argument. One that you might want to take particular note of is the 'From:' header - if it is not specified, the mail will be sent from the server's default email address.

In this case, the best choice would probably be to use the values entered as the users' name and email. For example:
mail(
  'snlildude87@gmail.com',
  'Someone Sent You Something!!!',
  $content,
  "From: $from <$email>"
);

Edited by Spectre, 19 April 2005 - 07:00 AM.


#5 boyCradle

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 469 posts
  • Location:Manila, Philippines

Posted 19 April 2005 - 09:49 AM

wow! this is nice, i always have problems with that mailto: codes at html. this will be helpful for those internet users not using Microsoft outlook. it is very anoying when the Outlook window comes out everytime i click the mailto links at websites. thanks

#6 hype

    Legend Killer

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 723 posts
  • Gender:Male
  • Location:Singapore
  • myCENT:40.70

Posted 19 April 2005 - 10:04 AM

Wow, we can place it at any part of the website as long as its with the <? php ?> tag right?

#7 snlildude87

    Moderator

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 2,325 posts
  • Location:Mawson, Antarctica

Posted 19 April 2005 - 11:15 AM

hype, on Apr 19 2005, 07:04 AM, said:

Wow, we can place it at any part of the website as long as its with the <? php ?> tag right?

View Post

Well, yes. Only this part is the PHP code (this is the part you put in between the <?php ?>:
 if($content)
 {
  $from = $_POST["from"];  //gets the name of the person
  $email = $_POST["email"];  //gets their email address
  $content = "This message is from $from whose email address is $email.\r\n-----------\r\n ";  //little intro of your email message that you will see in your inbox
  $content = $content . $_POST["content"]; //concatenates the intro with the real content
  $content = wordwrap($content, 70); //message must be no longer than 70 characters per line (PHP rule), so we wrap it
  mail('snlildude87@gmail.com', 'Someone Sent You Something!!!', $content); //first argument = your email address; second argument = subject of email (make it very catchy so you won't miss it); third argument = the content (DO NOT CHANGE THIS!!)
  echo "Your message has been sent, and if I like you, then you will receive a reply. Thank you."; //what you want to tell the user after sending
 }
 ?>
The rest is all HTML. You can, of course, always convert HTML to PHP...

I hope that answered your question.

#8 Carsten

    Member [Level 1]

  • Kontributors
  • PipPipPipPip
  • 51 posts

Posted 19 April 2005 - 12:00 PM

Unfortunately you might need to include more headers other then the From header if you want to send e-mail to big free e-mail providers like Hotmail. The mail() function has the syntax of mail($to, $subject, $content, $headers). To add some more required headers, just add a comma after $content and add a variable $headers. Then, fill the variable $headers with something like this:
$headers = "MIME-Version: 1.0\n"; // don't change
$headers .= "Content-type: text/html; charset=iso-8859-1\n"; // don't change
$headers .= "X-Priority: 1\n"; // don't change
$headers .= "X-MSMail-Priority: High\n"; // don't change
$headers .= "X-Mailer: php\n"; // change php in what you like
$headers .= "From: \"" . $from . "\" <" . $email . ">\n";
$headers .= "Reply-To: \"". $from ."\" <". $email .">\n";
Now you don't even have to copy/paste the users e-mail adress, you can directly click reply and send them an e-mail.

To make it clear, replace
 mail('snlildude87@gmail.com', 'Someone Sent You Something!!!', $content);
with
 mail('snlildude87@gmail.com', 'Someone Sent You Something!!!', $content, $headers);
and put all the $headers in front of that.

#9 Newton

    Newbie [Level 1]

  • Kontributors
  • Pip
  • 11 posts

Posted 19 April 2005 - 07:34 PM

I'd just like to add that mail headers should use \r\n line breaks, as per RFC 2822 section 2.1.

Also if you want something more 'complicated', use PHPMailer.

#10 hype

    Legend Killer

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 723 posts
  • Gender:Male
  • Location:Singapore
  • myCENT:40.70

Posted 20 April 2005 - 07:35 AM

I believe you have to replace this as well
mail('snlildude87@gmail.com', 'Someone Sent You Something!!!', $content);

to

mail('youremail@yourdomain.com', 'Someone Sent You Something!!!', $content);


#11 snlildude87

    Moderator

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 2,325 posts
  • Location:Mawson, Antarctica

Posted 23 April 2005 - 12:06 AM

hype, on Apr 20 2005, 04:35 AM, said:

I believe you have to replace this as well
mail('snlildude87@gmail.com', 'Someone Sent You Something!!!', $content);

to

mail('youremail@yourdomain.com', 'Someone Sent You Something!!!', $content);

View Post

Right, I stated that in my first post:

snlildude87, on Apr 18 2005, 11:27 PM, said:

 ...
  	mail('snlildude87@gmail.com', 'Someone Sent You Something!!!', $content); //first argument = your email address; second argument = subject of email (make it very catchy so you won't miss it); third argument = the content (DO NOT CHANGE THIS!!)
...

View Post


Thanks for noticing. :lol:

#12 Amezis

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 535 posts
  • Location:Oslo, Norway

Posted 12 May 2005 - 02:41 PM

Most usefull tutorial I have seen the web :P

#13 snlildude87

    Moderator

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 2,325 posts
  • Location:Mawson, Antarctica

Posted 12 June 2005 - 03:11 AM

Carsten, on Apr 19 2005, 09:00 AM, said:

Unfortunately you might need to include more headers other then the From header if you want to send e-mail to big free e-mail providers like Hotmail. The mail() function has the syntax of mail($to, $subject, $content, $headers). To add some more required headers, just add a comma after $content and add a variable $headers. Then, fill the variable $headers with something like this:
$headers = "MIME-Version: 1.0\n"; // don't change
$headers .= "Content-type: text/html; charset=iso-8859-1\n"; // don't change
$headers .= "X-Priority: 1\n"; // don't change
$headers .= "X-MSMail-Priority: High\n"; // don't change
$headers .= "X-Mailer: php\n"; // change php in what you like
$headers .= "From: \"" . $from . "\" <" . $email . ">\n";
$headers .= "Reply-To: \"". $from ."\" <". $email .">\n";
Now you don't even have to copy/paste the users e-mail adress, you can directly click reply and send them an e-mail.

To make it clear, replace
 mail('snlildude87@gmail.com', 'Someone Sent You Something!!!', $content);
with
 mail('snlildude87@gmail.com', 'Someone Sent You Something!!!', $content, $headers);
and put all the $headers in front of that.

View Post

Woohoo! It works now! I don't have to copy and paste their email address anymore! Thanks man. :D


Amezis, on May 12 2005, 11:41 AM, said:

Most usefull tutorial I have seen the web :(

View Post

Hey, no problem. Let me know if you used it on my site. I'm curios to see if/what changes you made to it. :D

#14 badinfluence

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 283 posts
  • Interests:Music,Game,Software

Posted 13 June 2005 - 04:42 PM

hi folks,
thks for the tutorial.. base on this.. i scripted my contact page successfully. here is a working script in action..

i just modified to limit the valid e-mail mandatory* for contact/query to webmaster and auto redirect upon process.. and i splited to form query page for contact.php and mailto for processing the query. i will add autorespond upon successful mail delivery back to visitor and phpBB forum registered users may not need to supplied e-mail(just registered username can allow to query)..

pls test it out and if you want code, pls post here

thks for all, folks

:P

#15 FaLgoR

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 217 posts

Posted 13 June 2005 - 04:48 PM

Quote

Most usefull tutorial I have seen the web

The forum is loaded with these kind of script.... really loaded o.O

#16 snlildude87

    Moderator

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 2,325 posts
  • Location:Mawson, Antarctica

Posted 13 June 2005 - 05:39 PM

FaLgoR, on Jun 13 2005, 01:48 PM, said:

The forum is loaded with these kind of script.... really loaded o.O

View Post

Really?? I thought this mail tutorial thing is the first on trap17. :P Can you give me 2 or 3 mail scripts that people have submitted in the past?

#17 Lozbo

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 282 posts
  • Location:Wanatos
  • Interests:Multimedia Arts and Technologies =)

Posted 23 November 2005 - 01:35 AM

Newton, on Apr 19 2005, 01:34 PM, said:

I'd just like to add that mail headers should use \r\n line breaks, as per RFC 2822 section 2.1.

Also if you want something more 'complicated', use PHPMailer.

View Post

So instead of
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n"; 

We should use
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";  

???

And what php mailer?

Notice from jlhaslip:
edit to include code tags

Edited by jlhaslip, 23 November 2005 - 01:49 AM.


#18 puffy

    Member [Level 1]

  • Kontributors
  • PipPipPipPip
  • 63 posts

Posted 11 February 2006 - 10:14 AM

Wow man great thx for giving me this script i was really searching for this script from so many days. I had searched google, yahoo etc... but none of them worked. But i found one in google but that script used to send the mail from contact form after few hours or so...... Those scripts lack, but urs is good script man. Please kepp posting the scripts for many programs and also i want the script for posting a google adsense banner on my forums pages, if you know the script please kindly let me know about this to me as soon as possible.

#19 technocrat

    Newbie [Level 2]

  • Kontributors
  • PipPip
  • 26 posts

Posted 11 February 2006 - 12:28 PM

so , does taht mean the HEADERS of the main() function are OPTIONAL ?
also , can i have the details of the headers please ?

#20 antony2kx

    Newbie [Level 2]

  • Kontributors
  • PipPip
  • 35 posts

Posted 17 February 2006 - 05:03 PM

does any one know of a good php wap forum that i can use. because i need one badly, please if any one know of any script like this please reply.

#21 reekingofrandomness

    Newbie

  • Kontributors
  • Pip
  • 1 posts

Posted 07 March 2006 - 07:15 PM

With the PHP Reply-TO: header...is there anyway you can hide it...so that the mail recipient cant see it...

#22 Dawiss

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 286 posts
  • Location:Latvia, Riga
  • Interests:Im back to Trap17 for new ideas and designs ;)..

Posted 08 March 2006 - 04:08 PM

Emm this Email script is very useful if you are making website for lots of people that will visit it.. like you can make with it and Feedback form that will send you the visitor ideas..

I ussaly put it too on my websites so it have easyer way to visitors contact me..

Nice tutorial hope to see some more things from you.. :(

#23 GodZillaMaN

    Newbie

  • Kontributors
  • Pip
  • 7 posts

Posted 10 March 2006 - 09:45 PM

Thank you a lot.
Will be helpful when I get my site.

#24 Kreative Net.: Deep Innovations

    Newbie [Level 1]

  • Kontributors
  • Pip
  • 10 posts

Posted 11 March 2006 - 06:18 AM

Hi,
I want to know whether we can use such scripts on webhosts without php support. Also I want to know whether we can edit these php scripts and how to edit them?
Sunny Kumar Gupta
Founder,
The KreativeNetworks

#25 Albus Dumbledore

    Nothing ventured, nothing gianed

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 1,563 posts
  • Gender:Male
  • Location:Sacramento California
  • Interests:Allot of things, websites, coding, photography, my future
  • myCENT:73.26

Posted 11 March 2006 - 06:35 AM

Quote

hey, just a little member to member tip Kreative Net.: Deep Innovations, you shouldn't be typing Sunny Kumar Gupta
Founder,
The KreativeNetworks at the end of all of your posts we have a signature option for this Click Here for a tutorial on how to set-up the signature

And no you cannot use this script on a non PHP supporting site, because in order for this to work the page extention would need to be .php, therefor the host must support php and have mail enabled for it to work!

as for editing it, you would need to do/learn that yourself because it is a bit of a big process..lol but what do you mean by edit...maybe i can help you with it!

Edited by Albus Dumbledore, 11 March 2006 - 06:46 AM.





Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users