October 18, 2011 mail_picture

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.

2 thoughts on “Simple PHP send mail

  1. Requested code

    Jotorres – PHP Send mail

    Jotorres – PHP Send Mail Form
    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:




Leave a Reply

Your email address will not be published. Required fields are marked *

The qTranslate Editor has disabled itself because it hasn't been tested with your Wordpress version yet. This is done to prevent Wordpress from malfunctioning. You can reenable it by clicking here (may cause data loss! Use at own risk!). To remove this message permanently, please update qTranslate to the corresponding version.