Oct
18
2011

Simple PHP send mail

At some point in your websites life, you would like to recieve either feedback, comments or suggestions from your users. With the power of PHP, you can have any type of contact form for any reason you’d like. In this section I want to teach you how to create a simple PHP send mail form, directly to your email with little or no hassle at all. The way we are going to do it is straight-forward, using the same PHP file. We will use a conditional statement if, else to determine what we will display to the user. Let’s use a similar form to the one used in one of our previous posts, Learn Basics for PHP Part 2.
 
 
 

The above is the simple html form. This should be inside our sendmail.php file. At the top of this file, our php code should be added, right before our html tag. When the user clicks on submit, we will have our variable

available to us. Let’s verify that variable, and all variables that are coming in from the user form. We also need to make sure that the email entered is a valid one as well.

Now that we have taken care of our erros, let’s try to send out the email message. Right under our previous logic, we should add this send logic.

Now that we have handled our sending mail logic, we need to setup the display. We have to make sure, that when the submit button is clicked, and everything goes well, we display a success message to the user. Otherwise, we need to tell them exactly what happened. In order to do this, we need to use the if, else conditional statement. Let’s take a look.

That’s basically it for this PHP mail function to work. When sending a message with mail function, you can also add an additional_parameters. This can contain a from address, and a reply to address as well.

That’s all for now, but if anyone has any suggestions, comments or questions, feel free to let me know, and I’ll be glad to help.

Related Posts

About the Author: Jorge Torres

MS. Computer Science Codeigniter Web Developer

2 Comments+ Add Comment

  • Requested code

    Jotorres – PHP Send mail

    Jotorres – PHP Send Mail Form
    0){
    echo (isset($errors[‘name’]) ? $errors[‘name’]: ”).”;
    echo (isset($errors[‘email’]) ? $errors[‘email’]: ”).”;
    echo (isset($errors[‘message’]) ? $errors[‘message’]: ”).”;
    echo (isset($errors[‘send’]) ? $errors[‘send’]:”).”;
    }

    ?>

    Full Name:

    Your Valid Email:

    Subject:

    Message:

     

    • I take it, that’s not your full logic. Post up how you are filling your message variable, the message being sent to the mail() function.

Leave a comment