Form to email php

Discussion in 'General troubleshooting' started by Debbie, Aug 9, 2014.

  1. Hi everybody,
    I set up my contact form just fine. I am now working on the php part of it. I do not know php so am learning it little by little as I do this. It doesn't seem to work. This is my code, I was hoping you could look at it and tell me what is wrong. When a user fills out the form, the thank you page will come but but no information from that form will be sent to my email. I only get the test message from the server that I was told I needed to put in.
    I am sure something is incorrect but I am so new to php.
    When I post the code, I masked some information I didn't want to bring to the forum. For example, recipient, host, username, password, and mail are all fake ones. My correct information is def there in my Dreamweaver program though.
    Thanks a lot. :)


    <html>
    <head><title>Test email via php</title></head>
    <body>
    <?
    require_once "Mail.php";
    $from = "Sender <[email protected]>";
    $to = "Recipient <[email protected]>";
    $subject = "This is a test email sent via php";
    $body = "This is a test email";
    $host = "mail.domain.com";
    $username = "[email protected]";
    $password = "password";
    $headers = array ('From' => $from,
    'To' => $to,
    'Subject' => $subject);
    $smtp = Mail::factory('smtp',
    array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));
    $mail = $smtp->send($to, $headers, $body);
    ?>

    <?php
    // This 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;
    }
    }

    // Load form field data into variables.
    $first_name= $_REQUEST['first_name'] ;
    $last_name = $_REQUEST['last_name'] ;
    $email = $_REQUEST['email'] ;
    $comments = $_REQUEST['comments'] ;

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

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

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

    // If we passed all previous tests, send the email!
    else {
    mail( "[email protected], "Email from website",
    $comments, "From: $email" );
    header( "Location: thank_you.html" );
    }
    ?>
    </body>
    </html>
     
  2. ComputerMan

    ComputerMan Winhost Staff

    You might want to take a look at this web site for help with php here: http://www.w3schools.com/php/php_forms.asp
     
  3. Hi, thank you for getting back to me. The code that I displayed is really what I want to stick with. Did you see anything wrong with my code? When you submit the comments, you are taken to the thank you page but I still get no email from it.
     
  4. You're getting the test message because you are telling the php script to send the test message:

    $subject = "This is a test email sent via php";
    $body = "This is a test email";


    That says, use this specific text for the subject and body.

    You have to pass the input from the form to the script...something like:

    $subject = $_POST['subject'];
    $body = $_POST['body'];


    The way you define those variables can be different depending on how the form works. We can't see your form input page, so we don't know what's going on there.

    It looks like you might be using pieces from different sources? If you're a little unsure of what you're doing, that's likely to cause problems.

    You might be better off finding a script that will work for you from a single source or tutorial and using that.
     
  5. Hi guys. I have been able to get some help with my code. It wasn't correct. I am still struggling on how to put this all together.
    My actual contact form is all set up and now I am putting it all together with the form_submit. I am just not sure how to go about it.
    This is my code to capture all of my fields and and submit the data to my email address. I changed email, password, and domain to fake ones just so it isn't public.
    My form action is form_submit.php. I just have no idea how to add in the authentication. which will come after this.


    <?php

    if (isset($_POST['submit'])) {

    // get name

    $name = trim($_POST['name']);

    // get email address

    $email = trim($_POST['email']);

    // get comments

    $comments = stripslashes(trim($_POST['comments']));

    // check antiSpam field

    $antiSpam = trim($_POST['antiSpam']);

    if (!empty($antiSpam)) {

    exit;

    }

    $to = "[email protected]";

    $subject = "Comments from website";

    $headers = "From: $email\r\n";

    $headers .= "Reply-To: $email\r\n";

    $message = "Name: $name\n\n";

    $message .= "Email Address: $email\n\n";

    $message .= "Comments: $comments\n\n";

    mail($to, $subject, $message, $headers);

    $sent = "Mail was sent successfully";

    }
    ?>

    This second part is the code to authenticate. I got this right from Winhost. I have no idea how to add this in. Do I save this as Mail.php and just upload it to the server? My form action is set to form_submit.php with just the code above. I am just not sure what I am doing wrong because as of now, I just get a blank page after hitting submit with nothing sent to my email Again, domain, password, and email are fake ones just to keep off the public forum.
    Thanks.

    <?php
    require_once "Mail.php";

    $from = "Sender <[email protected]>";
    $to = "Recipient <[email protected]>";
    $subject = "Hi!";
    $body = "Hi,\n\n This is my test sending from PHP wiht SMTP Authentication?";

    $host = "mail.domain.com";
    $username = "[email protected]";
    $password = "password";

    $headers = array ('From' => $from,
    'To' => $to,
    'Subject' => $subject);
    $smtp = Mail::factory('smtp',
    array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));

    $mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail)) {
    echo("<p>" . $mail->getMessage() . "</p>");
    } else {
    echo("<p>Message successfully sent!</p>");
    }
    ?>
     
    Last edited: Aug 13, 2014

Share This Page