Skip to content

Commit 139688b

Browse files
committed
Make the send_mail reference editable as Markdown
1 parent 8cf5e60 commit 139688b

2 files changed

Lines changed: 55 additions & 64 deletions

File tree

examples/official-site/sqlpage/migrations/75_send_mail.sql renamed to examples/official-site/content/functions/send_mail.md

Lines changed: 55 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
INSERT INTO sqlpage_functions (
2-
"name",
3-
"return_type",
4-
"introduced_in_version",
5-
"icon",
6-
"description_md"
7-
)
8-
VALUES (
9-
'send_mail',
10-
'JSON',
11-
'0.45.0',
12-
'mail',
13-
'Sends a plain-text email using the outgoing mail server configured in SQLPage.
1+
---
2+
icon: mail
3+
since: 0.45.0
4+
category: external-services
5+
summary: Send email with optional attachments through an SMTP server.
6+
return_type: JSON
7+
parameters:
8+
- name: message
9+
type: JSON
10+
required: true
11+
description: 'A JSON object containing the email to send. Required properties are `to` (an address or non-empty
12+
address array), `subject`, and either `body` or `body_md`. Optional properties are `from` (required unless
13+
`smtp_from` or `SMTP_FROM` is configured), `reply_to`, `cc` (an address or non-empty address array),
14+
`body` (the plain-text body; required unless `body_md` is provided), `body_html` (an HTML alternative
15+
body sent as `multipart/alternative` alongside `body`), `body_md` (a Markdown body rendered to HTML and
16+
sent as `multipart/alternative` with the raw Markdown as the plain-text alternative; cannot be combined
17+
with `body_html`), and `attachments` (an array of `{ "filename": "...", "data_url": "data:..." }` objects).
18+
Invalid JSON and invalid or unknown properties return `{ "status": "error", "error_code": "...", "error":
19+
"..." }`. SQL `NULL` returns SQL `NULL` without sending.'
20+
---
21+
22+
Sends a plain-text email using the outgoing mail server configured in SQLPage.
1423

1524
### Quick start
1625

@@ -35,9 +44,9 @@ You can now send an email from any SQL file:
3544

3645
```sql
3746
set result = sqlpage.send_mail(json_object(
38-
''to'', ''alice@example.com'',
39-
''subject'', ''Hello from SQLPage'',
40-
''body'', ''Your first email is working!''
47+
'to', 'alice@example.com',
48+
'subject', 'Hello from SQLPage',
49+
'body', 'Your first email is working!'
4150
));
4251
```
4352

@@ -50,16 +59,16 @@ The sender comes from `smtp_from`. The result is a JSON object:
5059
If the message cannot be sent, the function returns the reason instead of stopping the request:
5160

5261
```json
53-
{"status":"error","error_code":"INVALID_EMAIL_TO","error":"''xxx'' is not a valid to email address"}
62+
{"status":"error","error_code":"INVALID_EMAIL_TO","error":"'xxx' is not a valid to email address"}
5463
```
5564

5665
For every non-`NULL` call, `status` is either `accepted` or `error`. Always check it before showing a success message or continuing work that depends on the email:
5766

5867
```sql
59-
select ''alert'' as component,
60-
case when json_extract($result, ''$.status'') = ''accepted'' then ''success'' else ''danger'' end as color,
61-
case when json_extract($result, ''$.status'') = ''accepted'' then ''Email sent'' else ''Email could not be sent'' end as title,
62-
json_extract($result, ''$.error'') as description;
68+
select 'alert' as component,
69+
case when json_extract($result, '$.status') = 'accepted' then 'success' else 'danger' end as color,
70+
case when json_extract($result, '$.status') = 'accepted' then 'Email sent' else 'Email could not be sent' end as title,
71+
json_extract($result, '$.error') as description;
6372
```
6473

