-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.php
71 lines (66 loc) · 2 KB
/
functions.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
<?php
require __DIR__ . "/vendor/autoload.php";
$dotenv = Dotenv\Dotenv::create(__DIR__);
$dotenv->load();
/**
* Sendgrid Send email function
* @param $subject
* @param $body
* @param $to
* @return int
* @throws \SendGrid\Mail\TypeException
*/
function sendEmail($subject, $body, $to)
{
$email = new \SendGrid\Mail\Mail();
$email->setFrom(getenv('FROM_EMAIL'), getenv('FROM_NAME'));
$email->setSubject($subject);
$email->addTo($to);
$email->addContent("text/plain", $body);
$email->addContent(
"text/html",
$body
);
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
try {
$response = $sendgrid->send($email);
return $response->statusCode();
} catch (Exception $e) {
echo 'Caught exception: ' . $e->getMessage() . "\n";
}
}
/**
* Takes and process user's input, sends email.
* @param $message
* @return array|string[]
* @throws \SendGrid\Mail\TypeException
*/
function processAndSendEmail($message)
{
//TO:sam@mail.io+SUBJ:Hello+MSG: Im sending this email using SMS
//we split the first input command
$RawCommand = explode("+", $message);
if (count($RawCommand) === 3) {
//extract the useful data by spliting again using :
$To = explode(":", $RawCommand[0])[1];
$Subj = explode(":", $RawCommand[1])[1];
$Msg = explode(":", $RawCommand[2])[1];
//send email
$sendEmail = sendEmail($Subj, $Msg, $To);
//if email send success
if ($sendEmail === 202) {
$resp = ['status' => 'success', 'data' => [
'to' => $To,
'Subject' => $Subj,
'Message' => $Msg
]];
} else {
//if email send fails
$resp = ['status' => 'failed', 'message' => 'Message could\'nt be sent please try again'];
}
} else {
//if user syntax is incorrect
$resp = ['status' => 'failed', 'message' => 'Message could\'nt be sent please check your syntax'];
}
return $resp;
}