Skip to content

Commit 80cfc2b

Browse files
committed
add a demo for sending emails
1 parent 8b245f5 commit 80cfc2b

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

examples/sending emails/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Sending Emails with SQLPage
2+
3+
SQLPage lets you interact with any email service through their API,
4+
using the [`sqlpage.fetch` function](https://sql-page.com/functions.sql?function=fetch).
5+
6+
## Why Use an Email Service?
7+
8+
Sending emails directly from your server can be challenging:
9+
- Many ISPs block direct email sending to prevent spam
10+
- Email deliverability requires proper setup of SPF, DKIM, and DMARC records
11+
- Managing bounce handling and spam complaints is complex
12+
- Direct sending can impact your server's IP reputation
13+
14+
Email services solve these problems by providing reliable APIs for sending emails while handling deliverability, tracking, and compliance.
15+
16+
## Popular Email Services
17+
18+
- [Mailgun](https://www.mailgun.com/) - Developer-friendly, great for transactional emails
19+
- [SendGrid](https://sendgrid.com/) - Powerful features, owned by Twilio
20+
- [Amazon SES](https://aws.amazon.com/ses/) - Cost-effective for high volume
21+
- [Postmark](https://postmarkapp.com/) - Focused on transactional email delivery
22+
- [SMTP2GO](https://www.smtp2go.com/) - Simple SMTP service with API options
23+
24+
## Example: Sending Emails with Mailgun
25+
26+
Here's a complete example using Mailgun's API to send emails through SQLPage:
27+
28+
```sql
29+
-- Configure the email request
30+
set email_request = json_object(
31+
'url', 'https://api.mailgun.net/v3/' || sqlpage.environment_variable('MAILGUN_DOMAIN') || '/messages',
32+
'method', 'POST',
33+
'headers', json_object(
34+
'Authorization', 'Basic ' || encode(('api:' || sqlpage.environment_variable('MAILGUN_API_KEY'))::bytea, 'base64')
35+
),
36+
'body',
37+
'from=Sender Name <noreply@' || sqlpage.environment_variable('MAILGUN_DOMAIN') || '>'
38+
|| '&to=' || $to_email
39+
|| '&subject=' || $subject
40+
|| '&text=' || $message_text
41+
|| '&html=' || $message_html
42+
);
43+
44+
-- Send the email using sqlpage.fetch
45+
set email_response = sqlpage.fetch($email_request);
46+
47+
-- Handle the response
48+
select
49+
'alert' as component,
50+
case
51+
when $email_response->>'id' is not null then 'Email sent successfully'
52+
else 'Failed to send email: ' || ($email_response->>'message')
53+
end as title;
54+
```
55+
56+
### Setup Instructions
57+
58+
1. Sign up for a [Mailgun account](https://signup.mailgun.com/new/signup)
59+
2. Verify your domain or use the sandbox domain for testing
60+
3. Get your API key from the Mailgun dashboard
61+
4. Set these environment variables in your SQLPage configuration:
62+
```
63+
MAILGUN_API_KEY=your-api-key-here
64+
MAILGUN_DOMAIN=your-domain.com
65+
```
66+
67+
## Best Practices
68+
69+
- If you share your code with others, it should not contain sensitive data like API keys
70+
- Instead, use environment variables with [`sqlpage.environment_variable`](https://sql-page.com/functions.sql?function=environment_variable)
71+
- Implement proper error handling
72+
- Consider rate limiting for bulk sending
73+
- Include unsubscribe links when sending marketing emails
74+
- Follow email regulations (GDPR, CAN-SPAM Act)

examples/sending emails/email.sql

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
-- Configure the email request
2+
3+
-- Obtain the authorization by encoding "api:YOUR_PERSONAL_API_KEY" in base64
4+
set authorization = 'YXBpOjI4ODlmODE3Njk5ZjZiNzA4MTdhODliOGUwODYyNmEyLWU2MWFlOGRkLTgzMjRjYWZm';
5+
6+
-- Find the domain in your Mailgun account
7+
set domain = 'sandbox859545b401674a95b906ab417d48c97c.mailgun.org';
8+
9+
-- Set the recipient email address.
10+
--In this demo, we accept sending any email to any address.
11+
-- If you do this in production, spammers WILL use your account to send spam.
12+
-- Your application should only allow emails to be sent to addresses you have verified.
13+
set to_email = :to_email;
14+
15+
-- Set the email subject
16+
set subject = :subject;
17+
18+
-- Set the email message text
19+
set message_text = :message_text;
20+
21+
set email_request = json_object(
22+
'url', 'https://api.mailgun.net/v3/' || $domain || '/messages',
23+
'method', 'POST',
24+
'headers', json_object(
25+
'Content-Type', 'application/x-www-form-urlencoded',
26+
'Authorization', 'Basic ' || $authorization
27+
),
28+
'body',
29+
'from=Your Name <noreply@' || $domain || '>'
30+
|| '&to=' || sqlpage.url_encode($to_email)
31+
|| '&subject=' || sqlpage.url_encode($subject)
32+
|| '&text=' || sqlpage.url_encode($message_text)
33+
);
34+
-- Send the email using sqlpage.fetch
35+
set email_response = sqlpage.fetch($email_request);
36+
37+
-- Handle the response
38+
select
39+
'alert' as component,
40+
case
41+
when $email_response->>'id' is not null then 'Email sent successfully'
42+
else 'Failed to send email: ' || ($email_response->>'message')
43+
end as title;

examples/sending emails/index.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
select 'form' as component, 'Send an email' as title, 'email.sql' as action;
2+
3+
select 'to_email' as name, 'To email' as label, '[email protected]' as value;
4+
select 'subject' as name, 'Subject' as label, 'Test email' as value;
5+
select 'textarea' as type, 'message_text' as name, 'Message' as label, 'This is a test email' as value;

0 commit comments

Comments
 (0)