Jump to content
xisto Community
snlildude87

Email Script/form With Php how to make a simple email script using php

Recommended Posts

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://forums.xisto.com/no_longer_exists/

 

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:

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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 (see edit history)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

131413[/snapback]

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

131780[/snapback]

Right, I stated that in my first post:

 ...  	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!!)...

131276[/snapback]

Thanks for noticing. :lol:

Share this post


Link to post
Share on other sites

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.

131429[/snapback]

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

 

 

Most usefull tutorial I have seen the web :(

140635[/snapback]

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.