0

I'm trying to send an email using php, but when I try it on browser I get error:

"The server failed to send the message. Please try again later."

Here is my php file

<?php
if( isset($_GET['n']) && isset($_GET['e']) && isset($_GET['m']) ){
        $n = $_GET['n']; // HINT: use preg_replace() to filter the data
        $e = $_GET['e'];
        $m = nl2br($_GET['m']);
        $to = "amal.soltni@esprit.tn";
        $from = $e;
        $subject = 'Contact Form Message';
        $message = '<b>Name:</b> '.$n.' <br><b>Email:</b> '.$e.' <p>'.$m.'</p>';
        $headers = "From: $from\n";
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1\n";
        if( mail($to, $subject, $message, $headers) ){
                echo "success";
        } else {
                echo "The server failed to send the message. Please try again later.";
        }
}
?>
Ndroid21
  • 400
  • 1
  • 8
  • 19
Amal
  • 75
  • 1
  • 2
  • 11

2 Answers2

1

Try This one to send the mail and use live server to execute this code, don't use localhost to send the mail

    <?php
    if( isset($_GET['n']) && isset($_GET['e']) && isset($_GET['m']) ){
    $n = $_GET['n']; 
    $e = $_GET['e'];
    $m = nl2br($_GET['m']);
    $to = "amal.soltni@esprit.tn";
    $from = $e;
    $subject = "My subject";
    $message = '<b>Name:</b> '.$n.' <br><b>Email:</b> '.$e.' <p>'.$m.'</p>';;
    $headers = 'From: .$from.' . '\r\n' .
    $send = mail($to,$subject,$message,$headers);
   }else {
      echo "The server failed to send the message. Please try again later.";
    }
    ?>
Tausif Anwar
  • 1,237
  • 1
  • 10
  • 21
-1

Please use this following code.

   <?php
        if (isset($_POST["submit"]))
    {
        $n = $_POST['n'];
        $e = $_POST['e'];
        $m = $_POST['m'];
        $to = "amal.soltni@esprit.tn";
        $from = $e;
        $subject = 'Contact Form Message';
        $message = '<b>Name:</b> '.$n.' <br><b>Email:</b> '.$e.' <p>'.$m.'</p>';
        $headers = "From: $from\n";
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1\n";
        if( mail($to, $subject, $message, $headers) ){
                echo "success";
        } else {
                echo "The server failed to send the message. Please try again later.";
        }
    }

  ?>

  <form method="post" >
  <input type="text" name="n" placeholder="Name"><br>
  <input type="email" name="e" placeholder="Email"><br>
  <textarea col="10" rows="5"  name="m" placeholder="Message"></textarea><br>
  <input type="submit" name="submit">
  </form>
RichestSoft
  • 156
  • 1
  • 4