6574
### SMTP provider settings
@@ -137,13 +146,13 @@ Each attachment has a file name and a [data URL](https://developer.mozilla.org/e
137146

138147
```sql
139148
set result = sqlpage.send_mail(json_object(
140-
''to'', json_array(''alice@example.com'', ''bob@example.com''),
141-
''cc'', ''team@example.com'',
142-
''subject'', ''Monthly report'',
143-
''body'', ''The report is attached.'',
144-
''attachments'', json_array(json_object(
145-
''filename'', ''report.pdf'',
146-
''data_url'', sqlpage.read_file_as_data_url(''report.pdf'')
149+
'to', json_array('alice@example.com', 'bob@example.com'),
150+
'cc', 'team@example.com',
151+
'subject', 'Monthly report',
152+
'body', 'The report is attached.',
153+
'attachments', json_array(json_object(
154+
'filename', 'report.pdf',
155+
'data_url', sqlpage.read_file_as_data_url('report.pdf')
147156
))
148157
));
149158
```
@@ -158,10 +167,10 @@ Set `body_html` to send an HTML version of the message alongside the plain-text
158167

159168
```sql
160169
set result = sqlpage.send_mail(json_object(
161-
''to'', ''alice@example.com'',
162-
''subject'', ''Welcome'',
163-
''body'', ''Welcome to our service.'',
164-
''body_html'', ''<p>Welcome to <strong>our service</strong>.</p>''
170+
'to', 'alice@example.com',
171+
'subject', 'Welcome',
172+
'body', 'Welcome to our service.',
173+
'body_html', '<p>Welcome to <strong>our service</strong>.</p>'
165174
));
166175
```
167176

@@ -173,9 +182,9 @@ Set `body_md` to send a [Markdown](https://daringfireball.net/projects/markdown/
173182

174183
```sql
175184
set result = sqlpage.send_mail(json_object(
176-
''to'', ''alice@example.com'',
177-
''subject'', ''Welcome'',
178-
''body_md'', ''# Welcome\n\nWelcome to **our service**.''
185+
'to', 'alice@example.com',
186+
'subject', 'Welcome',
187+
'body_md', '# Welcome\n\nWelcome to **our service**.'
179188
));
180189
```
181190

@@ -186,25 +195,25 @@ SQLPage renders Markdown using the same [GFM](https://github.github.com/gfm/) op
186195
### Contact form
187196

188197
```sql
189-
select ''form'' as component, ''post'' as method;
190-
select ''email'' as name, ''email'' as type, true as required;
191-
select ''message'' as name, ''textarea'' as type, true as required;
198+
select 'form' as component, 'post' as method;
199+
select 'email' as name, 'email' as type, true as required;
200+
select 'message' as name, 'textarea' as type, true as required;
192201

193202
set mail = json_object(
194-
''to'', ''admin@example.com'',
195-
''reply_to'', :email,
196-
''subject'', ''Website contact form'',
197-
''body'', :message
203+
'to', 'admin@example.com',
204+
'reply_to', :email,
205+
'subject', 'Website contact form',
206+
'body', :message
198207
);
199208
set result = (
200209
select sqlpage.send_mail($mail)
201210
where :message is not null
202211
);
203212

204-
select ''alert'' as component,
205-
case when json_extract($result, ''$.status'') = ''accepted'' then ''success'' else ''danger'' end as color,
206-
case when json_extract($result, ''$.status'') = ''accepted'' then ''Message sent'' else ''Message could not be sent'' end as title,
207-
json_extract($result, ''$.error'') as description
213+
select 'alert' as component,
214+
case when json_extract($result, '$.status') = 'accepted' then 'success' else 'danger' end as color,
215+
case when json_extract($result, '$.status') = 'accepted' then 'Message sent' else 'Message could not be sent' end as title,
216+
json_extract($result, '$.error') as description
208217
where :message is not null;
209218
```
210219

@@ -279,20 +288,3 @@ SQLPage does not currently support:
279288
| `SMTP_TIMEOUT` | The SMTP operation timed out. |
280289
| `SMTP_REJECTED` | The SMTP server returned a temporary or permanent rejection, including authentication failures. |
281290
| `SMTP_CONNECTION_FAILED` | SQLPage could not connect to or communicate with the SMTP server. |
282-
'
283-
);
284-
285-
INSERT INTO sqlpage_function_parameters (
286-
"function",
287-
"index",
288-
"name",
289-
"description_md",
290-
"type"
291-
)
292-
VALUES (
293-
'send_mail',
294-
1,
295-
'message',
296-
'A JSON object containing the email to send. Required properties are `to` (an address or non-empty address array), `subject`, and either `body` or `body_md`. Optional properties are `from` (required unless `smtp_from` or `SMTP_FROM` is configured), `reply_to`, `cc` (an address or non-empty address array), `body` (the plain-text body; required unless `body_md` is provided), `body_html` (an HTML alternative body sent as `multipart/alternative` alongside `body`), `body_md` (a Markdown body rendered to HTML and sent as `multipart/alternative` with the raw Markdown as the plain-text alternative; cannot be combined with `body_html`), and `attachments` (an array of `{ "filename": "...", "data_url": "data:..." }` objects). Invalid JSON and invalid or unknown properties return `{ "status": "error", "error_code": "...", "error": "..." }`. SQL `NULL` returns SQL `NULL` without sending.',
297-
'JSON'
298-
);

‎examples/official-site/sqlpage/migrations/82_reference_catalog.sql‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ WITH copy(name, category, summary) AS (VALUES
8989

9090

9191

92-
('send_mail', 'external-services', 'Send email with optional attachments through an SMTP server.'),
9392

9493
('configuration_directory', 'server-and-runtime', 'Locate the directory for SQLPage configuration and templates.'),
9594
('current_working_directory', 'server-and-runtime', 'Get the working directory of the SQLPage server process.'),

0 commit comments

Comments
 (0)