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
  • 715
  • 1
  • 10
  • 25
  • are you testing this locally or on your website? – herriekrekel Sep 14 '15 at 13:59
  • From the local host, I have saved it as email.php – Jack Sep 14 '15 at 14:00
  • There are tester programmes with which you test if emails are sent. After that problem is not in your code and have to ask your ISP, or use middleware services, such as sendgrid ;) – DDeme Sep 14 '15 at 14:00
  • 1
    @Mary E yeay email usually doesn't work locally unless your willing to put in alot of afford i suggest just test it on your website your code should work – herriekrekel Sep 14 '15 at 14:01
  • http://stackoverflow.com/questions/5335273/how-to-send-an-email-using-php – Nana Partykar Sep 14 '15 at 14:04
  • @herriekrekel,thanks so it is not working because I am running it on a local server – Jack Sep 14 '15 at 14:08
  • @MaryE yup it's always a pain getting it to work on a local server if you just upload the file to your website it should work and save you some time in my opinion – herriekrekel Sep 14 '15 at 14:09

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,333
  • 3
  • 31
  • 48