-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact_me_smtp.php
More file actions
97 lines (76 loc) · 3.33 KB
/
contact_me_smtp.php
File metadata and controls
97 lines (76 loc) · 3.33 KB
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
<?php
if($_POST)
{
$to_Email = "info@century.consulting"; // Replace with recipient email address
$subject = 'Message depuis '.$_SERVER['SERVER_NAME']; //Subject line for emails
$host = "smtp.sendgrid.net"; // Your SMTP server. For example, smtp.mail.yahoo.com
$username = "apikey"; //For example, your.email@yahoo.com
$password = "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"; // Your password
$SMTPSecure = "ssl"; // For example, ssl
$port = 465; // For example, 465
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
//exit script outputting json data
$output = json_encode(
array(
'type'=>'error',
'text' => 'Request must come from Ajax'
));
die($output);
}
//check $_POST vars are set, exit if any missing
if(!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userMessage"]))
{
$output = json_encode(array('type'=>'error', 'text' => 'Certains champs sont vides, Veuilliez les remplir!'));
die($output);
}
//Sanitize input data using PHP filter_var().
$user_Name = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
$user_Email = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
$user_Message = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);
$user_Message = str_replace("\'", "'", $user_Message);
$user_Message = str_replace("'", "'", $user_Message);
//additional php validation
if(strlen($user_Name)<4) // If length is less than 4 it will throw an HTTP error.
{
$output = json_encode(array('type'=>'error', 'text' => 'Nom cours ou vide!'));
die($output);
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
{
$output = json_encode(array('type'=>'error', 'text' => 'Prière de rentrer un email valide!'));
die($output);
}
if(strlen($user_Message)<5) //check emtpy message
{
$output = json_encode(array('type'=>'error', 'text' => 'Message court ou vide!'));
die($output);
}
//proceed with PHP email.
include("php/PHPMailerAutoload.php"); //you have to upload class files "class.phpmailer.php" and "class.smtp.php"
include("php/class.phpmailer.php");
include("php/class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = $host;
$mail->Username = $username;
$mail->Password = $password;
$mail->SMTPSecure = $SMTPSecure;
$mail->Port = $port;
$mail->setFrom($username);
$mail->addReplyTo($user_Email);
$mail->AddAddress($to_Email);
$mail->Subject = $subject;
$mail->Body = $user_Message. "\r\n\n" .'Name: '.$user_Name. "\r\n" .'Email: '.$user_Email;
$mail->WordWrap = 200;
$mail->IsHTML(false);
if(!$mail->send()) {
$output = json_encode(array('type'=>'error', 'text' => 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo));
die($output);
} else {
$output = json_encode(array('type'=>'message', 'text' => 'Merci '.$user_Name .'! Century Consulting vous contactera très bientôt.'));
die($output);
}
}
?>