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?>
1<?php
2
3if($_POST) {
4 $visitor_name = "";
5 $visitor_email = "";
6 $email_title = "";
7 $concerned_department = "";
8 $visitor_message = "";
9 $email_body = "<div>";
10
11 if(isset($_POST['visitor_name'])) {
12 $visitor_name = filter_var($_POST['visitor_name'], FILTER_SANITIZE_STRING);
13 $email_body .= "<div>
14 <label><b>Visitor Name:</b></label> <span>".$visitor_name."</span>
15 </div>";
16 }
17
18 if(isset($_POST['visitor_email'])) {
19 $visitor_email = str_replace(array("\r", "\n", "%0a", "%0d"), '', $_POST['visitor_email']);
20 $visitor_email = filter_var($visitor_email, FILTER_VALIDATE_EMAIL);
21 $email_body .= "<div>
22 <label><b>Visitor Email:</b></label> <span>".$visitor_email."</span>
23 </div>";
24 }
25
26 if(isset($_POST['email_title'])) {
27 $email_title = filter_var($_POST['email_title'], FILTER_SANITIZE_STRING);
28 $email_body .= "<div>
29 <label><b>Reason For Contacting Us:</b></label> <span>".$email_title."</span>
30 </div>";
31 }
32
33 if(isset($_POST['concerned_department'])) {
34 $concerned_department = filter_var($_POST['concerned_department'], FILTER_SANITIZE_STRING);
35 $email_body .= "<div>
36 <label><b>Concerned Department:</b></label> <span>".$concerned_department."</span>
37 </div>";
38 }
39
40 if(isset($_POST['visitor_message'])) {
41 $visitor_message = htmlspecialchars($_POST['visitor_message']);
42 $email_body .= "<div>
43 <label><b>Visitor Message:</b></label>
44 <div>".$visitor_message."</div>
45 </div>";
46 }
47
48 if($concerned_department == "billing") {
49 $recipient = "billing@domain.com";
50 }
51 else if($concerned_department == "marketing") {
52 $recipient = "marketing@domain.com";
53 }
54 else if($concerned_department == "technical support") {
55 $recipient = "tech.support@domain.com";
56 }
57 else {
58 $recipient = "contact@domain.com";
59 }
60
61 $email_body .= "</div>";
62
63 $headers = 'MIME-Version: 1.0' . "\r\n"
64 .'Content-type: text/html; charset=utf-8' . "\r\n"
65 .'From: ' . $visitor_email . "\r\n";
66
67 if(mail($recipient, $email_title, $email_body, $headers)) {
68 echo "<p>Thank you for contacting us, $visitor_name. You will get a reply within 24 hours.</p>";
69 } else {
70 echo '<p>We are sorry but the email did not go through.</p>';
71 }
72
73} else {
74 echo '<p>Something went wrong</p>';
75}
76?>
77