1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

contact form doesnt send email

Discussion in 'PHP & Perl' started by thorjack1990, May 9, 2013.

  1. thorjack1990

    thorjack1990 BANNED BANNED

    Joined:
    Feb 15, 2013
    Messages:
    71
    Likes Received:
    9
    Does any1 know what ive done wrong with this code? when i click send on the form it will redirect me to thank_you page and says successfully sent.,but i did'nt get anything on my email.,ive also checked on spam folder nothing there.
    heres my code:

    <?php
    /*
    This first bit sets the email address that you want the form to be submitted to.
    You will need to change this value to a valid email address that you can access.
    */
    $webmaster_email = "roiancuares@gmail.com";


    /*
    This bit sets the URLs of the supporting pages.
    If you change the names of any of the pages, you will need to change the values here.
    */
    $contacts_page = "contacts.html";
    $error_page = "error_message.html";
    $thankyou_page = "thank_you.html";


    /*
    This next bit loads the form field data into variables.
    If you add a form field, you will need to add it here.
    */
    $firstname = $_REQUEST['firstname'] ;
    $email = $_REQUEST['email'] ;
    $website = $_REQUEST['website'] ;
    $comments = $_REQUEST['comments'] ;


    /*
    The following function checks for email injection.
    Specifically, it checks for carriage returns - typically used by spammers to inject a CC list.
    */
    function isInjected($str) {
    $injections = array('(\n+)',
    '(\r+)',
    '(\t+)',
    '(%0A+)',
    '(%0D+)',
    '(%08+)',
    '(%09+)'
    );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str)) {
    return true;
    }
    else {
    return false;
    }
    }


    // If the user tries to access this script directly, redirect them to the feedback form,
    if (!isset($_REQUEST['email'])) {
    header( "Location: $contacts_page" );
    }


    // If the form fields are empty, redirect to the error page.
    elseif (empty($firstname) ||empty($email) || empty($comments)) {
    header( "Location: $error_page" );
    }


    // If email injection is detected, redirect to the error page.
    elseif ( isInjected($email_address) ) {
    header( "Location: $error_page" );
    }


    // If we passed all previous tests, send the email then redirect to the thank you page.
    else {
    mail( "$webmaster_email", "Feedback Form Results",
    $firstname, $email, $website ,$comments, "From: $email" );
    header( "Location: $thankyou_page" );
    }
    ?>
     
  2. Wister_fr

    Wister_fr Registered Member

    Joined:
    Sep 6, 2012
    Messages:
    62
    Likes Received:
    23
    Location:
    Internet
    Hum ... Obviously you did not write those lines of code ... => You try to avoid injections (in an email?!) but you do not know how to use mail function

    SORRY I WILL NOT ANSWER TO THIS THREAD UNTIL YOU ANSWER ALL THE PREVIOUS THREADS YOU LAUNCHED WITHOUT ANSWERING.
     
  3. mazgalici

    mazgalici Supreme Member

    Joined:
    Jan 2, 2009
    Messages:
    1,489
    Likes Received:
    881
    Home Page:
    the email function might be disabled
     
  4. DeJohn

    DeJohn Newbie

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    16
    Home Page:
    Your code seems to be wrong created, you cannot use all strings into mail() function such as firstname,lastname, website etc. - these strings have to be included in mail content or headers. Here's the updated code:

    PHP:
    // If we passed all previous tests, send the email then redirect to the thank you page.
    else {

    // Email content
    $content 'Website: ' $website '\r\n';
    $content .= $comments '\r\n';

    // Additional headers
    $headers 'To: '.$firstname.' <'.$email.'>' "\r\n";
    $headers .= 'From: Webmaster <'.$webmaster_email.'>' "\r\n";
    mail($email"Feedback Form Results"$comments$headers);

    header"Location: $thankyou_page);
    }
     
    • Thanks Thanks x 1
  5. thorjack1990

    thorjack1990 BANNED BANNED

    Joined:
    Feb 15, 2013
    Messages:
    71
    Likes Received:
    9
    ive thank you for all your replies from my previous post.
     
  6. thorjack1990

    thorjack1990 BANNED BANNED

    Joined:
    Feb 15, 2013
    Messages:
    71
    Likes Received:
    9
    ive inserted the code you give me.,but nothing happens.,thank you for helping
     
  7. Wister_fr

    Wister_fr Registered Member

    Joined:
    Sep 6, 2012
    Messages:
    62
    Likes Received:
    23
    Location:
    Internet
    Accepted :)


    Your mail function was bad, here is a correct version (also accepted on gmail, because mail function send by default mails that are rejected by gmail)
    PHP:
    srand((double)microtime()*1000000); 
    $boundary md5(uniqid(rand())); 
    $texte .= "<center>Feedback Form Result</center>"


    $headers 'From: '.$firstname.' <'.$email.'>' "\r\n" .
     
    'Reply-To: '.$email.'' "\r\n";
    $headers .="MIME-Version: 1.0\n"
    $headers .="Content-Type: multipart/alternative;boundary=$boundary\n"




    $message "\nThis is a multi-part message in MIME format."
    $message .="\n--$boundary\nContent-Type: text/html;charset=\"iso-8859-1\"\n\n"
    $texte="Firstname: $firstname <br/>
    Email: 
    $email <br/>
    Website: 
    $website <br/>
    Comment: 
    $comments <br/>";
    $message .= $texte;
    $message .="\n--$boundary--\n end of the multi-part"
    $titre="Feedback Form Result";


    $to=$webmaster_email;
    mail($to$titre$message$headers);    



    You should also use $_GET or $_POST instead of $_REQUEST in your code (depending on the method you used in your form).
     
    • Thanks Thanks x 1
    Last edited: May 10, 2013
  8. thorjack1990

    thorjack1990 BANNED BANNED

    Joined:
    Feb 15, 2013
    Messages:
    71
    Likes Received:
    9
    well heres my form
     
  9. Wister_fr

    Wister_fr Registered Member

    Joined:
    Sep 6, 2012
    Messages:
    62
    Likes Received:
    23
    Location:
    Internet
    Try this, this should work:

    <?php


    /*
    This first bit sets the email address that you want the form to be submitted to.
    You will need to change this value to a valid email address that you can access.
    */
    $webmaster_email = "roiancuares@gmail.com";




    $contacts_page = "contacts.html";
    $error_page = "error_message.html";
    $thankyou_page = "thank_you.html";




    $firstname = $_POST['firstname'];
    $email = $_POST['email'];
    $website = $_POST['website'];
    $comments = $_POST['comments'];


    function isInjected($str) {
    $injections = array('(\n+)',
    '(\r+)',
    '(\t+)',
    '(%0A+)',
    '(%0D+)',
    '(%08+)',
    '(%09+)'
    );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if (preg_match($inject, $str)) {
    return true;
    } else {
    return false;
    }
    }


    if (!isset($_POST['email'])) {
    header("Location: $contacts_page");
    }elseif (empty($firstname) || empty($email) || empty($comments)) {
    header("Location: $error_page");
    }elseif (isInjected($email_address)) {
    header("Location: $error_page");
    }else {
    srand((double) microtime() * 1000000);
    $boundary = md5(uniqid(rand()));
    $texte .= "<center>Feedback Form Result</center>";




    $headers = 'From: ' . $firstname . ' <' . $email . '>' . "\r\n" .
    'Reply-To: ' . $email . '' . "\r\n";
    $headers .="MIME-Version: 1.0\n";
    $headers .="Content-Type: multipart/alternative;boundary=$boundary\n";








    $message = "\nThis is a multi-part message in MIME format.";
    $message .="\n--$boundary\nContent-Type: text/html;charset=\"iso-8859-1\"\n\n";
    $texte = "Firstname: $firstname <br/>
    Email: $email <br/>
    Website: $website <br/>
    Comment: $comments <br/>";
    $message .= $texte;
    $message .="\n--$boundary--\n end of the multi-part";
    $titre = "Feedback Form Result";




    $to = $webmaster_email;
    mail($to, $titre, $message, $headers);
    header("Location: $thankyou_page");
    }
    ?>
     
    • Thanks Thanks x 1
    Last edited: May 10, 2013
  10. DeJohn

    DeJohn Newbie

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    16
    Home Page:
    Does the code provided above work ? Otherwise i will create it from scratch and give you, i didn't have time yesterday to check that function but probably something is wrong in the rest of the code.

    Thanks !
     
    • Thanks Thanks x 1
  11. thorjack1990

    thorjack1990 BANNED BANNED

    Joined:
    Feb 15, 2013
    Messages:
    71
    Likes Received:
    9
    still not working.,when i fill all the forms it will redirect me to error_page.html

    heres the live view http://roiancuares.cuccfree.org/contacts.html
     
  12. DeJohn

    DeJohn Newbie

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    16
    Home Page:
    I have modified the send_mail.php page for your requirements and tested, it works on my side. The main problem was that comments string was $_REQUEST['comments'] NOT $_REQUEST['message'] as is your form, that's why you were redirected to error page, now works fine.

    PHP:
    <?php
    /*
    This first bit sets the email address that you want the form to be submitted to.
    You will need to change this value to a valid email address that you can access.
    */
    $webmaster_email "roiancuares@gmail.com";


    /*
    This bit sets the URLs of the supporting pages.
    If you change the names of any of the pages, you will need to change the values here.
    */
    $contacts_page "contacts.html";
    $error_page "error_message.html";
    $thankyou_page "thank_you.html";


    /*
    This next bit loads the form field data into variables.
    If you add a form field, you will need to add it here.
    */
    $firstname $_REQUEST['firstname'] ;
    $email $_REQUEST['email'] ;
    $website $_REQUEST['website'] ;
    $comments $_REQUEST['message'] ;


    /*
    The following function checks for email injection.
    Specifically, it checks for carriage returns - typically used by spammers to inject a CC list.
    */
    function isInjected($str) {
    $injections = array('(\n+)',
    '(\r+)',
    '(\t+)',
    '(%0A+)',
    '(%0D+)',
    '(%08+)',
    '(%09+)'
    );
    $inject join('|'$injections);
    $inject "/$inject/i";
    if(
    preg_match($inject,$str)) {
    return 
    true;
    }
    else {
    return 
    false;
    }
    }


    // If the user tries to access this script directly, redirect them to the feedback form,
    if (!isset($_REQUEST['email'])) {
    header"Location: $contacts_page);
    }


    // If the form fields are empty, redirect to the error page.
    elseif (empty($firstname) ||empty($email) || empty($comments)) {
    header"Location: $error_page);
    }


    // If email injection is detected, redirect to the error page.
    elseif ( isInjected($email_address) ) {
    header"Location: $error_page);
    }


    // If we passed all previous tests, send the email then redirect to the thank you page.
    else {

    // Email content
    $content "Firstname: $firstname ( $email ) \r\n";
    $content .= "Website: $website \r\n";
    $content .= "Comments: \n$comments  \r\n";

    // Additional headers
    $headers .= 'To: Webmaster <'.$webmaster_email.'>' "\r\n";
    $headers 'From: '.$firstname.' <'.$email.'>' "\r\n";
    mail($webmaster_email"Feedback Form Results"$content$headers);

    header"Location: $thankyou_page);
    }  
    ?> 
     
    • Thanks Thanks x 1
  13. prab1996

    prab1996 Elite Member

    Joined:
    Jan 8, 2013
    Messages:
    3,496
    Likes Received:
    2,027
    Occupation:
    your gf's <3 ♥♥♥♥
    Location:
    Prab1996.com
    Home Page:
    i use wufoo forums and they are toooooooo good .
     
  14. thorjack1990

    thorjack1990 BANNED BANNED

    Joined:
    Feb 15, 2013
    Messages:
    71
    Likes Received:
    9
    it works it will redirect to thank_you page.,the only problem is it wont send any email.,is it because of the hosting? or do you guys know any other way of testing form without hosting?
     
  15. Wister_fr

    Wister_fr Registered Member

    Joined:
    Sep 6, 2012
    Messages:
    62
    Likes Received:
    23
    Location:
    Internet
    That's because of the gmail adress. Try with an hotmail account or use MIME format in your mail because Gmail needs a strict MIME format to accept a mail.
     
    • Thanks Thanks x 1
  16. DeJohn

    DeJohn Newbie

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    16
    Home Page:
    Hello,

    I've tested it using a gmail address too and worked for me, probably you have any problem with mail function in PHP of your webhost, maybe is disabled ? or check SPAM folder.

    I suggest you to try this on some free webhosts such as 000webhost.com
     
    • Thanks Thanks x 1
  17. thorjack1990

    thorjack1990 BANNED BANNED

    Joined:
    Feb 15, 2013
    Messages:
    71
    Likes Received:
    9
    i also tried the form you give me but it doesnt send an email as well.,i think theyre is problem with hosting.,free hosting doesnt support email i think
     
  18. thorjack1990

    thorjack1990 BANNED BANNED

    Joined:
    Feb 15, 2013
    Messages:
    71
    Likes Received:
    9
    free hosting doesnt support email i guess.,are u using a premium one? when testing it.,it seems that it didnt work on me
     
  19. DeJohn

    DeJohn Newbie

    Joined:
    Nov 10, 2012
    Messages:
    41
    Likes Received:
    16
    Home Page:
    Hmm, as I tested a while ago, 000webhost sent emails with the free plan. I am sure the problem is from your webhost, I've tested my code and worked for me, try to change your webhost or first contact the new webhosting provider to ask about mail function, or contact your current webhost company to enable sending emails for you.
     
    • Thanks Thanks x 1
  20. thorjack1990

    thorjack1990 BANNED BANNED

    Joined:
    Feb 15, 2013
    Messages:
    71
    Likes Received:
    9
    thank you so much DeJohn.,its working now.,the hosting is the problem.