Grow your CSS skills. Land your dream job.

PHP Email doesn’t work!!!!!!!! HELP PLEASE

  • # April 1, 2013 at 4:47 pm

    Can someone help me with this? I used this PHP email form from CSS-TRICKS
    http://css-tricks.com/sending-nice-html-email-with-php/

    I am trying to customize it to my own but its not working, here is the link of the form I am customizing:
    http://www.keenannagle.com/clientreview/monumentalmortgage/form/form.php

    It looks like the email form goes through but I dont get any emails. I think it’s not working because the “radios” variables I am using.

    # April 1, 2013 at 8:24 pm

    what code are you using exactly

    # April 1, 2013 at 9:11 pm

    Would this help: http://css-tricks.com/forums/discussion/comment/96732

    # April 2, 2013 at 9:30 am

    This is the code I am using:

    < ?php

    session_start();

    function getRealIp() {
    if (!empty($_SERVER)) { //check ip from share internet
    $ip=$_SERVER;
    } elseif (!empty($_SERVER)) { //to check ip is pass from proxy
    $ip=$_SERVER;
    } else {
    $ip=$_SERVER;
    }
    return $ip;
    }

    function writeLog($where) {

    $ip = getRealIp(); // Get the IP from superglobal
    $host = gethostbyaddr($ip); // Try to locate the host of the attack
    $date = date(“d M Y”);

    // create a logging message with php heredoc syntax
    $logging = < < n
    < < Start of Message >>
    There was a hacking attempt on your form. n
    Date of Attack: {$date}
    IP-Adress: {$ip} n
    Host of Attacker: {$host}
    Point of Attack: {$where}
    < < End of Message >>
    LOG;
    // Awkward but LOG must be flush left

    // open log file
    if($handle = fopen(‘hacklog.log’, ‘a’)) {

    fputs($handle, $logging); // write the Data to file
    fclose($handle); // close the file

    } else { // if first method is not working, for example because of wrong file permissions, email the data

    $to = ‘rcespedes@keenannagle.com’;
    $subject = ‘HACK ATTEMPT’;
    $header = ‘From: rcespedes@keenannagle.com‘;
    if (mail($to, $subject, $logging, $header)) {
    echo “Sent notice to admin.”;
    }

    }
    }

    function verifyFormToken($form) {

    // check if a session is started and a token is transmitted, if not return an error
    if(!isset($_SESSION[$form.'_token'])) {
    return false;
    }

    // check if the form is sent with token in it
    if(!isset($_POST)) {
    return false;
    }

    // compare the tokens against each other if they are still the same
    if ($_SESSION[$form.'_token'] !== $_POST) {
    return false;
    }

    return true;
    }

    function generateFormToken($form) {

    // generate a token from an unique value, took from microtime, you can also use salt-values, other crypting methods…
    $token = md5(uniqid(microtime(), true));

    // Write the generated token to the session variable to check it against the hidden field when the form is sent
    $_SESSION[$form.'_token'] = $token;

    return $token;
    }

    // VERIFY LEGITIMACY OF TOKEN
    if (verifyFormToken(‘form1′)) {

    // CHECK TO SEE IF THIS IS A MAIL POST
    if (isset($_POST)) {

    // Building a whitelist array with keys which will send through the form, no others would be accepted later on
    $whitelist = array(‘token’,’req-name’,’radios’,’req-email’,’address’,’city’,’state’,’phone’,’curText’,’mult’);

    // Building an array with the $_POST-superglobal
    foreach ($_POST as $key=>$item) {

    // Check if the value $key (fieldname from $_POST) can be found in the whitelisting array, if not, die with a short message to the hacker
    if (!in_array($key, $whitelist)) {

    writeLog(‘Unknown form fields’);
    die(“Hack-Attempt detected. Please use only the fields in the form”);

    }
    }

    // PREPARE THE BODY OF THE MESSAGE

    $message = ‘‘;
    $message .= ‘

    ‘;
    $message .= “

    “;
    $message .= “

    “;
    $message .= “

    “;
    $message .= “

    “;
    $message .= “

    “;
    $message .= “

    “;
    $message .= “

    “;
    $message .= “

    “;
    $message .= “

    I Am Interested in: ” . strip_tags($_POST) . “
    Name: ” . strip_tags($_POST) . “
    Address: ” . $_POST . “
    City: ” . $_POST . “
    State: ” . $_POST . “
    Phone: ” . $_POST . “
    Email: ” . strip_tags($_POST) . “
    Questions/Comments: ” . $_POST . “

    “;
    $message .= ““;

    // MAKE SURE THE “FROM” EMAIL ADDRESS DOESN’T HAVE ANY NASTY STUFF IN IT

    $pattern = “/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i”;
    if (preg_match($pattern, trim(strip_tags($_POST)))) {
    $cleanedFrom = trim(strip_tags($_POST));
    } else {
    return “The email address you entered was invalid. Please try again!”;
    }

    // CHANGE THE BELOW VARIABLES TO YOUR NEEDS

    $to = ‘rcespedes@keenannagle.com’;

    $subject = ‘Mortgage & Refinance Pre-Application’;

    $headers = “From: ” . $cleanedFrom . “rn”;
    $headers .= “Reply-To: “. strip_tags($_POST) . “rn”;
    $headers .= “MIME-Version: 1.0rn”;
    $headers .= “Content-Type: text/html; charset=ISO-8859-1rn”;

    if (mail($to, $subject, $message, $headers)) {
    echo ‘Your info has been submitted to Monumental Mortgage. We will get back to you in one business day. Thank you.’;
    } else {
    echo ‘There was a problem sending the email.’;
    }

    // DON’T BOTHER CONTINUING TO THE HTML…
    die();

    }
    } else {

    if (!isset($_SESSION[$form.'_token'])) {

    } else {
    echo “Hack-Attempt detected. Got ya!.”;
    writeLog(‘Formtoken’);
    }

    }

    ?>

    < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>




    < ?php
    // generate a new token for the $_SESSION superglobal and put them in a hidden field
    $newToken = generateFormToken(‘form1′);
    ?>

    # April 2, 2013 at 9:52 am

    Do you really think someone will be able to dig into this wall of code? :s

    Use code blocks efficiently, or paste into a Fiddle/Pen.

    # April 2, 2013 at 4:16 pm

    > Do you really think someone will be able to dig into this wall of code? :s

    > Use code blocks efficiently, or paste into a Fiddle/Pen.

    making a [gist on github](http://gist.github.com) is a good way to share lots of code.

    *****

    > It looks like the email form goes through but I dont get any emails.

    What do you mean, it “looks like” the form goes through? What are you specific results?

    > I think it’s not working because the “radios” variables I am using.

    What makes you think this? If you remove that variable, does it work as expected?

    Are you getting any error messages?

    You need to be very specific and methodical if you expect someone to be able to help you.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".