473,387 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

php mail function not working? why?

127 100+
this is my contact form.html

Expand|Select|Wrap|Line Numbers
  1.  <form id="contacts-form" method="post" action="mail.php" onSubmit="return validateForm(this)">
  2.                                              <fieldset>
  3.                                                 <div class="field"><label>Full Name:</label><input type="text" value="" name="name"/> <font color="#FF0000" size="+1">*</font></div> 
  4.                                                 <div class="field"><label>E-mail:</label><input type="text" value="" name="email"/> <font color="#FF0000" size="+1">*</font></div>
  5.                                                 <div class="field"><label>Phone:</label><input type="text" value="" name="phone"/> <font color="#FF0000" size="+1"></font></div>
  6.                                                 <div class="field"><label>Subject:</label><input type="text" value="" name="subject"/></font></div>
  7.  
  8.                                                 <div class="field"><label>Services:</label><select name="services">
  9.   <option>------------Select------------</option>                                              
  10.   <option>Call Center Services</option>
  11.   <option>Data Entry Services</option>
  12.   <option>KPO Services</option>
  13.   <option>Health Care Services</option>
  14.   <option>Software Development</option>
  15. </select> 
  16.   <font color="#FF0000" size="+1">*</font>
  17. </div>
  18.                                                 <div class="field"><label>Message:</label><textarea name="message" cols="" rows=""></textarea> </div>
  19.  
  20.                                                 <div align="center"><input type="submit" value="SUBMIT"/></font></div>
  21.  
  22.                                                 <!--<div class="alignright"><a href="mail.php" class="button" onclick="document.getElementById('contacts-form').submit()"><em><b>Submit</b></em></a>
  23.                                                 </div>-->
  24.                                              </fieldset>
  25.                                           </form>
  26.  
  27.  

this is my mail.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //if(isset($_POST['email'])) {
  3.  
  4.  
  5.     $email_to = "lark.imp@gmail.com "; //mail id
  6.     $email_subject = "Outsource Projects to India";
  7.  
  8.  
  9.     $name = $_POST['name']; 
  10.     $email = $_POST['email'];
  11.     $phone = $_POST['phone']; 
  12.     $services = $_POST['services'];
  13.     $message = $_POST['message'];
  14.  
  15.  
  16.     $error_message = "";
  17.  
  18.     $email_message = "Form details below.\n\n";
  19.  
  20.     function clean_string($string) {
  21.       $bad = array("content-type","bcc:","to:","cc:","href");
  22.       return str_replace($bad,"",$string);
  23.     //}
  24.  
  25.     $email_message .= "Name: ".clean_string($name)."\n";
  26.     $email_message .= "Email: ".clean_string($email)."\n";
  27.     $email_message .= "Phone: ".clean_string($phone)."\n";
  28.     $email_message .= "Services: ".clean_string($services)."\n";
  29.     $email_message .= "Message: ".clean_string($message)."\n";
  30.  
  31.  
  32. // create email headers
  33. $headers = 'From: '.$email."\r\n".
  34. 'Reply-To: '.$email."\r\n" .
  35. 'X-Mailer: PHP/' . phpversion();
  36. @mail($email_to, $email_subject, $email_message, $headers);  
  37. ?>
  38. <?php
  39. }
  40. ?>
  41.  
is ther anything wrong? plz help
Aug 24 '10 #1
18 3061
Dormilich
8,658 Expert Mod 8TB
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.
Aug 24 '10 #2
impin
127 100+
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...
Aug 24 '10 #3
Dormilich
8,658 Expert Mod 8TB
you cannont get any errors, because you suppress them.
Aug 24 '10 #4
impin
127 100+
oh, then what i have to do?
Aug 24 '10 #5
impin
127 100+
i tried with different script..
this is my form.

Expand|Select|Wrap|Line Numbers
  1. <form name="contactform" method="post" action="send_form_email.php">
  2. <table width="450px">
  3. </tr>
  4. <tr>
  5.  <td valign="top">
  6.   <label for="first_name">First Name *</label>
  7.  </td>
  8.  <td valign="top">
  9.   <input  type="text" name="first_name" maxlength="50" size="30">
  10.  </td>
  11. </tr>
  12.  
  13. <tr>
  14.  <td valign="top">
  15.   <label for="last_name">Last Name *</label>
  16.  </td>
  17.  <td valign="top">
  18.   <input  type="text" name="last_name" maxlength="50" size="30">
  19.  </td>
  20. </tr>
  21. <tr>
  22.  <td valign="top">
  23.   <label for="email">Email Address *</label>
  24.  </td>
  25.  <td valign="top">
  26.   <input  type="text" name="email" maxlength="80" size="30">
  27.  </td>
  28.  
  29. </tr>
  30. <tr>
  31.  <td valign="top">
  32.   <label for="telephone">Telephone Number</label>
  33.  </td>
  34.  <td valign="top">
  35.   <input  type="text" name="telephone" maxlength="30" size="30">
  36.  </td>
  37. </tr>
  38. <tr>
  39.  <td valign="top">
  40.   <label for="comments">Comments *</label>
  41.  </td>
  42.  <td valign="top">
  43.   <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>
  44.  </td>
  45.  
  46. </tr>
  47. <tr>
  48.  <td colspan="2" style="text-align:center">
  49.   <input type="submit" value="Submit">   <a href="http://www.freecontactform.com/email_form.php">Email Form</a>
  50.  </td>
  51. </tr>
  52. </table>
  53. </form>
  54.  
  55.  
this is my send_form_email.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if(isset($_POST['email'])) {
  3.  
  4.     // EDIT THE 2 LINES BELOW AS REQUIRED
  5.     $email_to = "impin@in.com.com";
  6.     $email_subject = "Your email subject line";
  7.  
  8.  
  9.     function died($error) {
  10.         // your error code can go here
  11.         echo "We are very sorry, but there were error(s) found with the form your submitted. ";
  12.         echo "These errors appear below.<br /><br />";
  13.         echo $error."<br /><br />";
  14.         echo "Please go back and fix these errors.<br /><br />";
  15.         die();
  16.     }
  17.  
  18.     // validation expected data exists
  19.     if(!isset($_POST['first_name']) ||
  20.         !isset($_POST['last_name']) ||
  21.         !isset($_POST['email']) ||
  22.         !isset($_POST['telephone']) ||
  23.         !isset($_POST['comments'])) {
  24.         died('We are sorry, but there appears to be a problem with the form your submitted.');        
  25.     }
  26.  
  27.     $first_name = $_POST['first_name']; // required
  28.     $last_name = $_POST['last_name']; // required
  29.     $email_from = $_POST['email']; // required
  30.     $telephone = $_POST['telephone']; // not required
  31.     $comments = $_POST['comments']; // required
  32.  
  33.     $error_message = "";
  34.     $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
  35.   if(!eregi($email_exp,$email_from)) {
  36.       $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  37.   }
  38.     $string_exp = "^[a-z .'-]+$";
  39.   if(!eregi($string_exp,$first_name)) {
  40.       $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  41.   }
  42.   if(!eregi($string_exp,$last_name)) {
  43.       $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  44.   }
  45.   if(strlen($comments) < 2) {
  46.       $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  47.   }
  48.   $string_exp = "^[0-9 .-]+$";
  49.   if(!eregi($string_exp,$telephone)) {
  50.       $error_message .= 'The Telphone Number you entered does not appear to be valid.<br />';
  51.   }
  52.   if(strlen($error_message) > 0) {
  53.       died($error_message);
  54.   }
  55.     $email_message = "Form details below.\n\n";
  56.  
  57.     function clean_string($string) {
  58.       $bad = array("content-type","bcc:","to:","cc:","href");
  59.       return str_replace($bad,"",$string);
  60.     }
  61.  
  62.     $email_message .= "First Name: ".clean_string($first_name)."\n";
  63.     $email_message .= "Last Name: ".clean_string($last_name)."\n";
  64.     $email_message .= "Email: ".clean_string($email_from)."\n";
  65.     $email_message .= "Telephone: ".clean_string($telephone)."\n";
  66.     $email_message .= "Comments: ".clean_string($comments)."\n";
  67.  
  68.  
  69. // create email headers
  70. $headers = 'From: '.$email_from."\r\n".
  71. 'Reply-To: '.$email_from."\r\n" .
  72. 'X-Mailer: PHP/' . phpversion();
  73. mail($email_to, $email_subject, $email_message, $headers);  
  74. ?>
  75.  
  76. <!-- include your own success html here -->
  77.  
  78. Thank you for contacting us. We will be in touch with you very soon.
  79.  
  80. <?php
  81. }
  82. ?>
  83.  
  84.  
i get the message "Thank you for contacting us. We will be in touch with you very soon.". but i wont receive any mails....
Aug 24 '10 #6
impin
127 100+
i tried with a simple form

Expand|Select|Wrap|Line Numbers
  1. <form action="exmail.php" method="POST">
  2. <b>Email</b><br>
  3. <input type="text" name="email" size=40>
  4. <p><b>Subject</b><br>
  5. <input type="text" name="subject" size=40>
  6. <p><b>Message</b><br>
  7. <textarea cols=40 rows=10 name="message"></textarea>
  8. <p><input type="submit" value=" Send ">
  9. </form>
  10.  
this is my simple php script

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. /* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
  4. $email = $_POST['email'];
  5. $subject = $_POST['subject'];
  6. $message = $_POST['message'];
  7.  
  8. /* 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. */
  9. if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
  10.   echo "<h4>Invalid email address</h4>";
  11.   echo "<a href='javascript:history.back(1);'>Back</a>";
  12. } elseif ($subject == "") {
  13.   echo "<h4>No subject</h4>";
  14.   echo "<a href='javascript:history.back(1);'>Back</a>";
  15. }
  16.  
  17. /* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
  18. elseif (mail($email,$subject,$message)) {
  19.   echo "<h4>Thank you for sending email</h4>";
  20. } else {
  21.   echo "<h4>Can't send email to $email</h4>";
  22. }
  23. ?>
  24.  
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...
Aug 24 '10 #7
Dormilich
8,658 Expert Mod 8TB
check your mail settings whether the meet the requirements
Aug 24 '10 #8
impin
127 100+
this is my php.ini file.

Expand|Select|Wrap|Line Numbers
  1.  
  2. [mail function]
  3. ; For Win32 only.
  4. ; http://php.net/smtp
  5. SMTP = localhost
  6. ; http://php.net/smtp-port
  7. smtp_port = 25
  8.  
  9. ; For Win32 only.
  10. ; http://php.net/sendmail-from
  11. sendmail_from = you@yourdomain
  12.  
  13.  
i hav to change anything here?
Aug 24 '10 #9
Dormilich
8,658 Expert Mod 8TB
looks sensible, though …

do you have a smtp service running in windows?
Aug 24 '10 #10
impin
127 100+
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...
Aug 25 '10 #11
Dormilich
8,658 Expert Mod 8TB
nope, php looks for an smtp service on the computer, PHP is installed on, i.e. the host computer (yours)
Aug 25 '10 #12
impin
127 100+
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.
Aug 25 '10 #13
Dormilich
8,658 Expert Mod 8TB
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.
Aug 25 '10 #14
impin
127 100+
oh that means the webserver must support smtp services right?
Aug 25 '10 #15
Dormilich
8,658 Expert Mod 8TB
no. only windows servers must.
Aug 25 '10 #16
impin
127 100+
oh. so what should i do? should i contact the web host providers? they are the one's should know about this? or else i have to do something in myside?

now my website is online. but the mails only not going...

Expand|Select|Wrap|Line Numbers
  1. if (mail($email_to, $email_subject, $email_message, $headers))
  2. {
  3.     echo "<h4>Thank you for sending email</h4>";
  4. } else {
  5.   echo "<h4>Can't send email</h4>";
  6. }
  7. ?>
  8.  
i get result as "i cant send email".
Aug 26 '10 #17
Dormilich
8,658 Expert Mod 8TB
should i contact the web host providers?
yes .
Aug 26 '10 #18
impin
127 100+
thank you.
Aug 26 '10 #19

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

Similar topics

7
by: Jim Seymour | last post by:
I have a contact form that uses PHP's mail() function. It recently came to my attention that (some?) MS-Windows mail servers may not properly process data given to the mail() function that's only...
4
by: Data Goob | last post by:
This one has me stumped. How did SuSE disable the mail() function in their RPM'd version of PHP? ( This is their install-version of PHP with RPMs not what you download from PHP.net ) I have...
1
by: tzuccolo2001 | last post by:
I'm trying to send an email using the mail() function. I've carefully made use of the ini_set(SMTP, "mail.my.server") function to set my SMTP server and also to set my From: address. I'm still...
0
by: chris.huh | last post by:
I have setup Apache and PHP on my local machine as a testing server. So to test the mail() function i have set the SMTP_host thing in php.ini to what i think is the hotmail SMTP server. Whenever...
2
by: Bandul | last post by:
Hi everyone I am trying to send a e-mail using the mail() function. Now i try to send more than one data from mysql database so i must use example $results=mysql_num_rows($result_query)...
11
by: Lieven | last post by:
Hey, I had a hard disc problem last week on my server. I replaced the disc and copied al the files to the new hard disc, everything works fine again except some php scripts that are using the...
5
by: maya | last post by:
I'm learning how to send mail with php... only thing that mystifies me a bit is I can't get NAME of sender to appear in e-mail INBOX under "from"... I only see e-mail address.. I'm coming from...
9
by: shror | last post by:
hi every body, i have a problem which is when i was checking my mail() form it work fine but the problem is that the form configuration is not set correctly, in details: the from var is not...
8
by: anandaraman | last post by:
Hi all, i want to send mail through php coding.but error displays. i am working in windows environment. please help.. PHP Coding: <?php $to = "anandh@yahoo.com"; $subject = "Example"; $txt =...
1
prabirchoudhury
by: prabirchoudhury | last post by:
mail() function stopped working after server upgraded to php 4.4.4. not getting any error on server error-log file and the php page . any idea please. using mail function code $to =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.