-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsend.php
72 lines (55 loc) · 1.83 KB
/
send.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
<?php
try {
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
throw new Exception("This request is invalid");
}
$name = filter_input(INPUT_POST, 'name');
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
$description = filter_input(INPUT_POST, 'description');
if (!$name) {
throw new Exception("Name is empty");
}
if (strlen($name) <= 3) {
throw new Exception("Name is to small");
}
if (!$email) {
throw new Exception("Email is empty");
}
if (!$description) {
throw new Exception("Description is empty");
}
if (strlen($description) <= 10) {
throw new Exception("Description is to small");
}
include __DIR__.'/vendor/autoload.php';
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = getenv('SMTP_HOST');
$mail->SMTPAuth = true;
$mail->Username = getenv('SMTP_USERNAME');
$mail->Password = getenv('SMTP_PASSWORD');
$mail->Port= getenv('SMTP_PORT');
$mail->From = $email;
$mail->FromName = $name;
$mail->addAddress(getenv('SMTP_RECIPIENT'));
$mail->isHTML(true);
$mail->Subject = getenv('SMTP_SUBJECT');
$mail->Body = sprintf(file_get_contents(__DIR__.'/body.html'), $name, $email, $description);
if (!$mail->send()) {
include __DIR__.'/MailException.php';
throw new MailException($mail->ErrorInfo);
}
header('location: '.getenv('SMTP_SUCCESS_REDIRECT'));
} catch (MailException $e) {
$translate = include __DIR__.'/translate.php';
echo $translate['Mail hasn\'t been sent.'];
echo '<br>';
echo $e->getMessage();
die();
} catch (Exception $e) {
$translate = include __DIR__.'/translate.php';
echo $translate[$e->getMessage()] ?? $e->getMessage();
die();
}