简体   繁体   中英

I can't send mail using PHP mail function… I am getting an error

I am unable to send mail using the PHP mail() function. I am getting an error message. What's wrong in it? Please, help me...

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 E:\\xampp\\htdocs\\SimpleEmail.php on line 14

Email could not be sent.

 <html>
          <head>
            <title>Simple Send Mail Form</title>
        </head>
         <body>
          <h1>Mail Form</h1>
        <form name="form1" method="post" action="SimpleEmail.php">
       <table>
         <tr><td><b>To</b></td><td><input type="text" name="mailto" size="35"></td></tr>
            <tr><td><b>Subject</b></td>
      <td><input type="text" name="mailsubject" size="35"></td></tr>
        <tr><td><b>Message</b></td>
      <td><textarea name="mailbody" cols="50" rows="7"></textarea></td>
      </tr>
         <tr><td colspan="2">
        <input type="submit" name="Submit" value="Send">
      </td>
        </tr>
        </table>
   </form>
      </body>
     </html>  

      //SimpleEmail.php//

         <?php
      if (empty ($mailto) ) {
    die ( "Recipient is blank! ") ;
    }

if (empty ($mailsubject) ){
   $mailsubject=" " ;
}

if (empty ($mailbody) ) {
   $mailbody=" " ; 
}

$result = mail ($mailto, $mailsubject, $mailbody) ;

if ($result) {
   echo "Email sent successfully!" ;
}else{
   echo "Email could not be sent." ;
}
?>

You aren't running your own mail server and aren't configured to use anyone else's. So where would the mail go? Talk to the system administrator or hosting provider.

the error message mean you need to install SMTP server to let the mail function work normally .
since you using xampp you may configure Mercury Server which comes with XAMPP ..
once you install it and configure it the mail function will work normally ..

不用担心...在我们的本地服务器中,许多服务器不是基于SMTP(简单邮件传输协议)运行的,因此我们不发送邮件,但是某些服务器可以在该服务器上运行,如果您将其配置为SMTP协议端口,则可以也寄给他们

You can try to test your PHP mail script in your localhost by follow given steps in this tutorial. http://blogs.bigfish.tv/adam/2009/12/03/setup-a-testing-mail-server-using-php-on-mac-os-x/

May it will help you.

Use following code to send an email

ini_set("SMTP","aspmx.l.google.com");
$to = "yourmailid@gmail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "toemail@gmail.com";
$headers  = 'From: '.$from. "\r\n" .
            'Reply-To: '.$from. "\r\n" .
            'MIME-Version: 1.0' . "\r\n" .
            'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();
if(mail($to,$subject,$message,$headers)) echo "Mail Sent.";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM