By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,843 Members | 864 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,843 IT Pros & Developers. It's quick & easy.

why doesnt my email send?

P: 69
This is my script.....
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     $to = "example@example.co.uk";
  4.     $subject = "subject";
  5.  
  6.     //minimum characters allowed in the message box
  7.     $msg_min_chars = "10";
  8.  
  9.     //maximum characters allowed in the message box
  10.     $msg_max_chars = "250";
  11.  
  12.     function validate_form_items()
  13.     {
  14.        global $msg_min_chars, $msg_max_chars;
  15.        $msg_chars = "{".$msg_min_chars.",".$msg_max_chars."}";
  16.  
  17.        $form_items = array(
  18.  
  19.            "name"  => array(
  20.                            "regex" => "/^([a-zA-Z '-]+)$/",
  21.                            "error" => "Name appears to be in inproper format",
  22.                            ),
  23.             "email" => array(
  24.                            "regex" =>
  25.                             "/^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/",
  26.                            "error" => "Please enter a valid email address",
  27.                            ),
  28.            "message" => array(
  29.                            "regex" => "/^.{$msg_chars}$/",
  30.                            "error" => "Your message is either too short or exceeds $msg_max_chars characters",
  31.                            ),
  32.        );
  33.  
  34.        $errors = array();
  35.  
  36.        foreach($form_items as $item_name => $item_props)
  37.        {
  38.            if (!preg_match($item_props["regex"], trim($_POST[$item_name])))
  39.            {
  40.                    $errors[] = $item_props["error"];
  41.            }
  42.        }
  43.  
  44.        return $errors;
  45.     }
  46.  
  47.     function email($from, $to, $subject, $message)
  48.     {
  49.         $headers = "From: ".$from."\r\n";
  50.         $headers .= "Reply-To: ".$from."\r\n";
  51.         $headers .= "Return-Path: ".$from."\r\n";
  52.  
  53.         if (mail($to,$subject,$message,$headers) ) {
  54.             echo "Thank you for your inquiry, Your message has been received. <br>We will get back to you as soon as we can.";
  55.         } else {
  56.             echo "Your message could not be sent at this time. Please try again.";
  57.         }
  58.     }
  59.  
  60.     function print_error($errors)
  61.     {
  62.         foreach($errors as $error)
  63.         {
  64.             echo $error."<br>";
  65.         }
  66.     }
  67.  
  68.     function form_process()
  69.     {    
  70.         global $to, $subject;
  71.  
  72.         $errors = validate_form_items();
  73.  
  74.         if(count($errors) == 0)
  75.         {
  76.             $errors [] = email(trim($_POST["email"]), $to, $subject, $_POST["message"]);
  77.         }
  78.  
  79.         print_error($errors);
  80.     }
  81.  
  82.     form_process();
  83.  
  84. ?>
  85.  

My form......

Expand|Select|Wrap|Line Numbers
  1. <form id="test" method="post" onsubmit="return false;">
  2.         <table border="0">
  3.                <tr>
  4.                    <td colspan="2">
  5.  
  6.                        <div id="errors">
  7.                                &nbsp;
  8.                        </div>
  9.  
  10.                    </td>
  11.                </tr>
  12.  
  13.                <tr>
  14.                    <td colspan="2">
  15.  
  16.                       <font size="+2"><b>Email Us</b></font>
  17.  
  18.                    </td>
  19.                </tr>
  20.  
  21.                <tr>
  22.                    <td>
  23.                         <b>Name:</b>
  24.  
  25.                         <input type="text" name="name" id="name" size="25" maxlength="20" />
  26.                    </td>
  27.                </tr>
  28.  
  29.                <tr>
  30.                    <td>
  31.                         <b>Email:</b>
  32.  
  33.                         <input type="text" name="email" id="email" size="25" maxlength="35" />
  34.                    </td>
  35.                </tr>
  36.  
  37.                <tr>
  38.                    <td>
  39.                         <b>&nbsp;Message:</b> 
  40.                    </td>
  41.                    <td>
  42.                         <i>(max 250 characters allowed)</i>
  43.                    </td>
  44.                </tr>
  45.                <tr>
  46.                    <td colspan="2">
  47.                         <textarea name="message" id="message" cols="36" rows="10"></textarea>
  48.                    </td>
  49.                </tr>
  50.                <tr>
  51.                    <td colspan="2" align="right">
  52.                         <input type="submit" value="submit" name="submit" onclick="javascript: sendRequest();" />
  53.                    </td>
  54.                </tr>
  55.         </table>
  56.         </form>
  57.  
  58.  
