0
    <?php
if (isset($_POST["send"])) {
  $to = $_POST["to"];
  $subject = $_POST["subject"];
  $message = $_POST["message"];
  $headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
  $headers = "From: your@email-address.com\r\n";
  if (mail($to, $subject, $message, $headers)) {
     echo "SUCCESS";
  } else {
     echo "ERROR";
  }
}
?>

This is my php code, I have a HTML form and $to, $subject, $message is the input fields of the form, the $_POST["send"] is the button through which I want to send emails after clicking that button

2 Answers2

0

You can't send emails from your localhost server. Get a free PHP server and upload your files using ftp. Following are some free php server providers where you can test your project.

Roshana Pitigala
  • 7,058
  • 8
  • 38
  • 66
0

That's true, you can't send emails from localhost, unless you were using a VPS configured with DNS + public IP + email server; If isn't your case, I would recommend sending emails through a SMTP server:

PHP Mail (php.ini configuration)
PHPMailer (library)

For the SMTP server you could use any provider that allows SMTP.

hutuh
  • 151
  • 5
  • I have edited my php.ini file and sendmail.ini according to smtp.gmail.com, so is PHPMailer is still mandatory? – Dipesh Karki Apr 26 '18 at 16:09
  • No, or you change the php.ini configuration to use a SMTP server or you use PHPMailer. It should work if your provider allows remote SMTP access. – hutuh Apr 26 '18 at 16:53