1#Contact form
2
3NOTE: NEEDS An SMTP service on the website server.
4
5<?php
6 //Message Vars
7 $msg = '';
8 $msgClass = '';
9 //check for the submit
10 if(filter_has_var(INPUT_POST,'submit')){
11 //Get form Data
12 $name = htmlspecialchars($_POST['name']);
13 $email = htmlspecialchars($_POST['email']);
14 $message = htmlspecialchars($_POST['message']);
15
16 //Check Required Fields
17 if(!empty($email) && !empty($name) && !empty($message)){
18 //passed
19 //check enail
20 if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
21 //Failed
22 $msg = 'email format is incorrect';
23 $msgClass='alert-danger';
24 }else{
25 //Passed
26 //send to Recipient email needs an email host to send it
27 $toEmail = 'someemail@example.com';
28
29 }
30
31 }else{
32 //failed
33 $msg = 'Please Fill in all fields completely';
34 $msgClass='alert-danger';
35 //Email Subject
36 $subject = 'contact request from '.$name;
37 //creat body of the email
38 $body = "<h2>Contact Request</h2>
39 <h4>Name</h4><p>'.$name.'</p>
40 <h4>Email</h4><p>'.$email.'</p>
41 <h4>Message</h4><p>'.$message.'</p>";
42
43 //Email Header
44 $headers = "MIME-VERSION: 1.0" . "\r\n";
45 $headers .= "Content-Type:text/html;charset=UTF-8" . "/r/n";
46
47 //Additional Headers
48 $headers.= "From: ".$name."<" .$email. ">". "\r\n";
49
50 if(mail($toEmail, $subject, $body, $headers)){
51 //Email sent
52 $msg = 'Email sent';
53 $msgClass = 'alert-success';
54
55 }else{
56 $msg = 'Email has not been sent';
57 $msgClass = 'alert-danger';
58 }
59 }
60?>