Evan Evan - 4 years ago 86
PHP Question

Is this contact form fully function?

When filling out the form it is saying sent successfully. So I need to enable or include phpmailer? The form is being sent via submit.php but the contact form is an html page. Do I also need to make the contact page php as well. Chmod permissions for submit.php is 755. In the script I do have the email address properly. I have phpmailer installed, do I need to activate it?

<?php


// specify your email here

$to = 'example@example.com';



// Assigning data from $_POST array to variables
if (isset($_POST['name'])) { $name = $_POST['name']; }
if (isset($_POST['email'])) { $from = $_POST['email']; }
if (isset($_POST['company'])) { $company = $_POST['company']; }
if (isset($_POST['message'])) { $message = $_POST['message']; }

// Construct subject of the email
$subject = 'Contact Inquery ' . $name;

// Construct email body
$body_message .= 'NAME: ' . $name . "\r\n\n";
$body_message .= 'EMAIL: ' . $from . "\r\n\n";
$body_message .= 'SUBJECT: ' . $company . "\r\n\n";
$body_message .= 'MESSAGE: ' . $message . "\r\n\n";

// Construct headers of the message
$headers = 'From: ' . $from . "\r\n";
$headers .= 'Reply-To: ' . $from . "\r\n";

$mail_sent = mail($to, $subject, $body_message, $headers);

if ($mail_sent == true){ ?>
<script language="javascript" type="text/javascript">
window.alert("Sent Successfuly.");
</script>
<?php } else { ?>
<script language="javascript" type="text/javascript">
window.alert("Error! Please Try Again Later.");
</script>
<?php
} // End else

?>

Answer Source
  1. Currently, your form doesn't needs to be rendered by PHP. So, it cans be kept in an HTML file.
  2. When you say:

    The form is being sent via submit.php

    I think you want to say:

    The form is being sent to submit.php

    If this is the case, then I think the source code you posted is the content of this file.

  3. Concerning the email sending, you use the built-in PHP mail() function. But this function use the sendmail binary which needs to be installed on your server. Moreover, you have to setup the appropriate settings in the php.ini file in order to make your host correctly send the email.
  4. PHPMailer is a good replacement as it doesn't require any external dependencies. This simple example will show you basically how to use it.

N.B: I see that you PHP code is very simple and I deduce that you're a beginner, that you took this source code somewhere and that you don't really understand it. That's right. You have to start from somewhere.

BUT:

Be careful with this code! It takes the user inputs and sends it without any sanity checks. I recommend you to not deploy this code in production environment (if it's for testing and learning, that's right).

Look at this answer about PHP user input sanitation. It doesn't talk about sanitation for email sending. But it might be a good starting point to understand how it works basically. Then you have to search by yourself.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download