• PHP Form That Sends You An Email

      0 comments

    When making  a form on a website there are several options of where you want that information to go, either a database or to your email. This would be done with a site that you have a form that gathers information from a client who is requesting more information. For some time now I have been trying to work out how to have both actions, send the data to a database and to my email. This way I have captured the clients information in a database which I can then use to contact them at a later date. Having to go to my email helps me know who is trying to reach me about their website.

    Now I have it set up so that both can happen and here is the php code for it for the form:

    <?php
    } else {
    ?>
    <form action=”test.php” method=”post”>
    <table width=”400″ border=”0″ align=”center” cellspacing=”2″ cellpadding=”0″>
    <tr>
    <td width=”29%”>Your name:</td>
    <td width=”71%”><input name=”name” type=”text” id=”name” size=”32″></td>
    </tr>
    <tr>
    <td>Email address:</td>
    <td><input name=”email” type=”text” id=”email” size=”32″></td>
    </tr>
    <tr>
    <td>Comment:</td>
    <td><textarea name=”comment” cols=”45″ rows=”6″ id=”comment”></textarea></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td align=”left” valign=”top”><input type=”submit” name=”Submit” value=”Send”></td>
    </tr>
    </table>
    </form> ?>

    Here is the code for the email action:

    <?php
    if ($_POST["email"]<>”) {
    $ToEmail = ‘youremail@site.com’;
    $EmailSubject = ‘Site contact form ‘;
    $mailheader = “From: “.$_POST["email"].”\r\n”;
    $mailheader .= “Reply-To: “.$_POST["email"].”\r\n”;
    $mailheader .= “Content-type: text/html; charset=iso-8859-1\r\n”;
    $MESSAGE_BODY = “Name: “.$_POST["name"].”<br>”;
    $MESSAGE_BODY .= “Email: “.$_POST["email"].”<br>”;
    $MESSAGE_BODY .= “Comment: “.nl2br($_POST["comment"]).”<br>”;
    mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die (“Failure”);
    ?>}

    Couple of points that you need to pay close attention to in the email PHP code is that you need to fill in the $ToEmail variable so that it has your email address that you are sending it to. Also, make sure that the other variable’s strings throughout the form match the tables id’s. For example: $mailheader = “From: “.$_POST["email"].”\r\n”; the area $_POST["email"] is the id for the form which you will have to match with the database row. With my site I have it like this: $mailheader = “From: “.$_POST["txtEmail"].”\r\n”;. The txtEmail represents my row in the table for my database. So make you go through and match this up.

    Very, very important update! You will need to add this hidden field just below the submit but still in the <form></form> area “<input name=”email” type=”hidden” id=”admin@linkedupdesign.com” value=”email” />”. The email will not be sent unless you have this in there. This is the hidden field that tells the <?php?> to work!!  make sure that the value=”email” and the name=”email” stay the same because that is the global $_POST variable name.

    I have this email PHP code set up just about the form but not part of the <form></form>. Reason being is the form action is the recordset to post in to the databse so you don’t want to cross wires per say. These will be two separate action in the form.

    If you have any questions about this please feel free to get in touch with me.

    Write a comment




SEO Powered by Platinum SEO from Techblissonline