-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemail.php
70 lines (55 loc) · 1.54 KB
/
email.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
<?php
session_start();
error_reporting(E_ALL);
date_default_timezone_set("America/Sao_Paulo");
require_once("php-mailer/PHPMailer.php");
require_once("php-mailer/SMTP.php");
require_once("php-mailer/Exception.php");
use PHPMailer\PHPMailer\PHPMailer;
function redirectToIndex()
{
header("Location: ./index.php");
exit;
}
function sendMail($name, $email, $message, $subject, $date)
{
include './config.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = $myemail;
$mail->Password = $mypassword;
$mail->Port = 587;
$mail->setFrom($myemail, $name);
$mail->addReplyTo($email, $name);
$mail->addAddress($myemail);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = "<b>Name:</b> {$name}<br><b>Email:</b> {$email}<br><br><b>Message:</b><br><br>
{$message}<br><br><b>Date:</b> {$date}";
if ($mail->send()) {
$_SESSION["mail_success"] = true;
} else {
$_SESSION["mail_error"] = true;
}
redirectToIndex();
}
function start()
{
if (
isset($_POST["email"]) && !empty(trim($_POST["email"]))
&& isset($_POST["message"]) && !empty(trim($_POST["message"]))
) {
$name = !empty($_POST["name"]) ? $_POST["name"] : "Not informed";
$email = trim($_POST["email"]);
$message = trim(str_replace("\n", '<br />', $_POST["message"]));
$subject = "Contact";
$date = date("d/m/Y H:i");
sendMail($name, $email, $message, $subject, $date);
} else {
$_SESSION["mail_error"] = true;
redirectToIndex();
}
}
start();