Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question PHP Validation/Email Wont Send

    I am having a few problems, my email wont send I get a few errors where I have my mail (). Also, not too sure how to validate my form. I am using php 6 and if possible would like to stick to it cause its what I understand better.


    PHP Code:

    <?php

    $first_name
    =$_POST['author'];
    $email_address=$_POST['email'];
    $subject=$_POST['subject'];
    $message=$_POST['text'];

    if (empty(
    $_POST['author']))
    {
    $errors[] = 'Please enter a name';
    }
    if (empty(
    $_POST['subject']))
    {
    $errors[] = 'Please enter a subject';
    }

    if (empty(
    $_POST['text']))
    {
    $errors[] = 'Please enter your comments.';
    }

    if(empty(
    $_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        
    $errors[] = "Please enter a valid email address.";
    }
    if (
    count($errors) == 0)
    {
    // Process form
    mail("myemailaddress@gmail.com","Subject: $subject",
    $message,"From: $first_name <$email_address>");

    echo 
    "Thank you for using our mail form.<br/>";
    echo 
    "Your email has been sent.";
    }
    else
    {
    echo 
    $errors[0];
    }
    ?>

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,301
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    mail() returns a boolean therefore you should always use it inside an if conditional so that you know the result from the mail() call.

    True means it connected and communicated with sendmail (not that sendmail successfully sent) and false means something went wrong. This is vital as it lets you track down where the problem lies - your code/server or the internet.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    New Coder
    Join Date
    Feb 2009
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    mail() returns a boolean therefore you should always use it inside an if conditional so that you know the result from the mail() call.

    True means it connected and communicated with sendmail (not that sendmail successfully sent) and false means something went wrong. This is vital as it lets you track down where the problem lies - your code/server or the internet.
    So, my error lies in this portion?

    PHP Code:

    if (count($errors) == 0)
    {
    // Process form
    mail("myemailaddress@gmail.com","Subject: $subject",
    $message,"From: $first_name <$email_address>");

    echo 
    "Thank you for using our mail form.<br/>";
    echo 
    "Your email has been sent.";
    }
    else
    {
    echo 
    $errors[0];
    }
    ?> 
    I need to put my mail () in an if statement?

  • #4
    New Coder
    Join Date
    Sep 2011
    Posts
    61
    Thanks
    9
    Thanked 1 Time in 1 Post
    What he tells you to is to make sure it tells you something is wrong if mail() returns false...
    You can do this by using this instead.
    PHP Code:
    mail("myemailaddress@gmail.com","Subject: $subject",
    $message,"From: $first_name <$email_address>") or die("Mail failed to communicate with the server"); 
    By adding "or die("Error message here");" to the end of a function returning boolean, the script will exit when the function returns false and then print out the given error message.

  • #5
    New Coder
    Join Date
    Feb 2009
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks! I got it working.

  • #6
    New Coder
    Join Date
    Sep 2011
    Posts
    61
    Thanks
    9
    Thanked 1 Time in 1 Post
    Quote Originally Posted by saxchick1 View Post
    Thanks! I got it working.
    Got what working? Die or your problem?
    If it's your issue, then please explain how, as I have a problem quite similar to this :-)

  • #7
    New Coder
    Join Date
    Feb 2009
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Time Sheep View Post
    Got what working? Die or your problem?
    If it's your issue, then please explain how, as I have a problem quite similar to this :-)
    I went about a different route and used a different type of form validation.

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,301
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by Time Sheep View Post
    Got what working? Die or your problem?
    If it's your issue, then please explain how, as I have a problem quite similar to this :-)
    No you have a problem where you're not setting the senders address from what I recall.

    Fakesendmail doesn't like that and on my setup it refuses to send.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •