0

Given the following php code:

 <html>
     <body>
     <?php 
         $fname ="First Name = ".$_REQUEST['fname']."\n"; 
         $lname ="Last Name = ".$_REQUEST['lname']."\n";
         $breakfast = "Desired Breakfast = ".$_REQUEST['breakfast']."\n";
         $floor ="Desired Floor = ".$_REQUEST['floor']."\n"; 
         $to = "XXXX@gmail.com";
         $email="XXXX@gmail.com";
         $subject="shenkar php form from erez geva site";
         $body =$fname.$lname.$breakfast.$floor;
         if (mail($to, $subject, $body, "From: $email")){
             header("Location: http://localhost:9090/sent.html");
             exit();
         }else{
             echo("<p>Message delivery failed...</p>");
         } 
     ?>
     </body>
 </html>

Does not work. The Index HTML file which the user picks the desired options from is well written (accouring to me professor), so lets assume the problem is elsewhere. If I go directly to that php file : localhost\send.php I get this error:

Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\send.php on line 12

What is the meanning of it?

Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
izac89
  • 3,790
  • 7
  • 30
  • 46
  • ** I am using Wamp 2.2e – izac89 Jun 04 '13 at 09:26
  • 2
    Well, let's see... do you have a mail server running at localhost and listening on port 25? – Jon Jun 04 '13 at 09:27
  • 1
    This is because mail cannot be sent from localhost unless you have a mail service properly configured – Fabio Jun 04 '13 at 09:27
  • I know this and yet I forget every time I work locally! – Will Jun 04 '13 at 09:34
  • Don't use PHP's `mail()` function. It sucks. Use a decent mailer class like phpMailer. I can't say for sure whether that'll be enough to solve your problem this time round, but it will make things a lot easier to work with and to debug, not to mention all the additional features it brings. – Spudley Jun 04 '13 at 10:27
  • Port 25 = Spam, some servers even block you from sending email using this port – Lefsler Jun 04 '13 at 10:34

3 Answers3

1

Your script is trying to send an email but there is no email service properly configured on the local server.

if (mail($to, $subject, $body, "From: $email")){ This is the line that is causing the problem. The mail function is a PHP function which sends an email.

When your script reaches this call, PHP tries to connect to the mail service on your server which it is unable to do and so fails.

Most likely there is no mail service running, or it is possible that it is configured in a non-default manner. Either way this is one for your system admin.

ose
  • 4,065
  • 2
  • 24
  • 40
  • 1
    Go to your professor/system admin. Tell them that you want to send email from PHP. They will either: (a) tell you what the correct configuration is/fix the configuration for you, (b) install a mailserver for you, (c) tell you that it is not within their policy to allow students to send email. – ose Jun 04 '13 at 09:30
1

You will need to install a local mailserver in order to do this. If you want to send it to external e-mail addresses, it might end up in unwanted e-mails or it may not arrive at all.

A good mailserver which I use (I use it on Linux, but it's also available for Windows) is Axigen: http://www.axigen.com/mail-server/download/

You might need some experience with mailservers to install it, but once it works, you can do anything you want with it.

liyakat
  • 11,825
  • 2
  • 40
  • 46
-1

You cannot send email from your local server. Upload your files to a web server and then try to run this code. It will work.

CYRIL
  • 31
  • 1
  • 7