-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact.php
102 lines (87 loc) · 3.39 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
require 'includes/header.php';
require 'includes/database.php';
?>
<?php
$result="";
require 'phpmailer/PHPMailer.php';
require 'phpmailer/SMTP.php';
require 'phpmailer/Exception.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$msg = $_POST['msg'];
$captcha = $_POST["g-recaptcha-response"];
$secretkey = "6LeoprsZAAAAALf5sxzvkTBwYV4dv-n-tPHW6n4t";
$ip = $_SERVER['REMOTE_ADDR'];
$url = 'https://www.google.com/recaptcha/api/siteverify?secret='.urldecode($secretkey).'&response='.urldecode($captcha).'&remoteip'.$ip;
$response = file_get_contents($url);
$responseKey = json_decode($response,TRUE);
if($responseKey["success"]){
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = "true";
$mail->SMTPSecure = "tls";
$mail->Port = "587";
$mail->Username = "[email protected]";
$mail->Password = "sabitashakyamanandhar";
$mail->Subject='Form Submission: ' . $_POST['subject'];
$mail->setFrom($_POST['email'],$_POST['name']);
$mail->isHTML(true);
$mail->Body='<p>Name: '. $_POST['name']. '<br>Email: ' .$_POST['email'].'<br>
Message: ' . $_POST['msg'] . '</p>';
$mail->addAddress('[email protected]');
//$mail->addReplyTo($_POST['email'],$_POST['name']);
if(!$mail->send()){
$result="Something went wrong. Please try again.";
}
else{
$result="Thanks " . $_POST['name']." for contacting us.";
}
$mail->smtpClose();
saveinDatabase($name, $email, $subject, $msg, $conn);
}
else{
$result="Please perform the captcha test";
}
}
?>
<h1>
Please fill the form below
</h1>
<h5>
<?=$result; ?>
</h5>
<div class="contactForm">
<form action='contact.php' method="POST">
<input type="text" name="name" placeholder="Enter your name" maxlength="30" minlength="4" required>
<input type="email" name="email" placeholder="Enter your email" maxlength="30" minlength="10" required>
<input type="text" name="subject" placeholder="Enter your subject" maxlength="30" minlength="5" required>
<textarea name="msg" rows="5" cols="30" placeholder="Write your message here" required></textarea>
<div class="g-recaptcha" data-sitekey="6LeoprsZAAAAAHDD2xO0NEl4WHy9QMBWAUIIldrm"></div>
<br/>
<button type="submit" name="submit">Submit</button>
</form>
</div>
<?php
function saveinDatabase($name, $email, $subject, $msg, $conn){
$sql = "INSERT INTO contact (name, email, subject, msg) VALUES (?,?,?,?)";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt,$sql)){
header('Location: contact.php?error=sqlerror');
exit();
}
else{
mysqli_stmt_bind_param($stmt, "ssss", $name, $email, $subject, $msg);
mysqli_stmt_execute($stmt);
}
}
?>
<?php
require 'includes/footer.php';
?>