0

I've got a form page on my website where the user has to enter his email ID. As soon as he does that an email should get triggered to him. I've used the mail() function but the email isn't going through. This is on a remote server. Am I missing out on something? Below is the PHP code that I'm using.

<?php
$to  = 'xyz@gmail.com';
$subject = 'Demo mail';
$message = 'This is a demo mail. Please reply to make sure the mail communication is okay.';
$headers = 'From: abc@gmail.com' . "\r\n" .
    'Reply-To: abc@gmail.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers)) {
   echo("<p>Email successfully sent!</p>");
  } else {
   echo("<p>Email delivery failed</p>");
  }
?>

  The output is Email delivery failed! What is it that I'm supposed to do apart from this? Thank you in advance!

Phantom
  • 320
  • 1
  • 5
  • 18

2 Answers2

0

mail() function relies on email configuration of the server the program is running on. You should take a look at your server email configuration, or use a library in order to hook up with an external SMTP service, like Gmail or one of your email servers. I recommend the Swift email library, which is well known, it's thoroughly tested and has a great community behind:

http://swiftmailer.org/

Alberto Fernández
  • 1,568
  • 13
  • 22
0

check php.ini config file for SMTP connection or add ini_set("SMTP", "IP_ADDRESS"); in your script.

you can also wrap your script with a error handler to debug the SMTP connection.

hackitect
  • 141
  • 1
  • 8