-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.php
53 lines (53 loc) · 1.56 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
<?php
function sendEmail($to, $subject, $message, $html = false)
{
//use the sendgrid API key
$type = "text/plain";
if ($html) {
$type = "text/html";
}
//read api key from sg.api
$file = fopen("E:\projects\php\php\src\sg.api", "r");
$sendgridAPIkey = fread($file, filesize("E:\projects\php\php\src\sg.api"));
fclose($file);
//sendgrid API URL
$url = 'https://api.sendgrid.com/v3/mail/send';
//sendgrid API headers
$authheader = 'Authorization: Bearer ' . $sendgridAPIkey;
$headers = array($authheader, 'Content-Type: application/json');
//sendgrid API data
$data = array(
"personalizations" => array(
array(
"to" => array(
array(
"email" => $to
)
)
)
),
"from" => array(
"email" => "[email protected]"
),
"subject" => $subject,
"content" => array(
array(
"type" => $type,
"value" => $message
)
)
);
//send the email
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//turn off SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
//return the response
return $response;
}