Announcement

PowerBB 2.2.1 has been released. This is an extremely important release and all users are urged to download and upgrade their forums immediately. Download it here: PowerBB 2.2.1.

#1 2006-07-22 04:24:07

Eli
Staff
Registered: 2006-02-14
Posts: 692
Reputation : give 1 reputation point take 1 reputation point  43
E-mail  Website  Expertise

Email Sender

In this tutorial, you will learn how to send emails online automatically to a user entered email.

Lets start out by just making a form in a page called "email.php":

---------------------------------FORM-----------------------------------------------

Code: html:

<form action="email.php" method="post">
  <p>
    <input type="hidden" name="formsent" value="1" />
    Email To Send to: <input type="text" name="email" value="" /><br />
    Email Subject: <input type="text" name="subject" value="" /><br />
    Your Email: <input type="text" name="replyto" value="" /><br />
    Message:<br /> <textarea cols="70" rows="10" name="message"></textarea><br />
    <input type="submit" value="Send Email" /><input type="reset" value="Reset" />
  </p>
</form>


Okay, now first thing you should notice, is that this form's action (or page to go for processing) is itself. This will make life easier in the future. And as usual, we are using post as the method because it is more secure.

Now we have the hidden field:

<input type="hidden" name="formsent" value="1" />

This helps us figure out when to process the form, and when to show the form.

Email To Send to: <input type="text" name="email" value="" /><br />

Is to give you the ability to send an email to whoever you want.

Your Email: <input type="text" name="replyto" value="" /><br />

Is there so that the user can reply to the email and really be responding to your email, not the website that sent him the email.

Message:<br /> <textarea cols="70" rows="10" name="message"></textarea><br />

For the message we are using a textarea, because it is the only html field that can give you enough space to type an email. Textareas do not have a value attribute, so you put the stuff you want to show up by default inbetween the textarea opening tag, and the textarea ending tag.

Then we provide the user with two buttons. A send button, and a form reset button:

<input type="submit" value="Send Email" /><input type="reset" value="Reset" />

-------------------------------PROCESSOR-----------------------------------------------
Okay, now we need to create the code that will parse the information recieved from the form and send the email.

Because we are using the same page to process, you need to put the php code above the form.

Here is how the processor will look:

Code: php:

<?php
if ($_POST['formsent'] == "1")
{
  $email = $_POST['email'];
  $subject = $_POST['subject'];
  $youremail = $_POST['replyto'];
  $message = $_POST['message'];
  if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
  {
    echo "The e-mail was not valid. Please click the back button on your browser and try again.";
  }
  else
  {
    if($subject == "")
    {
      echo "You must enter a email subject";
    }
    else
    {
      if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $youremail))
      {
        echo "Your e-mail was not valid. Please click the back button on your browser and try again.";
      }
      else
      {
        if ($message == "")
        {
          echo "You must enter a message";
        }
        else
        {
          
          $headers = "From: ".$youremail;
          mail($email,$subject,$message,$headers);
          echo "Your email was sent succsessfully.";
        }
      }
    }
  }
}
?>


Okay, that looks complicated, but it is really quite simple.

First we check that the form was sent by checking if $_POST['formsent'] equals 1.

If it is, then we move on to our validation.

We first set all variables equal to that which we got from the form. Here we use the global variable $_POST['']; because that is the method we used.

Then this:

Code: php:

if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
  {
    echo "The e-mail was not valid. Please click the back button on your browser and try again.";
  }


Checks if the email you entered to email to is in the correct email format, and if it is, then we continue checking other things.

The next check is if the subject is empty:

Code: php:

$subject == ""


If it is, we move on, and check if your email is in the correct format. Your email is important because that tells the reciever what email to respond to.

The last check is if the message was blank.

If all of the checks were successfull, we can send the email, otherwise we get an error message for the thing we did wrong.

Here is how we send the email:

Code: php:

$headers = "From: ".$youremail;
          mail($email,$subject,$message,$headers);
          echo "Your email was sent succsessfully.";


The first line: $headers = "From: ".$youremail; tells the reciever that it came from the email you entered as yours, which means when they reply to the email, it will send to the email you entered as yours.

Then the script uses php's built in mail function to send the email.

$email was defined at the start of the script as the email address to send to.
$subject was defined at the start of the script as the message subject.
$message was defined at the start of the script as the email main content.

$headers we defined right above to say it comes from your email address.
If all of this was successfull, you will see a message saying: "Your email was sent succsessfully."

Now, if formsent does not equal 1, then the form proccessor will not be executed, and the user will be shown the email form.

Now the entire email.php together in one spot:

Code: php:

<?php
if ($_POST['formsent'] == "1")
{
  $email = $_POST['email'];
  $subject = $_POST['subject'];
  $youremail = $_POST['replyto'];
  $message = $_POST['message'];
  if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
  {
    echo "The e-mail was not valid. Please click the back button on your browser and try again.";
  }
  else
  {
    if($subject == "")
    {
      echo "You must enter a email subject";
    }
    else
    {
      if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $youremail))
      {
        echo "Your e-mail was not valid. Please click the back button on your browser and try again.";
      }
      else
      {
        if ($message == "")
        {
          echo "You must enter a message";
        }
        else
        {
          
          $headers = "From: ".$youremail;
          mail($email,$subject,$message,$headers);
          echo "Your email was sent succsessfully.";
        }
      }
    }
  }
}
else
{
?>


<form action="email.php" method="post">
  <p>
    <input type="hidden" name="formsent" value="1" />
    Email To Send to: <input type="text" name="email" value="" /><br />
    Email Subject: <input type="text" name="subject" value="" /><br />
    Your Email: <input type="text" name="replyto" value="" /><br />
    Message:<br /> <textarea cols="70" rows="10" name="message"></textarea><br />
    <input type="submit" value="Send Email" /><input type="reset" value="Reset" />
  </p>
</form>
<?php
}
?>
    


If you liked this tutorial, please register on our forums and help support us.

Last edited by Eli (2006-07-22 04:25:39)


http://www.powerwd.net/files/gallery/signatures/savior-sig.png

Offline

 

2006-07-22 04:24:07

AdBot
AdsPosting Bot



Information

Please donate to Power Software to help keep our programs free.

Board footer