-
Notifications
You must be signed in to change notification settings - Fork 0
/
welcome_email.php
94 lines (61 loc) · 2.76 KB
/
welcome_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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use Symfony\Component\Dotenv\Dotenv;
require 'vendor/autoload.php';
$dotenv = new Dotenv();
$dotenv->load(__DIR__.'/.env');
$send_to_email = "[email protected]";
$email_password = $_ENV["EMAIL_PASS"] ?? '';
$email_user = $_ENV["EMAIL_USER"] ?? '';
$email_host = $_ENV["MAIL_HOST"] ?? '';
$email_port = $_ENV["MAIL_PORT"] ?? '';
$otp = str_pad(mt_rand(0, 999999), 6, '0', STR_PAD_LEFT);
$response = array();
$json = send_email_message($send_to_email, $otp, $email_host, $email_user, $email_password, $email_port);
$result = json_decode($json);
if($result->code == 200){
$response['success'] = "1";
$response['message'] = "success";
echo json_encode($response);
}else{
$response['success'] = "0";
$response['message'] = "failed";
echo json_encode($response);
}
function send_email_message($send_to, $send_otp, $host, $username, $password, $port){
$body = file_get_contents('welcome-email.html');
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = $host;
$mail->SMTPAuth = true;
$mail->Username = $username;
$mail->Password = $password;
$mail->SMTPSecure = 'ssl';
$mail->Port = $port;
$mail->setFrom($username, 'Name of Sender');
$mail->addAddress($send_to);
$mail->isHTML(true);
$mail->Subject = 'Subject';
$mail->Body = str_replace('{{otp}}', $send_otp, $body);
$mail->send();
// set response code
http_response_code(200);
// display message: Message sent
return json_encode(array('code' => http_response_code(), "message"=>"Mail has been sent successfully!."));
} catch (Exception $e) {
// set response code
http_response_code(400);
// display message: Message Failed
return json_encode(array('code' => http_response_code(), "message" => "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"));
}
}
?>