please help
Aug 16 '09 #1
Share this Question
Share on Google+
9 Replies


P: 44
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
Expand|Select|Wrap|Line Numbers
  1. <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.
Aug 16 '09 #2

gopan
P: 41
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
Expand|Select|Wrap|Line Numbers
  1.         $sent = @mail($to,$subject,$message,$headers);
  2.  
  3.         if ($sent ) {
  4.             echo "Thank you for your inquiry, Your message has been received. <br>We will get back to you as soon as we can.";
  5.         } else {
  6.             echo "Your message could not be sent at this time. Please try again.";
  7.         }
  8.  
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

Expand|Select|Wrap|Line Numbers
  1.       mail($to,$subject,$message,$headers) or die("Mail function failed");
  2.  
Try this...
Aug 17 '09 #3

P: 69
Thanks for the reply, i have used a bit of ajax that is not shown here it is
Expand|Select|Wrap|Line Numbers
  1. <style>
  2.         #errors
  3.         {
  4.  
  5.             font-size:14px;
  6.             font-weight:normal;
  7.             margin:0px;
  8.             padding:0px;
  9.         }
  10.     </style>
  11.  
  12.     <script type="text/javascript" src="js/prototype.js"></script>
  13.     <script type="text/javascript">
  14.  
  15.     function sendRequest() {
  16.  
  17.         new Ajax.Request("email_form_process.php", {
  18.                method: 'post',
  19.                postBody: "name="+$F("name")+"&email="+$F("email")+"&message="+$F("message"),
  20.                onComplete: showResponse
  21.  
  22.         });
  23.     }
  24.  
  25.  
  26.     function showResponse(req)
  27.     {
  28.        $('errors').style.borderStyle= "solid";
  29.  
  30.        var res=/message was received/;
  31.  
  32.        if(req.responseText.match(res))
  33.        {
  34.         $('errors').style.borderColor= "green";
  35.         $('errors').style.color="green";
  36.        }else{
  37.         $('errors').style.borderColor= "red";
  38.         $('errors').style.color="red";
  39.        }
  40.  
  41.        $('errors').style.borderSize= "1px";
  42.        $('errors').innerHTML=  req.responseText;
  43.     }
  44.  
  45.     </script>
  46.  
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.
Aug 17 '09 #4

gopan
P: 41
ooh...
check your first code php script line#49

Expand|Select|Wrap|Line Numbers
  1. $headers = "From: ".$from."\r\n";
  2.  
to

Expand|Select|Wrap|Line Numbers
  1. $headers = "From: ".$name." <".$from.">\r\n";
  2.  
then you will get name of the sender...
Hope this will help...
Aug 17 '09 #5

P: 69
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. $to = "example@example.co.uk";
  3.  
itz not a serious thing but it would make life a bit easier.
Aug 17 '09 #6

Dheeraj Joshi
Expert 100+
P: 1,123
I think you can have $to as an array...

Regards
Dheeraj Joshi
Aug 18 '09 #7

gopan
P: 41
@dheerajjoshim
No dear... php mail function can mail to multiple recipients by giving all the addresses as a coma seperated string... thats all...

Expand|Select|Wrap|Line Numbers
  1.     $to = "example@example.co.uk, example@example.co.au, example@example.co.ar";
  2.  
Aug 18 '09 #8

P: 69
Thank you this helps alot.
Aug 18 '09 #9

Dheeraj Joshi
Expert 100+
P: 1,123
Thanks for the info Gopan..

I did not know that..


Regards
Dheeraj Joshi
Aug 19 '09 #10

Post your reply

Sign in to post your reply or Sign up for a free account.