0

I am trying to send an email when the 'Send Email' button is pressed, but the following code won't send anything to my email:

<!DOCTYPE html>
<html>
<form action="email.php" method="post"> 
<input value="Send Email" name="email" type="submit">
</form>
 <?php
   if (isset($_POST['email'])) {
   $msg = 'hello';
   mail('example@gmail.com', 'Sample', $msg); 
                               }                              
 ?>
</html>

Any idea on how to make it work?

Jack
  • 725
  • 1
  • 10
  • 27

2 Answers2

1

Use the following PHP code to send the email

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?> 

Mail function will not work in Local server.You need to config SMTP on your local server. Take a look at this similar post,

php mail setup in xampp

Community
  • 1
  • 1
Santosh Jagtap
  • 995
  • 8
  • 17
0

try this

<?php
         $to = "someemail.com"
         $subject = "This is subject";

         $message = "<b>This is message.</b>";
         $message .= "<h1>This is headline.</h1>";

         $header = "From:abc@somedomain.com \r\n";

         $header .= "MIME-Version: 1.0\r\n";
         $header .= "Content-type: text/html\r\n";

         $retval = mail ($to,$subject,$message,$header);

         if( $retval == true )
         {
            echo "Message sent successfully...";
         }
         else
         {
            echo "Message could not be sent...";
         }
      ?>
Sid
  • 5,693
  • 3
  • 33
  • 50