PHP Developers Network
http://forums.devnetwork.net/

email practice
http://forums.devnetwork.net/viewtopic.php?f=50&t=122474
Page 1 of 1

Author:  the_cheat [ Tue Oct 19, 2010 12:40 pm ]
Post subject:  email practice

Hi everyone, Right now I am trying to perfect creating a contact form and emailing the message. The two problems I am having right now is using preg_match()
for email address validation, and reporting user errors back to the user.

here is the current script I am using for the form's action
Syntax: [ Download ] [ Hide ]
<?php
    define(EMAIL, "rmccaffe1@gmail.com");
   
    if (!$_POST[txt_name]) {
        header("Location: email_practice.php");
    }
    if (!$_POST[txt_email_address]) {
        header("Location: email_practice.php");
    }
    if (!$_POST[txt_email_address]) {
        header("Location: email_practice.php");
    }
    if (!$_POST[txta_email_message]) {
        header("Location: email_practice.php");
    }
   
    $sender_name = $_POST[txt_name];
    $sender_address = $_POST[txt_email_address];
    $email_subject = $_POST[txt_email_message];
    $email_message = $_POST[txta_email_message];
    $headers[address] = $sender_address;
   
    $result = mail(EMAIL, $email_subject, $email_message, $headers[address]);
   
    if ($result) {
        echo "Message sent successfully";
    } else {
        echo "Message send was unsuccessful";
    }    
   
?>


when I try to place the mail() function inside of an if statement as such
Syntax: [ Download ] [ Hide ]
if (preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$^", $sender_address) {
        $result = mail(EMAIL, $email_subject, $email_message, $headers[address]);
        if ($result) {
            echo "Message sent successfully";
        } else {
            echo "Message send was unsuccessful";
        }    
    } else {
        echo "Address not valid";
    }
   


a blank page loads with the correct URL after pressing the submit button, and I am having trouble figuring out why.

I am also having trouble figuring out how to display to the user that they entered an invalid value in any of the form controls.
If anyone can tell me what to do, without really telling me how to do it, or telling me of any resources that will help with the problems I am having
I would really appreciate it. Thanks

Author:  MichaelR [ Tue Oct 19, 2010 1:58 pm ]
Post subject:  Re: email practice

Put this at the top of your code:

Syntax: [ Download ] [ Hide ]
ini_set('display_errors', 1);


You'll probably see an error. I'm guessing this will fix it (there was a missing closing parenthesis on the preg_match line):

Syntax: [ Download ] [ Hide ]
if (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$/", $sender_address)) {
        $result = mail(EMAIL, $email_subject, $email_message, $headers[address]);
        if ($result) {
            echo "Message sent successfully";
        } else {
            echo "Message send was unsuccessful";
        }    
    } else {
        echo "Address not valid";
    }
 

Author:  VladSun [ Tue Oct 19, 2010 3:21 pm ]
Post subject:  Re: email practice

You must call exit() after every header('Location:.... you've ever used.

Author:  the_cheat [ Tue Oct 19, 2010 9:43 pm ]
Post subject:  Re: email practice

Thanks a lot for the help, everything is working fine now, it was just that missing closing parenthesis. Now I am just looking for a tutorial that will help me
relay user error messages back to the user and possibly writing the form and script on the same page.

Author:  social_experiment [ Sun Dec 19, 2010 3:57 am ]
Post subject:  Re: email practice


Author:  VladSun [ Sun Dec 19, 2010 5:56 am ]
Post subject:  Re: email practice


Author:  social_experiment [ Sun Dec 19, 2010 1:31 pm ]
Post subject:  Re: email practice


Author:  VladSun [ Sun Dec 19, 2010 2:07 pm ]
Post subject:  Re: email practice


Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/