Display success message on submit form

Sure. I understand. I will ask my host. Will be back soon. Thanks Pullo.

just got off the chat with my host, they confirmed I can have my own contact form and that PHP mail function is working on their end. They referred me to this article on how to use it : http://www.karavadra.net/blog/php-mail-function-with-bluehost-working/

Nice one!

Let’s take that code one for one and see if it works.

submit.php

<?php 
  $emailto = 'to@domain.com';
  $toname = 'TO NAME';
  $emailfrom = 'from@domain.com';
  $fromname = 'FROM NAME';
  $subject = 'Email Subject';
  $messagebody = 'Hello.';
  $headers = 
    'Return-Path: ' . $emailfrom . "\\r\
" . 
    'From: ' . $fromname . ' <' . $emailfrom . '>' . "\\r\
" . 
    'X-Priority: 3' . "\\r\
" . 
    'X-Mailer: PHP ' . phpversion() .  "\\r\
" . 
    'Reply-To: ' . $fromname . ' <' . $emailfrom . '>' . "\\r\
" .
    'MIME-Version: 1.0' . "\\r\
" . 
    'Content-Transfer-Encoding: 8bit' . "\\r\
" . 
    'Content-Type: text/plain; charset=UTF-8' . "\\r\
";
  $params = '-f ' . $emailfrom;
  $success = mail($emailto, $subject, $message, $headers, $params);

  if ($success){
    echo "Mail sent successfully";
  }
  else{
    echo "Something went wrong";
  }
?>

Don’t forget to change the $emailto variable to your email address.
Also, it is not a good idea to post your address in plain text in the forum - this is a good way of getting spam.
I edited it out of your last post :slight_smile:

Now there are no checks as to whether the input was correct, rather when you submit the form, you should get an email with the text “Hello.”

Sorry if it seems a bit frustrating to take such small steps, but we need to get a working base, which we can then expand from.
I’m in Europe, so if you can reply by this evening, I’ll be able to write more.

Thanks for catching this for me:)

After I submit the form, I get the email with empty text field…there’s no word “Hello”…

No worries. I understand the time difference:)

Hi there,

On closer inspection there is a typo in the article you link to.
No matter, we now know that we can send mail via your host’s server :slight_smile:

Let’s try and incorporate all of that in our code so far then:

<?php
$emailto = 'to@domain.com';
$toname = 'TO NAME';

$fromname = filter_var($_POST['name'],FILTER_SANITIZE_STRING);
$emailfrom = filter_var($_POST['email'],FILTER_SANITIZE_STRING, FILTER_VALIDATE_EMAIL);
$subject = filter_var($_POST['subject'],FILTER_SANITIZE_STRING);

$message = filter_var($_POST['message'],FILTER_SANITIZE_STRING);
$error = "";

$headers = 
  'Return-Path: ' . $emailfrom . "\\r\
" . 
  'From: ' . $fromname . ' <' . $emailfrom . '>' . "\\r\
" . 
  'X-Priority: 3' . "\\r\
" . 
  'X-Mailer: PHP ' . phpversion() .  "\\r\
" . 
  'Reply-To: ' . $fromname . ' <' . $emailfrom . '>' . "\\r\
" .
  'MIME-Version: 1.0' . "\\r\
" . 
  'Content-Transfer-Encoding: 8bit' . "\\r\
" . 
  'Content-Type: text/plain; charset=UTF-8' . "\\r\
";
$params = '-f ' . $emailfrom;

if (empty($fromname)){
  $error .= "You didn't enter a name <br />";
}

if (empty($subject)){
  $error .= "You didn't enter a subject <br />";
}

if (empty($emailfrom)){
  $error .= "You didn't enter an email address <br />";
} elseif (!eregi("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,})\\.?$", $emailfrom)){
  $error .= "The email address appears to be invalid <br />";  
}

if (empty($message)){
  $error .= "You didn't enter a message <br />";
}

if (empty($error)) {
  $mailcontent = "Name: ". $name. "\
".
                 "Subject: ". $subject. "\
".
                 "Email: ". $email. "\
\
".
                 $message;
                 
  $success = mail($emailto, $subject, $mailcontent, $headers, $params);

  if ($success){
    echo "Mail sent successfully";
  }
  else{
    echo "Something went wrong";
  }
} else {
  echo $error;
}
?>

With this code, the script validates the fields as before, then attempts to send the mail.
If it can send the mail, then it echos “Mail sent successfully” to the screen.

Have a play with that and let me know if it works.

Hi Pullo,

I just replaced the code in submit.php with the code you gave me and received message “something went wrong”…

Aw! - that’s weak!
I’ll get this set up on my server and see if I can do some troubleshooting.

Ok, the only thing that I could find that was slightly wrong, was two variable names:

$mailcontent = "Name: ". $name. "\
".
                 "Subject: ". $subject. "\
".
                 "Email: ". $email. "\
\
".
                 $message;

