-2

I'm testing a PHP form to send email in local.

I put all input but when I press the submit button it always returns "false" and then the error message. Is that because I'm working in local and don't have any mail server, or is there something wrong in my code?

here the code:

<?php
    if(isset($_POST['submit']))
    {
        if(empty($_POST['nome'])      ||
           empty($_POST['email'])     ||
           empty($_POST['motivo'])     ||
           empty($_POST['messaggio'])   ||
           !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
           {
           echo "No arguments Provided!";
           // return false;
           }
        else
            {

                $nome = strip_tags(htmlspecialchars($_POST['nome']));
                $email_address = strip_tags(htmlspecialchars($_POST['email']));
                $motivo = strip_tags(htmlspecialchars($_POST['motivo']));
                $messaggio = strip_tags(htmlspecialchars($_POST['messaggio']));

                // Create the email and send the message
                $to = 'mirkocoppola80@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
                $email_subject = "Website Contact Form:  $nome";
                $email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $nome\n\nEmail: $email_address\n\nOggetto: $motivo\n\nMessaggio:\n$messaggio";
                $headers = "From: mirkocoppola80@gmail.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
                $headers .= "Reply-To: $email_address";
                if (!@mail($to,$email_subject,$email_body,$headers))
                    {
                    // return true;
                        echo "<p>Email error</p>";
                    }
                    else
                    {
                        echo "<p>Email sent successfully!</p>";
                    }
            }
    }
    ?>
    <form class="form-horizontal col-sm-6 col-sm-offset-3" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
      <div class="form-group">
        <div class="row">
          <label for="email" class="col-sm-12">Email</label>
        </div>
        <div class="row">
          <div class="col-sm-12">
            <input type="email" name="email" class="form-control" id="email" placeholder="Email">
          </div>
        </div>
      </div>
      <div class="form-group">
        <div class="row">
          <label for="nome" class="col-sm-12">Nome</label>
        </div>
        <div class="row">
          <div class="col-sm-12">
            <input type="text" name="nome" class="form-control" id="nome" placeholder="Nome">
          </div>
        </div>
      </div>
      <div class="form-group">
        <div class="row">
          <label for="motivo" class="col-sm-12">Motivo</label>
        </div>
        <div class="row">
          <div class="col-sm-12">
            <input type="text" name="motivo" class="form-control" id="motivo" placeholder="Motivo">
          </div>
        </div>
      </div>
      <div class="form-group">
        <div class="row">
          <label for="messaggio" class="col-sm-12">Messaggio</label>
        </div>
        <div class="row">
          <div class="col-sm-12">
            <textarea class="form-control" name="messaggio" rows="5" id="messaggio" placeholder="Motivo">Inserisci il tuo messaggio...</textarea>
          </div>
        </div>
      </div>
      <div class="form-group">
        <div class="">
          <button type="submit" name="submit" class="btn btn-default">Invia</button>
        </div>
      </div>
    </form>

Any help?

techraf
  • 64,883
  • 27
  • 193
  • 198
lisarko8077
  • 299
  • 5
  • 23
  • What `error` are you getting? – Jagrati Oct 19 '16 at 11:42
  • totally depends upon your `error` message. post it! – Pragun Oct 19 '16 at 11:44
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. – xenteros Oct 19 '16 at 11:49
  • It always return the message in echo "Email error" which is the case of mail() not true. – lisarko8077 Oct 19 '16 at 11:54
  • I want to have returned the message inthis line echo "

    Email sent successfully!

    "; .
    – lisarko8077 Oct 19 '16 at 11:55
  • have you tried to change the if statement? to : if (@mail($to,$email_subject,$email_body,$headers)) { // return true; echo "

    Email sent successfully!

    "; } else { echo "

    Email error

    "; }
    – Masivuye Cokile Oct 19 '16 at 14:40
  • @MasivuyeCokile i have just tried this but it return the same result. – lisarko8077 Oct 20 '16 at 03:42

2 Answers2

0

Is that because I'm working in local and don't have any mail server

Yes.

PHP's mail() function will always fail and return false if you don't have a local mail server running.

This answer has some useful tips

You need to setup a mail server on your machine for the mail function to work. If you are on Windows (which I am guessing you are from your use of WAMP) you can setup a Pegasus mail server.

Other options include using a wrapper class such as SwiftMailer or PHPMailer and using them to connect to another SMTP server such as your GMail account. Even if you go the Pegasus mail server on your own localhost route then I would still recommend using one of the two classes I have mentioned above. They give you far more flexibility and are safer.

Connecting to either your ISPs SMTP server or GMail or whatever is the easiest route out of this one.

To expand on the above, you can also look into Mailhog or Mailcatcher to capture your mail locally and examine its contents.

Community
  • 1
  • 1
maiorano84
  • 11,574
  • 3
  • 35
  • 48
0

remove the "@" in

if (@mail($to,$email_subject,$email_body,$headers))

<?php
    if(isset($_POST['submit']))
    {
        if(empty($_POST['nome'])      ||
           empty($_POST['email'])     ||
           empty($_POST['motivo'])     ||
           empty($_POST['messaggio'])   ||
           !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
           {
           echo "No arguments Provided!";
           // return false;
           }
        else
            {

                $nome = strip_tags(htmlspecialchars($_POST['nome']));
                $email_address = strip_tags(htmlspecialchars($_POST['email']));
                $motivo = strip_tags(htmlspecialchars($_POST['motivo']));
                $messaggio = strip_tags(htmlspecialchars($_POST['messaggio']));

                // Create the email and send the message
                $to = 'mirkocoppola80@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
                $email_subject = "Website Contact Form:  $nome";
                $email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $nome\n\nEmail: $email_address\n\nOggetto: $motivo\n\nMessaggio:\n$messaggio";
                $headers = "From: mirkocoppola80@gmail.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
                $headers .= "Reply-To: $email_address";
                if (mail($to,$email_subject,$email_body,$headers))
                    {
                    // return true;
                        echo "<p>Email error</p>";
                    }
                    else
                    {
                        echo "<p>Email sent successfully!</p>";
                    }
            }
    }
    ?>

it should work then.

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34