-
Notifications
You must be signed in to change notification settings - Fork 3
/
mail.php
executable file
·83 lines (71 loc) · 2.27 KB
/
mail.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
<?php
require 'config.php';
if (!ini_get('date.timezone')) {
date_default_timezone_set('GMT');
}
$msg = '';
function Validate($name, $phone, $email, $message) {
global $msg;
if ($name == '' or $phone == '' or $email == '' or $message == '') {
$msg = 'Please fill all the fields.';
return false;
}
foreach ($_POST as $value) {
if (stripos($value, 'Content-Type:') !== false) {
$msg = 'There was a problem with the information you entered.';
return false;
}
}
return true;
}
function ValidateEmail($mail, $email) {
global $msg;
if (!$mail->ValidateAddress($email)) {
$msg = 'You must specify a valid email address.';
return false;
}
return true;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = trim($_POST['name']);
$phone = trim($_POST['phone']);
$email = trim($_POST['email']);
$message = trim($_POST['message']);
$data = array();
if (Validate($name, $phone, $email, $message)) {
require_once 'inc/phpmailer.php';
$mail = new PHPMailer();
if (ValidateEmail($mail, $email)) {
$email_body = '';
$email_body = $email_body.'Name: '.$name.'<br>';
$email_body = $email_body.'Phone: '.$phone.'<br>';
$email_body = $email_body.'Email: '.$email.'<br>';
$email_body = $email_body.'Message: '.$message.'<br>';
$mail->isSMTP();
$mail->Host = 'smtp.sendgrid.net';
$mail->SMTPAuth = true;
$mail->Username = SD_USERNAME;
$mail->Password = SD_PASSWORD;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SetFrom($email, $name);
$address = '[email protected]';
$mail->AddAddress($address, 'Renaissance');
$mail->Subject = $name.' | Renaissance Contact';
$mail->MsgHTML($email_body);
if (!$mail->Send()) {
$msg = 'There was a problem sending the email.';
}
}
}
if ($msg != '') {
$data['success'] = false;
$data['error'] = $msg;
} else {
$data['success'] = true;
$data['message'] = 'success';
}
echo json_encode($data);
} else {
echo 'error';
}