should have been:

$mailcontent = "Name: ". $fromname. "\
".
               "Subject: ". $subject. "\
".
               "Email: ". $emailfrom. "\
\
".
               $message;

but this makes your host’s mail servers very fussy if they throw an error at that.

Can you try something?

Copy index.html and submit.php into a folder on your server.
Access the form and try and use it to send a mail.
This works for me.

index.html

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>Contact Us!</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      body {
        font-size: 62.5%;
        font-family: Helvetica, sans-serif;
      }

      p {
        font-size: 1.3em;
        margin-bottom: 15px;
      }

      #page-wrap {
        width: 660px;
        background: white;
        padding: 20px 50px 20px 50px;
        margin: 20px auto;
        height: auto !important;
      }

      #contact-area {
        width: 600px;
        margin-top: 25px;
      }

      #contact-area input, #contact-area textarea {
        padding: 5px;
        width: 471px;
        font-family: Helvetica, sans-serif;
        font-size: 1.4em;
        margin: 0px 0px 10px 0px;
        border: 2px solid #ccc;
      }

      #contact-area textarea {
        height: 90px;
      }

      #contact-area textarea:focus, #contact-area input:focus {
        border: 2px solid #900;
      }

      #contact-area input.submit-button {
        width: 100px;
        float: right;
      }

      label {
        float: left;
        text-align: right;
        margin-right: 15px;
        width: 100px;
        padding-top: 5px;
        font-size: 1.4em;
      }  
    </style>
  </head>
  <body>

    <div id="page-wrap">
      <h1>Contact form</h1>

      <div id="contact-area">
        <form method="post" action="submit.php">

          <label for="name">Name:</label>
          <input type="text" name="name" id="name" />
          
          <label for="subject">Subject:</label>
          <input type="text" name="subject" id="subject" />
    
          <label for="email">Email:</label>
          <input type="text" name="email" id="email" />
          
          <label for="message">Message:</label><br />
          <textarea name="message" rows="20" cols="20" id="message"></textarea>

          <input type="submit" name="submit" value="Submit" class="submit-button" />
        </form>
      </div>
    </div>
  </body>
</html>

submit.php

<?php
$emailto = 'you@you.com';
$toname = 'Your name';

$fromname = filter_var($_POST['name'],FILTER_SANITIZE_STRING);
$emailfrom = filter_var($_POST['email'],FILTER_SANITIZE_STRING, FILTER_VALIDATE_EMAIL);
$subject = filter_var($_POST['subject'],FILTER_SANITIZE_STRING);

$message = filter_var($_POST['message'],FILTER_SANITIZE_STRING);
$error = "";

$headers = 
  'Return-Path: ' . $emailfrom . "\\r\
" . 
  'From: ' . $fromname . ' <' . $emailfrom . '>' . "\\r\
" . 
  'X-Priority: 3' . "\\r\
" . 
  'X-Mailer: PHP ' . phpversion() .  "\\r\
" . 
  'Reply-To: ' . $fromname . ' <' . $emailfrom . '>' . "\\r\
" .
  'MIME-Version: 1.0' . "\\r\
" . 
  'Content-Transfer-Encoding: 8bit' . "\\r\
" . 
  'Content-Type: text/plain; charset=UTF-8' . "\\r\
";
$params = '-f ' . $emailfrom;

if (empty($fromname)){
  $error .= "You didn't enter a name <br />";
}

if (empty($subject)){
  $error .= "You didn't enter a subject <br />";
}

if (empty($emailfrom)){
  $error .= "You didn't enter an email address <br />";
} elseif (!eregi("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,})\\.?$", $emailfrom)){
  $error .= "The email address appears to be invalid <br />";  
}

if (empty($message)){
  $error .= "You didn't enter a message <br />";
}

if (empty($error)) {
  $mailcontent = "Name: ". $fromname. "\
".
                 "Subject: ". $subject. "\
".
                 "Email: ". $emailfrom. "\
\
".
                 $message;
                 
  $success = mail($emailto, $subject, $mailcontent, $headers, $params);

  if ($success){
    echo "Mail sent successfully";
  }
  else{
    echo "Something went wrong";
  }
} else {
  echo $error;
}
?>

Don’t forget to change the variables $emailto and $toname accordingly.

I created a folder in www.domain/form and put 2 files there www.domain/form/index.html and submit.php…received message “something went wrong”…

That’s a real shame.
The code I posted works great for me, so there is something in there that your host’s servers don’t like.

All you can do now is go through and start reverting back the values to hard-coded ones, e.g. change:

$fromname = filter_var($_POST['name'],FILTER_SANITIZE_STRING);

to:

$fromname = 'FROM NAME';

Then see if that makes a difference.
If not, repeat with $emailfrom etc.

Other suggestions would be to contact your host and see if they can spot anything that might cause an error, or ask over in the PHP forum.

Sorry I can’t help you more.

No worries. Thanks Pullo.