Does php mail() require database connection using the contact us form?

+2 Kumi Festus · December 26, 2014
I created a Contact Us page on my website with the following html and php code. The message fails to send anytime I try the system. I contacted the Hosting Company and they replied that there is no proper database connection at the background that's the form cannot deliver messages to my inbox.

But my bigger surprise is , does the php mail() need a database connection to function? I am not saving the content of the form to the database. I want users of my site to be able to send messages to my CPanel  email account if they have to contact me.

The code is below:
<?php
     if(
       isset($_POST['contact_name']) &&
       isset($_POST['contact_email']) &&
       isset($_POST['contact_subject']) &&
       isset($_POST['contact_text'])){
       
        $contact_name =  $_POST['contact_name'];
        $contact_email = $_POST['contact_email'];
        $contact_subject = $_POST['contact_subject'];
        $contact_text = $_POST['contact_text'];
        if(
          empty($contact_name) &&
          empty($contact_email) &&
          empty($contact_subject) &&
          empty($contact_text)){
            echo '<p style="color:red;">All fields are required!</p>';
          }else{
           $to = 'info@codinginstitution.com';
           $subject = $contact_subject;
           $body = $contact_name. "\n" .$contact_text;
           $headers = 'From:'. $contact_email;
           if(@mail($to,$subject,$body,$headers)){
           echo '<p style="color:green;">Your message was sent successfully. We will reply soon.</p>';
           }else{
           echo '<p style="color:red;">Sorry,something went wrong.Please send the message again.</p>';
           }
          }
       }
    ?>
<form action="contact.php" method="post" id="contact_form" >
  Name*:<br/>
   <input type="text" name="contact_name" id="contact_name" maxlength="40"/>
   <span id="contact_name_span"></span>
   <br/>
  Email*:<br/>
    <input type="text" name="contact_email" id="contact_email" maxlength="60"/>
    <span id="contact_email_span"></span>
    <br/>
  Subject*:<br/>
    <input type="text" name="contact_subject" id="contact_subject" maxlength="40"/>
    <span id="contact_subject_span"></span>
    <br/>
  Message*:<br/>
   <textarea cols="60" rows="10" name="contact_text" id="contact_text" maxlength="1000"></textarea>
   <span id="contact_text_span"></span>
   <br/>
   <input type="submit" value="Send Message" id="contact_form_submit"/>
</form>



Post a Reply

Replies

Oldest  Newest  Rating
0 Alex . · December 29, 2014
form.html


<form action="contact.php" method="post" id="contact_form" >
  Name*:<br/>
   <input type="text" name="contact_name" id="contact_name" maxlength="40"/>
   <span id="contact_name_span"></span>
   <br/>
  Email*:<br/>
    <input type="text" name="contact_email" id="contact_email" maxlength="60"/>
    <span id="contact_email_span"></span>
    <br/>
  Subject*:<br/>
    <input type="text" name="contact_subject" id="contact_subject" maxlength="40"/>
    <span id="contact_subject_span"></span>
    <br/>
  Message*:<br/>
   <textarea cols="60" rows="10" name="contact_text" id="contact_text" maxlength="1000"></textarea>
   <span id="contact_text_span"></span>
   <br/>
   <input type="submit" value="Send Message" id="contact_form_submit"/>
</form>


contact.php


<?php
     if(
       isset($_POST['contact_name']) &&
       isset($_POST['contact_email']) &&
       isset($_POST['contact_subject']) &&
       isset($_POST['contact_text'])){
       
        $contact_name =  $_POST['contact_name'];
        $contact_email = $_POST['contact_email'];
        $contact_subject = $_POST['contact_subject'];
        $contact_text = $_POST['contact_text'];
        if(
          empty($contact_name) &&
          empty($contact_email) &&
          empty($contact_subject) &&
          empty($contact_text)){
            echo '<p style="color:red;">All fields are required!</p>';
          }else{
           $to = 'info@codinginstitution.com';
           $subject = $contact_subject;
           $body = $contact_name. "\n" .$contact_text;
           $headers = 'From:'. $contact_email;
           if(@mail($to,$subject,$body,$headers)){
           echo '<p style="color:green;">Your message was sent successfully. We will reply soon.</p>';
           }else{
           echo '<p style="color:red;">Sorry,something went wrong.Please send the message again.</p>';
           }
          }
       }
    ?>

0 Kumi Festus · December 29, 2014
Thanks,
Great suggestion
0 Alex . · December 29, 2014
Let me know if it works.
0 Alex . · December 27, 2014
i got your code split it the html from php ie (form.html and contact.php) naturally and tested to 2 different servers.

worked fine.
0 Kumi Festus · December 27, 2014
So are you suggesting one of these?

<?php
//php code here
include 'form.html';
?>

or
<html>
<?php
include 'contact.php';
?>
<form>
<!--form elements here-->
</form>
</html>

Tell me how you did it.
Thanks
0 Kumi Festus · December 31, 2014
Sure, I will let you know. I actually suspect the server. I tested it on XAMPP and it worked.
My site http://codinginstitution.com/index.php is under construction. So  i want to concentrate on more complex issues. I will come back to that later.
0 Kumi Festus · December 31, 2014
public class thanks{
public static void main(String[] args){
System.out.println("Thanks For Your Help");
}
}
  • 1

PHP

107,090 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators