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);





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