Be a Supporter!

Php: Basic Mail Forms

  • 1,109 Views
  • 3 Replies
New Topic Respond to this Topic
Janj
Janj
  • Member since: Nov. 10, 2009
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Php: Basic Mail Forms 2011-06-20 13:19:42 Reply

PHP:Main
Here, I'm going to show you how to create a basic PHP mail form.
The mail function
First, you need to know about mail();
mail() sends anything to a specified email address. With mail, everything is specified within the parenthesis.
First comes 'to.' This is, in case you didn't notice, who you're sending the email to. Then comes the 'subject' and the 'message.' Those three are required. Here's an example:

<?php
mail("contact@ssjan.com","subject name","A user has visited this webpage");
echo "email has been sent.  Thank you for visiting this webpage";
?>

That would work, right? But it's really disorganized. Also, the sender is 'me@127.0.0.1'
If I remember correctly, 127.0.0.1 is that address on every computer for your localhost.

A better code would be this:

<?php
$to = "contact@ssjan.com";
$subject = "subject name";
$message = "A user has visited this webpage";
mail($to,$subject,$message);
echo "email has been sent. Thank you for visiting this webpage";
?>

And the output would be the exact same!
Now, if you want to know about the two optional mail parameters, here they are:
Headers
You can use this to do stuff like "From," "CC," and "BCC."
I'm going to show "From." From would replace the me@127.0.0.1 address to whatever you want.
add this to your code:
$headers = "From: spam@ssjan.com"
mail($to,$subject,$message,$headers);
Now, you've done a header!
Parameters
Parameters are things you can send to the mail program. I don't really know about parameters, so I'm not gonna explain it. Google is your friend, though

The Form
First, you need two webpages. At least one of them has to be PHP.
Now, you have to create the form webpage. Let's call this form.html. In form.html, you need to have a textbox for the subject, the message, and the header. The to should be defined by you.
The code for the form:
form.html

<form action="mail.php" method="post" name="mailform">
  <p>
    <input type="text" name="subject" id="subject" />
  </p>
  <p>
    <input type="text" name="from" id="from" />
</p>
  <p>
    <textarea name="message" id="message" cols="45" rows="5"></textarea>
  </p>

<input type="submit" name="submit" id="submit" value="Submit" />
</form>

The first line makes the method 'post,' because we don't want people sending the parameters through the URL. It names the form 'mailform' and makes the action 'mail.php' which means when the user clicks the submit button, they will go to mail.php. Everything else should be self explanatory if you have a basic knowledge of HTML.

The PHP page
As defined in the form, the PHP page is called mail.php
First you're gonna have to create variables and request the user input. The variables are $to,$message,$subject, and $headers.
Here's the code:

<?php
$to = "contact@ssjan.com";
$subject = $_POST['subject'];
$message = $_POST['message']
$headers = $_POST['from'];
?>

Good! Now, you've got all your variables ready!

All we have to do is add the mail(); function!
Add this before the ?> in your code:

mail($to,$subject,$message,$headers);

Congratulations! You've created your own mail form in PHP! Of course, you may want to check if everything is set. Here's the code:

<?php
if (isset($_POST['subject']) && isset($_POST['message']) && isset($_POST['from'])){
$to = "contact@ssjan.com";
$subject = $_POST['subject'];
$message = $_POST['message']
$headers = $_POST['from'];
mail($to,$subject,$message,$headers);
}else{
echo "Error:  Not everything is filled out.  Please go back. "
}
?>

Of course, you can add specific 'isset's with else if to check if a specific form is left out or if there are optionals, but these are just the basics.

I hope you learned something from this tutorial!

Janj
Janj
  • Member since: Nov. 10, 2009
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to Php: Basic Mail Forms 2011-06-20 13:37:04 Reply

Oh thanks!
That reminds me when I was looking for an HTML mail form. I was annoyed when I couldn't test PHP without a localhost XD

Oh, and I just re-tested my code for mail.php.
There's an error, here's the fix:

<title>mail.php</title>
<?php
if (isset($_POST['subject']) && isset($_POST['message']) && isset($_POST['from'])){
$to = "contact@ssjan.com";
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = $_POST['from'];
mail($to,$subject,$message,$headers);
}else{
echo "Error:  Not everything is filled out.  Please go back.";
}
?>

missing some semicolons, raar

Dragonslayerk
Dragonslayerk
  • Member since: Dec. 4, 2009
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to Php: Basic Mail Forms 2011-06-20 14:04:26 Reply

At 6/20/11 01:33 PM, Xyphon202 wrote: Looks like a decent tutorial, good job!

What I found annoying with the mail() function, is that many free hosts don't support it. I was creating an email verification for someone, and it would work fine on my paid host but on their localhost or a free host, it wouldn't.

well, my host supported it, I just had to wait for my website to "propagate throughout the internet"...


Gordon Freeman is better than Master Chief. I'm sorry, but he just is.

BBS Signature