-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact.php
71 lines (63 loc) · 1.9 KB
/
contact.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
$toEmail = "asd@asdasd"; //Replace it recipient email address
$subject = 'Ah!! My email from Somebody out there...'; //Subject line for emails
//Let's clean harmful characters from raw POST data using PHP Sanitize filters.
$postName = filter_var($_POST["postName"], FILTER_SANITIZE_STRING);
$postEmail = filter_var($_POST["postEmail"], FILTER_SANITIZE_EMAIL);
$postPhone = filter_var($_POST["postPhone"], FILTER_SANITIZE_STRING);
$postMessage = filter_var($_POST["postMessage"], FILTER_SANITIZE_STRING);
//Let's put additional php validation here
if(strlen($postName)<1) // If length is less than 1 we will throw an HTTP error.
{
header('HTTP/1.1 500 Name Field Empty');
exit();
}
//similar validation applies to all data, unless you want to replace with some other strong validation.
if(strlen($postEmail)<1)
{
header('HTTP/1.1 500 Email Field Empty');
exit();
}
if(strlen($postPhone)<1)
{
header('HTTP/1.1 500 Phone Field Empty');
exit();
}
if(strlen($postMessage)<1)
{
header('HTTP/1.1 500 Message Field Empty');
exit();
}
//Finally we can proceed with PHP email.
$headers = 'From: '.$postEmail.'' . "\r\n" .
'Reply-To: '.$postEmail.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//Email Body
$Body = "";
$Body .= "Name: ";
$Body .= $postName;
$Body .= "\n";
$Body .= "\n";
$Body .= "Email: ";
$Body .= $postEmail;
$Body .= "\n";
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $postPhone;
$Body .= "\n";
$Body .= "\n";
$Body .= "Message: ";
$Body .= $postMessage;
$Body .= "\n";
$Body .= "\n";
@$sentMail = mail($toEmail, $subject, $Body .' -'.$postName, $headers);
if(!$sentMail)
{
header('HTTP/1.1 500 Couldnot send mail! Sorry..');
exit();
}else{
echo '<h3>Hi '.$postName.', Thank you for your email</h3>
<p>Your email has already arrived in our Inbox, all We need to do is Check it..
<br />Good day.</p>';
}
?>