php mail function not working? why?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • impin
    New Member
    • Jul 2010
    • 127

    php mail function not working? why?

    this is my contact form.html

    Code:
     <form id="contacts-form" method="post" action="mail.php" onSubmit="return validateForm(this)">
                                                 <fieldset>
                                                    <div class="field"><label>Full Name:</label><input type="text" value="" name="name"/> <font color="#FF0000" size="+1">*</font></div> 
                                                    <div class="field"><label>E-mail:</label><input type="text" value="" name="email"/> <font color="#FF0000" size="+1">*</font></div>
                                                    <div class="field"><label>Phone:</label><input type="text" value="" name="phone"/> <font color="#FF0000" size="+1"></font></div>
                                                    <div class="field"><label>Subject:</label><input type="text" value="" name="subject"/></font></div>
                                                   
                                                    <div class="field"><label>Services:</label><select name="services">
      <option>------------Select------------</option>                                              
      <option>Call Center Services</option>
      <option>Data Entry Services</option>
      <option>KPO Services</option>
      <option>Health Care Services</option>
      <option>Software Development</option>
    </select> 
      <font color="#FF0000" size="+1">*</font>
    </div>
                                                    <div class="field"><label>Message:</label><textarea name="message" cols="" rows=""></textarea> </div>
                                                                                                  
                                                    <div align="center"><input type="submit" value="SUBMIT"/></font></div>
                                                    
                                                    <!--<div class="alignright"><a href="mail.php" class="button" onclick="document.getElementById('contacts-form').submit()"><em><b>Submit</b></em></a>
                                                    </div>-->
                                                 </fieldset>
                                              </form>

    this is my mail.php

    Code:
    <?php
    //if(isset($_POST['email'])) {
    	
    	
    	$email_to = "lark.imp@gmail.com "; //mail id
    	$email_subject = "Outsource Projects to India";
    	
    	
    	$name = $_POST['name']; 
    	$email = $_POST['email'];
    	$phone = $_POST['phone']; 
    	$services = $_POST['services'];
    	$message = $_POST['message'];
    	
    			
    	$error_message = "";
    	
    	$email_message = "Form details below.\n\n";
    	
    	function clean_string($string) {
    	  $bad = array("content-type","bcc:","to:","cc:","href");
    	  return str_replace($bad,"",$string);
    	//}
    	
    	$email_message .= "Name: ".clean_string($name)."\n";
    	$email_message .= "Email: ".clean_string($email)."\n";
    	$email_message .= "Phone: ".clean_string($phone)."\n";
    	$email_message .= "Services: ".clean_string($services)."\n";
    	$email_message .= "Message: ".clean_string($message)."\n";
    	
    
    // create email headers
    $headers = 'From: '.$email."\r\n".
    'Reply-To: '.$email."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);  
    ?>
    <?php
    }
    ?>
    is ther anything wrong? plz help
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you could say the @ is wrong, as it prevents you to receive any errors mail() may encounter. you also don’t check mail()’s return value to see if the call was successful.

    Comment

    • impin
      New Member
      • Jul 2010
      • 127

      #3
      ya. i tested with the script, when i click submit it goes to the thank you page without any error. but i am not recieving any mails...

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        you cannont get any errors, because you suppress them.

        Comment

        • impin
          New Member
          • Jul 2010
          • 127

          #5
          oh, then what i have to do?

          Comment

          • impin
            New Member
            • Jul 2010
            • 127

            #6
            i tried with different script..
            this is my form.

            Code:
            <form name="contactform" method="post" action="send_form_email.php">
            <table width="450px">
            </tr>
            <tr>
             <td valign="top">
              <label for="first_name">First Name *</label>
             </td>
             <td valign="top">
              <input  type="text" name="first_name" maxlength="50" size="30">
             </td>
            </tr>
            
            <tr>
             <td valign="top">
              <label for="last_name">Last Name *</label>
             </td>
             <td valign="top">
              <input  type="text" name="last_name" maxlength="50" size="30">
             </td>
            </tr>
            <tr>
             <td valign="top">
              <label for="email">Email Address *</label>
             </td>
             <td valign="top">
              <input  type="text" name="email" maxlength="80" size="30">
             </td>
            
            </tr>
            <tr>
             <td valign="top">
              <label for="telephone">Telephone Number</label>
             </td>
             <td valign="top">
              <input  type="text" name="telephone" maxlength="30" size="30">
             </td>
            </tr>
            <tr>
             <td valign="top">
              <label for="comments">Comments *</label>
             </td>
             <td valign="top">
              <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>
             </td>
            
            </tr>
            <tr>
             <td colspan="2" style="text-align:center">
              <input type="submit" value="Submit">   <a href="http://www.freecontactform.com/email_form.php">Email Form</a>
             </td>
            </tr>
            </table>
            </form>
            this is my send_form_email .php

            Code:
            <?php
            if(isset($_POST['email'])) {
            	
            	// EDIT THE 2 LINES BELOW AS REQUIRED
            	$email_to = "impin@in.com.com";
            	$email_subject = "Your email subject line";
            	
            	
            	function died($error) {
            		// your error code can go here
            		echo "We are very sorry, but there were error(s) found with the form your submitted. ";
            		echo "These errors appear below.<br /><br />";
            		echo $error."<br /><br />";
            		echo "Please go back and fix these errors.<br /><br />";
            		die();
            	}
            	
            	// validation expected data exists
            	if(!isset($_POST['first_name']) ||
            		!isset($_POST['last_name']) ||
            		!isset($_POST['email']) ||
            		!isset($_POST['telephone']) ||
            		!isset($_POST['comments'])) {
            		died('We are sorry, but there appears to be a problem with the form your submitted.');		
            	}
            	
            	$first_name = $_POST['first_name']; // required
            	$last_name = $_POST['last_name']; // required
            	$email_from = $_POST['email']; // required
            	$telephone = $_POST['telephone']; // not required
            	$comments = $_POST['comments']; // required
            	
            	$error_message = "";
            	$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
              if(!eregi($email_exp,$email_from)) {
              	$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
              }
            	$string_exp = "^[a-z .'-]+$";
              if(!eregi($string_exp,$first_name)) {
              	$error_message .= 'The First Name you entered does not appear to be valid.<br />';
              }
              if(!eregi($string_exp,$last_name)) {
              	$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
              }
              if(strlen($comments) < 2) {
              	$error_message .= 'The Comments you entered do not appear to be valid.<br />';
              }
              $string_exp = "^[0-9 .-]+$";
              if(!eregi($string_exp,$telephone)) {
              	$error_message .= 'The Telphone Number you entered does not appear to be valid.<br />';
              }
              if(strlen($error_message) > 0) {
              	died($error_message);
              }
            	$email_message = "Form details below.\n\n";
            	
            	function clean_string($string) {
            	  $bad = array("content-type","bcc:","to:","cc:","href");
            	  return str_replace($bad,"",$string);
            	}
            	
            	$email_message .= "First Name: ".clean_string($first_name)."\n";
            	$email_message .= "Last Name: ".clean_string($last_name)."\n";
            	$email_message .= "Email: ".clean_string($email_from)."\n";
            	$email_message .= "Telephone: ".clean_string($telephone)."\n";
            	$email_message .= "Comments: ".clean_string($comments)."\n";
            	
            	
            // create email headers
            $headers = 'From: '.$email_from."\r\n".
            'Reply-To: '.$email_from."\r\n" .
            'X-Mailer: PHP/' . phpversion();
            mail($email_to, $email_subject, $email_message, $headers);  
            ?>
            
            <!-- include your own success html here -->
            
            Thank you for contacting us. We will be in touch with you very soon.
            
            <?php
            }
            ?>
            i get the message "Thank you for contacting us. We will be in touch with you very soon.". but i wont receive any mails....

            Comment

            • impin
              New Member
              • Jul 2010
              • 127

              #7
              i tried with a simple form

              Code:
              <form action="exmail.php" method="POST">
              <b>Email</b><br>
              <input type="text" name="email" size=40>
              <p><b>Subject</b><br>
              <input type="text" name="subject" size=40>
              <p><b>Message</b><br>
              <textarea cols=40 rows=10 name="message"></textarea>
              <p><input type="submit" value=" Send ">
              </form>
              this is my simple php script

              Code:
              <?php
              
              /* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
              $email = $_POST['email'];
              $subject = $_POST['subject'];
              $message = $_POST['message'];
              
              /* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn't empty. preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see PHP manual for details. */
              if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
                echo "<h4>Invalid email address</h4>";
                echo "<a href='javascript:history.back(1);'>Back</a>";
              } elseif ($subject == "") {
                echo "<h4>No subject</h4>";
                echo "<a href='javascript:history.back(1);'>Back</a>";
              }
              
              /* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
              elseif (mail($email,$subject,$message)) {
                echo "<h4>Thank you for sending email</h4>";
              } else {
                echo "<h4>Can't send email to $email</h4>";
              }
              ?>
              i get result as " Can't send email to <email address>".

              i dont what the problem is?

              i just upload the script into my website and tested...

              when i test the script in my local it shows the error
              " Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()"
              .
              what shall i do? plz help...

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                check your mail settings whether the meet the requirements

                Comment

                • impin
                  New Member
                  • Jul 2010
                  • 127

                  #9
                  this is my php.ini file.

                  Code:
                  [mail function]
                  ; For Win32 only.
                  ; http://php.net/smtp
                  SMTP = localhost
                  ; http://php.net/smtp-port
                  smtp_port = 25
                  
                  ; For Win32 only.
                  ; http://php.net/sendmail-from
                  sendmail_from = you@yourdomain
                  i hav to change anything here?

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    looks sensible, though …

                    do you have a smtp service running in windows?

                    Comment

                    • impin
                      New Member
                      • Jul 2010
                      • 127

                      #11
                      i dont know about that. my doubt is, when my website is online each user access from different systems, that means each system must have the smtp service?
                      its very confusing... i am sorry i am new to this...

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        nope, php looks for an smtp service on the computer, PHP is installed on, i.e. the host computer (yours)

                        Comment

                        • impin
                          New Member
                          • Jul 2010
                          • 127

                          #13
                          that means that php installed computer must have smtp service then only the mail script will work when we upload it into the website...?

                          what i have done is...
                          its a simple php script. i wrote script in notepad and save it as mail.php and i just upload it into my website... in my system i didnt install php.

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            that means that php installed computer must have smtp service then only the mail script will work when we upload it into the website...?
                            that depends on the OS of the webserver that’s hosting the script. *NIX type OS only require the sendmail library.

                            Comment

                            • impin
                              New Member
                              • Jul 2010
                              • 127

                              #15
                              oh that means the webserver must support smtp services right?

                              Comment

                              Working...