-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendemail.php
60 lines (54 loc) · 2.21 KB
/
sendemail.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
<?php
// Allow CORS (Cross-Origin Resource Sharing) for testing purposes
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
// Check if the request method is POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get the raw POST data
$data = json_decode(file_get_contents("php://input"));
// Check if the necessary fields are provided
if (isset($data->recipient) && isset($data->message)) {
$recipientEmail = $data->recipient;
$subject = isset($data->subject) ? $data->subject : "Task Management"; // Default subject if not provided
$messageContent = $data->message;
// SendGrid API key and URL
$apiKey = 'yourAPI key';
$url = 'https://api.sendgrid.com/v3/mail/send';
// Prepare email data
$emailData = [
"personalizations" => [
["to" => [["email" => $recipientEmail]]]
],
"from" => ["email" => "[email protected]"],
"subject" => $subject,
"content" => [
["type" => "text/plain", "value" => $messageContent]
]
];
// Send email using cURL
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $apiKey",
"Content-Type: application/json"
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($emailData));
$response = curl_exec($ch);
if ($response === false) {
// Return error if email sending fails
echo json_encode(['status' => 'error', 'message' => curl_error($ch)]);
} else {
// Return success response
echo json_encode(['status' => 'success', 'message' => 'Email sent successfully']);
}
curl_close($ch);
} else {
// Missing required fields
echo json_encode(['status' => 'error', 'message' => 'Missing required fields']);
}
} else {
// Method not allowed
echo json_encode(['status' => 'error', 'message' => 'Invalid request method']);
}
?>