-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path02E_send_email_mailersend.sql
84 lines (75 loc) · 2.74 KB
/
02E_send_email_mailersend.sql
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
--drop function send_email_mailersend;
CREATE OR REPLACE FUNCTION public.send_email_mailersend (message JSONB)
RETURNS json
LANGUAGE plpgsql
SECURITY DEFINER -- required in order to read keys in the private schema
-- Set a secure search_path: trusted schema(s), then 'pg_temp'.
-- SET search_path = admin, pg_temp;
AS $$
DECLARE
retval json;
MAILERSEND_API_TOKEN text;
BEGIN
SELECT value::text INTO MAILERSEND_API_TOKEN FROM private.keys WHERE key = 'MAILERSEND_API_TOKEN';
IF NOT found THEN RAISE 'missing entry in private.keys: MAILERSEND_API_TOKEN'; END IF;
SELECT
* INTO retval
FROM
http
((
'POST',
'https://api.mailersend.com/v1/email',
ARRAY[http_header ('Authorization',
'Bearer ' || MAILERSEND_API_TOKEN
), http_header ('X-Requested-With', 'XMLHttpRequest')],
'application/json',
json_build_object(
'from', json_build_object(
'email', message->>'sender'
),
'to', json_build_array(
json_build_object(
'email', message->>'recipient'
)
),
'subject', message->>'subject',
'text', message->>'text_body',
'html', message->>'html_body' --,
--'CustomID', message->>'messageid'
)::text
));
-- if the message table exists,
-- and the response from the mail server contains an id
-- and the message from the mail server starts wtih 'Queued'
-- mark this message as 'queued' in our message table, otherwise leave it as 'ready'
IF (SELECT to_regclass('public.messages')) IS NOT NULL AND
retval::text = '202' THEN
UPDATE public.messages SET status = 'queued' WHERE id = (message->>'messageid')::UUID;
ELSE
RAISE 'error sending message with mailersend: %',retval;
END IF;
RETURN retval;
END;
$$;
-- Do not allow this function to be called by public users (or called at all from the client)
REVOKE EXECUTE on function public.send_email_mailersend FROM PUBLIC;
/*
curl -X POST \
https://api.mailersend.com/v1/email \
-H 'Content-Type: application/json' \
-H 'X-Requested-With: XMLHttpRequest' \
-H 'Authorization: Bearer {place your token here without brackets}' \
-d '{
"from": {
"email": "[email protected]"
},
"to": [
{
"email": "[email protected]"
}
],
"subject": "Hello from MailerSend!",
"text": "Greetings from the team, you got this message through MailerSend.",
"html": "Greetings from the team, you got this message through MailerSend."
}'
*/