Question

I am using the form below. I am using the page, contact.php to display the form and submit the form. The form validates and says email sent but when i check the email it doesn't show.

PHP:

         <?php
          function spamcheck($field)
           {
         //filter_var() sanitizes the e-mail
      //address using FILTER_SANITIZE_EMAIL
       $field=filter_var($field, FILTER_SANITIZE_EMAIL);

       //filter_var() validates the e-mail
      //address using FILTER_VALIDATE_EMAIL
        if(filter_var($field, FILTER_VALIDATE_EMAIL))
          {
         return TRUE;
            }
         else
        {
         return FALSE;
             }
         }

          if (isset($_REQUEST['email']))
           {//if "email" is filled out, proceed

             //check if the email address is invalid
           $mailcheck = spamcheck($_REQUEST['email']);
           if ($mailcheck==FALSE)
            {
               echo "Invalid input";
                 }
            else
          {//send email
         $email = $_REQUEST['email'] ;
          $subject = $_REQUEST['subject'] ;
         $message = $_REQUEST['message'] ;
        mail("me@mysite.com", "Subject: $subject",
         $message, "From: $email" );
        echo "Thank you for using our mail form";
           }
         }
        else
         {//if "email" is not filled out, display the form
         echo "<form method='post' action='contact.php'>
        Email: <input name='email' type='text' /><br />
        Subject: <input name='subject' type='text' /><br />
         Message:<br />
      <textarea name='message' rows='15' cols='40'>
      </textarea><br />
       <input type='submit' />
      </form>";
        }
          ?>
Was it helpful?

Solution

Check your php log. Maybe it does fail because it does not have a SMTP client configured neither in your app or in PHP to be used when trying to send mails.

OTHER TIPS

In your form when the 'thank you' message displays it means the mail function has been run, not that it succeeded. the php mail function returns true when it succeeds and othwerwise false. Could you please try something like:

if(mail("me@mysite.com", "Subject: $subject",$message, "From: $email")){
echo "Thank you for using our mail form";
}else{
echo "hmm... seems the mail cannot be sent";
}

also is the mail function allowed in the php.ini?

Try to send mail "from" an address you have on your contacts. Are you sure your email account accepts these spoofed emails? Gmail sends those php emails from addresses not in your contact list to spam. Try to test on a different email account. It's worth the shot.

Another test you can run is using an alert to retrieve the mail() return, to check whether it comes true or false..

alert(mail(hey@joe.jh, subject, message, email));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top