You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 9aa852b
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: examples/official-site/content/functions/send_mail.md
+55-63Lines changed: 55 additions & 63 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff 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.
14
23
15
24
### Quick start
16
25
@@ -35,9 +44,9 @@ You can now send an email from any SQL file:
35
44
36
45
```sql
37
46
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!'
41
50
));
42
51
```
43
52
@@ -50,16 +59,16 @@ The sender comes from `smtp_from`. The result is a JSON object:
50
59
If the message cannot be sent, the function returns the reason instead of stopping the request:
51
60
52
61
```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"}
54
63
```
55
64
56
65
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:
57
66
58
67
```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;
63
72
```
64
73
65
74
### SMTP provider settings
@@ -137,13 +146,13 @@ Each attachment has a file name and a [data URL](https://developer.mozilla.org/e
@@ -158,10 +167,10 @@ Set `body_html` to send an HTML version of the message alongside the plain-text
158
167
159
168
```sql
160
169
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>'
165
174
));
166
175
```
167
176
@@ -173,9 +182,9 @@ Set `body_md` to send a [Markdown](https://daringfireball.net/projects/markdown/
173
182
174
183
```sql
175
184
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**.'
179
188
));
180
189
```
181
190
@@ -186,25 +195,25 @@ SQLPage renders Markdown using the same [GFM](https://github.github.com/gfm/) op
186
195
### Contact form
187
196
188
197
```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;
192
201
193
202
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
198
207
);
199
208
set result = (
200
209
selectsqlpage.send_mail($mail)
201
210
where :message is not null
202
211
);
203
212
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
208
217
where :message is not null;
209
218
```
210
219
@@ -279,20 +288,3 @@ SQLPage does not currently support:
279
288
|`SMTP_TIMEOUT`| The SMTP operation timed out. |
280
289
|`SMTP_REJECTED`| The SMTP server returned a temporary or permanent rejection, including authentication failures. |
281
290
|`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.',
0 commit comments