0

I'm learning php and I'm trying to send an email to myself using an html form but it doesn't works.

    <form action="index.php" role="form" method="post" name="emailform">
                        <div class="form-group">
                            <label for="email">Email address:</label>
                            <input type="email" class="form-control" id="email" name="email">
                        </div>
                        <div class="form-group">
                            <label for="comment">Text:</label>
                            <textarea type="textarea" class="form-control" id="textarea" rows="5" name="textarea"></textarea>
                        </div>
                        <button type="submit" class="btn btn-default" id="submit" name="submit">Submit</button>
                    </form>

                    <?php
                        function email()
                            {
                            $to = 'my_mail';
                            $message = $_POST['textarea'];
                            $from = $_POST['email'];
                            $subject = 'Portfolio';
                            $mail_headers = "From: " .  $from . " <" .  $from . ">\r\n";
                            $mail_headers .= "Reply-To: " .  $from . "\r\n";
                            $mail_headers .= "X-Mailer: PHP/" . phpversion();

                            echo $to . " " . $message . " " . $from;
                            if(@mail($to, $subject, $message, $mail_headers))
                                echo @mail($to, $subject, $message, $mail_headers);
                            else echo "ERROR";
                            }

if(isset($_POST['submit']))
email();
?>

my_mail is my mail (I replaced it here but in the code there is my real email). The code seem to work, in fact it display the echo @mail but the mail doesn't appear in my inbox

Stefano Pisano
  • 440
  • 1
  • 8
  • 24

2 Answers2

0

The code looks fine, but I think because of @ in the mail function you are not seeing any errors. If you don't want to display error you can use it like this:

<?php
if(@mail($to, $subject, $message, $mail_headers)){
    echo "Mail Sent!";
}else{
    print_r(error_get_last());
}
?>

In this way the error is not thrown but you can use error_get_last() to see the error and log it if you want.

PS you are using mail function 2 times, so mail would be sent two times when it is going to work.

Ghulam Ali
  • 1,935
  • 14
  • 15
0

here is the correct way.

<form action="contact.php"  etc... etc..>
blah blah blah
</form>

Then create a new php file (contact.php) and use the following

<?php

$field_name = $_POST["cname"];/* change " " according to your form */
$field_email = $_POST["cmail"];
$field_sub = $_POST["csub"];
$field_message = $_POST["cmsg"];

$to = "mailid1@mail.com, mailid2@mail.com";
$subject = " give subject" ;
$message = "message";

if(mail($to,$subject,$message))
 {
      echo "<script>alert('Your Message was sent Successfully. Thank You For Your Time !');</script>";
      }
      else
      {
      echo "<script>alert('Something wrong happened. Please try again later. Sorry For The Trouble');                   </script>";
      }


?>
<meta http-equiv="refresh" content="2; url=contact.html"> 
<!-- for coming back to intial page -->
Ijas
  • 93
  • 1
  • 11