2

http://webprojects.co/dev_wed/mail.php trying to send mail from server using this script but i am not been able to send mail from server

<?php
$to = "test@gmail.com";
$subject = "HTML email";

$message = "
<html>
  <head>
    <title>HTML email</title>
  </head>
  <body>
  <p>This email contains HTML Tags!</p>
      <table>
        <tr>
         <th>Firstname</th>
         <th>Lastname</th>
        </tr>
        <tr>
          <td>John</td>
          <td>Doe</td>
        </tr>
      </table>
  </body>
  </html>
";


$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <webprojects@webprojects.com>' . "\r\n";
$headers .= 'Cc: webprojects@webprojects.com' . "\r\n";

$result = mail($to,$subject,$message,$headers);

if(!$result) {   
     echo "Error";   
} else {
    echo "Success";
}
?>
always display error trying to send email using php mail() function
Raj
  • 22,346
  • 14
  • 99
  • 142

1 Answers1

1

Are you testing the code online or via localhost? The mail() function doesn't work in localhost without SMTP.

  • i know that mail() not work on localhost. testing my code online at http://webprojects.co/dev_wed/mail.php – Patel Parth Jan 04 '17 at 11:05
  • I run the exact the same code with an success at dhjcastelijns.nl/index.php. Maybe it has something to do with your webserver? I had this problem once on my Raspberry Pi and it was because there was no mailserver installed. –  Jan 04 '17 at 11:07
  • so there is no problem with my code I guess I am facing the same issue here can you help me how can i install mail severer ? – Patel Parth Jan 04 '17 at 11:11
  • Get in contact with your host, alot of hosts have disabled the mail() function for anti-spam purposes. You might need to use SMTP instead. You can also check the SMTP configuration in your php.ini file, is the SMTP port set to 25? –  Jan 04 '17 at 11:16
  • I do not have access to php.ini file let me contact my host to check is mail() function is disabled or not.Thanks a lot buddy – Patel Parth Jan 04 '17 at 11:21
  • No problem. Goodluck - make sure to post the solution when you find it. –  Jan 04 '17 at 11:22
  • ok buddy will definitely do that.. – Patel Parth Jan 04 '17 at 11:33