|
| 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) |
0 commit comments