Execute the following code in a SQL Query window:
INSERT INTO private.keys (key, value) values ('SENDGRID_API_KEY', 'aaaaaaaaaa');
Where:
aaaaaaaaaa
is your Sendgrid API Key
Run the SQL
code contained in 02_send_email_message.sql in a query window to create the PostgreSQL function. NOTE: You must modify this function for Sendgrid. See the line:
email_provider text := 'sendgrid';
Run the SQL
code contained in 02D_send_email_sendgrid.sql in a query window to create the PostgreSQL function.
You can send a test message from a query window like this:
select send_email_message('{
"sender": "[email protected]",
"recipient": "[email protected]",
"subject": "This is a test message from my Supabase app!",
"html_body": "<html><body>This message was sent from <a href=\"https://postgresql.org\">PostgreSQL</a> using <a href=\"https://supabase.io\">Supabase</a> and <a href=\"https://sendgrid.com\">Sendgrid</a>.</body></html>"
}');
If you've got everything setup correctly, you'll get a JSON object back with the Provider's response, such as:
200
At this point, you have everything you need to send messages. If you want to track your messages, read on.
Run the SQL
code from 03_setup_messages_table.sql in a query window to create the table that will store your email messages. When the send_email_message
function senses that this table exists, it will store your messages in this table automatically when you send them.
This step is not yet implemented.
This is completely optional, but if your workflow calls for you to create messages to be sent at a later time (say, according to a schedule, or triggered from another event or table update) you can use the create_email_message
function.
Run the SQL
code in 05_create_email_message.sql in a query window. Now you can create messages in the messages table like this:
select create_email_message('{
"sender": "[email protected]",
"recipient": "[email protected]",
"subject": "This is a test message from my Supabase app!",
"html_body": "<html><body>This message was originally created as \"ready\" in the messages table, then sent later from <a href=\"https://supabase.io\">Supabase</a> using <a href=\"https://sendgrid.com\">Sendgrid</a>.</body></html>"
}');
This will create a message in the messages table with messages.status
= ready
and it will return the messageid
of the message it just created. To send the message, just call send_email_message
later and pass it the messageid
of this message. For example:
select send_email_message('{
"messageid": "7f5fd9b7-cacb-4949-b8d4-a0398fa382e7"
}');