why doesnt my email send?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luke noob
    New Member
    • Aug 2009
    • 69

    why doesnt my email send?

    This is my script.....
    [code=php]
    <?php

    $to = "example@exampl e.co.uk";
    $subject = "subject";

    //minimum characters allowed in the message box
    $msg_min_chars = "10";

    //maximum characters allowed in the message box
    $msg_max_chars = "250";

    function validate_form_i tems()
    {
    global $msg_min_chars, $msg_max_chars;
    $msg_chars = "{".$msg_min_ch ars.",".$msg_ma x_chars."}";

    $form_items = array(

    "name" => array(
    "regex" => "/^([a-zA-Z '-]+)$/",
    "error" => "Name appears to be in inproper format",
    ),
    "email" => array(
    "regex" =>
    "/^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/",
    "error" => "Please enter a valid email address",
    ),
    "message" => array(
    "regex" => "/^.{$msg_chars}$/",
    "error" => "Your message is either too short or exceeds $msg_max_chars characters",
    ),
    );

    $errors = array();

    foreach($form_i tems as $item_name => $item_props)
    {
    if (!preg_match($i tem_props["regex"], trim($_POST[$item_name])))
    {
    $errors[] = $item_props["error"];
    }
    }

    return $errors;
    }

    function email($from, $to, $subject, $message)
    {
    $headers = "From: ".$from."\r \n";
    $headers .= "Reply-To: ".$from."\r \n";
    $headers .= "Return-Path: ".$from."\r \n";

    if (mail($to,$subj ect,$message,$h eaders) ) {
    echo "Thank you for your inquiry, Your message has been received. <br>We will get back to you as soon as we can.";
    } else {
    echo "Your message could not be sent at this time. Please try again.";
    }
    }

    function print_error($er rors)
    {
    foreach($errors as $error)
    {
    echo $error."<br>";
    }
    }

    function form_process()
    {
    global $to, $subject;

    $errors = validate_form_i tems();

    if(count($error s) == 0)
    {
    $errors [] = email(trim($_PO ST["email"]), $to, $subject, $_POST["message"]);
    }

    print_error($er rors);
    }

    form_process();

    ?>
    [/code]


    My form......

    [code=php]
    <form id="test" method="post" onsubmit="retur n false;">
    <table border="0">
    <tr>
    <td colspan="2">

    <div id="errors">
    &nbsp;
    </div>

    </td>
    </tr>

    <tr>
    <td colspan="2">

    <font size="+2"><b>Em ail Us</b></font>

    </td>
    </tr>

    <tr>
    <td>
    <b>Name:</b>

    <input type="text" name="name" id="name" size="25" maxlength="20" />
    </td>
    </tr>

    <tr>
    <td>
    <b>Email:</b>

    <input type="text" name="email" id="email" size="25" maxlength="35" />
    </td>
    </tr>

    <tr>
    <td>
    <b>&nbsp;Messag e:</b>
    </td>
    <td>
    <i>(max 250 characters allowed)</i>
    </td>
    </tr>
    <tr>
    <td colspan="2">
    <textarea name="message" id="message" cols="36" rows="10"></textarea>
    </td>
    </tr>
    <tr>
    <td colspan="2" align="right">
    <input type="submit" value="submit" name="submit" onclick="javasc ript: sendRequest();" />
    </td>
    </tr>
    </table>
    </form>

    [/code]

    please help
  • labmonkey111
    New Member
    • Sep 2008
    • 44

    #2
    Many things could cause the email not to send, or appear not to send. Are you getting any errors? Does it say its send its but you don't get the email? In anything in the error logs?

    A few problems I had when first using the mail() function was that php.ini needed to use the full path to sendmail, the message text needs each line to be less than 70 characters long, and of course, my spam filter kicked it every time. Giving more detail should help us figure out your problem.

    I just noticed this line
    Code:
    <form id="test" method="post" onsubmit="return false;">
    Why are you doing a "return false"? That will cause the form not to submit at all, which could be your whole problem right there.

    Comment

    • gopan
      New Member
      • Apr 2009
      • 41

      #3
      Dear labmonkey111 ,
      luke noob is using a javascript to send the form...
      And php mail( ) does not require the path to send mail. i think you thought of perl mail( ).

      Dear luke noob,
      First of all your sendRequest( ) function is not given in the code... I'm assuming its having a basic validation and submit code (line#52 in form code)

      In your mail function php code try the code below at line#53
      Code:
              $sent = @mail($to,$subject,$message,$headers);
      
              if ($sent ) {
                  echo "Thank you for your inquiry, Your message has been received. <br>We will get back to you as soon as we can.";
              } else {
                  echo "Your message could not be sent at this time. Please try again.";
              }
      coz I encounter not sending mail if my code mail() in a if condition.. dunno the reason why.

      You may also check if the mail function fails for real thru

      Code:
            mail($to,$subject,$message,$headers) or die("Mail function failed");
      Try this...

      Comment

      • luke noob
        New Member
        • Aug 2009
        • 69

        #4
        Thanks for the reply, i have used a bit of ajax that is not shown here it is
        [code=php]
        <style>
        #errors
        {

        font-size:14px;
        font-weight:normal;
        margin:0px;
        padding:0px;
        }
        </style>

        <script type="text/javascript" src="js/prototype.js"></script>
        <script type="text/javascript">

        function sendRequest() {

        new Ajax.Request("e mail_form_proce ss.php", {
        method: 'post',
        postBody: "name="+$F("nam e")+"&email="+$ F("email")+"&me ssage="+$F("mes sage"),
        onComplete: showResponse

        });
        }


        function showResponse(re q)
        {
        $('errors').sty le.borderStyle= "solid";

        var res=/message was received/;

        if(req.response Text.match(res) )
        {
        $('errors').sty le.borderColor= "green";
        $('errors').sty le.color="green ";
        }else{
        $('errors').sty le.borderColor= "red";
        $('errors').sty le.color="red";
        }

        $('errors').sty le.borderSize= "1px";
        $('errors').inn erHTML= req.responseTex t;
        }

        </script>
        [/code]
        i tryed again today and it works. For some reason it didnt yesterday,

        i have another problem though, i have an input feild for the name to be enterd, but when i post it to my email address the name does not show there, i have tryed to edit this to work but its not working still, could you give me some advice please. my input for the name, is "name" but im not sure where to edit my script.

        Comment

        • gopan
          New Member
          • Apr 2009
          • 41

          #5
          ooh...
          check your first code php script line#49

          Code:
          $headers = "From: ".$from."\r\n";
          to

          Code:
          $headers = "From: ".$name." <".$from.">\r\n";
          then you will get name of the sender...
          Hope this will help...

          Comment

          • luke noob
            New Member
            • Aug 2009
            • 69

            #6
            Perfect thank you, I last thing...

            Is it possible to sent to two different email addresses at the same time, by changing line 3, somehow

            [code=php]
            $to = "example@exampl e.co.uk";
            [/code]

            itz not a serious thing but it would make life a bit easier.

            Comment

            • Dheeraj Joshi
              Recognized Expert Top Contributor
              • Jul 2009
              • 1129

              #7
              I think you can have $to as an array...

              Regards
              Dheeraj Joshi

              Comment

              • gopan
                New Member
                • Apr 2009
                • 41

                #8
                Originally posted by dheerajjoshim
                I think you can have $to as an array...

                Regards
                Dheeraj Joshi
                No dear... php mail function can mail to multiple recipients by giving all the addresses as a coma seperated string... thats all...

                Code:
                    $to = "example@example.co.uk, example@example.co.au, example@example.co.ar";

                Comment

                • luke noob
                  New Member
                  • Aug 2009
                  • 69

                  #9
                  Thank you this helps alot.

                  Comment

                  • Dheeraj Joshi
                    Recognized Expert Top Contributor
                    • Jul 2009
                    • 1129

                    #10
                    Thanks for the info Gopan..

                    I did not know that..


                    Regards
                    Dheeraj Joshi

                    Comment

                    Working...