Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
INSERT INTO sqlpage_functions (
"name",
"return_type",
"introduced_in_version",
"icon",
"description_md"
)
VALUES (
'send_mail',
'JSON',
'0.45.0',
'mail',
'Sends a plain-text email using the outgoing mail server configured in SQLPage.
---
icon: mail
since: 0.45.0
category: external-services
summary: Send email with optional attachments through an SMTP server.
return_type: JSON
parameters:
- name: message
type: JSON
required: true
description: '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.'
---

Sends a plain-text email using the outgoing mail server configured in SQLPage.

### Quick start

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

```sql
set result = sqlpage.send_mail(json_object(
''to'', ''alice@example.com'',
''subject'', ''Hello from SQLPage'',
''body'', ''Your first email is working!''
'to', 'alice@example.com',
'subject', 'Hello from SQLPage',
'body', 'Your first email is working!'
));
```

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

```json
{"status":"error","error_code":"INVALID_EMAIL_TO","error":"''xxx'' is not a valid to email address"}
{"status":"error","error_code":"INVALID_EMAIL_TO","error":"'xxx' is not a valid to email address"}
```

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:

```sql
select ''alert'' as component,
case when json_extract($result, ''$.status'') = ''accepted'' then ''success'' else ''danger'' end as color,
case when json_extract($result, ''$.status'') = ''accepted'' then ''Email sent'' else ''Email could not be sent'' end as title,
json_extract($result, ''$.error'') as description;
select 'alert' as component,
case when json_extract($result, '$.status') = 'accepted' then 'success' else 'danger' end as color,
case when json_extract($result, '$.status') = 'accepted' then 'Email sent' else 'Email could not be sent' end as title,
json_extract($result, '$.error') as description;
```

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

```sql
set result = sqlpage.send_mail(json_object(
''to'', json_array(''alice@example.com'', ''bob@example.com''),
''cc'', ''team@example.com'',
''subject'', ''Monthly report'',
''body'', ''The report is attached.'',
''attachments'', json_array(json_object(
''filename'', ''report.pdf'',
''data_url'', sqlpage.read_file_as_data_url(''report.pdf'')
'to', json_array('alice@example.com', 'bob@example.com'),
'cc', 'team@example.com',
'subject', 'Monthly report',
'body', 'The report is attached.',
'attachments', json_array(json_object(
'filename', 'report.pdf',
'data_url', sqlpage.read_file_as_data_url('report.pdf')
))
));
```
Expand All @@ -158,10 +167,10 @@ Set `body_html` to send an HTML version of the message alongside the plain-text

```sql
set result = sqlpage.send_mail(json_object(
''to'', ''alice@example.com'',
''subject'', ''Welcome'',
''body'', ''Welcome to our service.'',
''body_html'', ''<p>Welcome to <strong>our service</strong>.</p>''
'to', 'alice@example.com',
'subject', 'Welcome',
'body', 'Welcome to our service.',
'body_html', '<p>Welcome to <strong>our service</strong>.</p>'
));
```

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

```sql
set result = sqlpage.send_mail(json_object(
''to'', ''alice@example.com'',
''subject'', ''Welcome'',
''body_md'', ''# Welcome\n\nWelcome to **our service**.''
'to', 'alice@example.com',
'subject', 'Welcome',
'body_md', '# Welcome\n\nWelcome to **our service**.'
));
```

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

```sql
select ''form'' as component, ''post'' as method;
select ''email'' as name, ''email'' as type, true as required;
select ''message'' as name, ''textarea'' as type, true as required;
select 'form' as component, 'post' as method;
select 'email' as name, 'email' as type, true as required;
select 'message' as name, 'textarea' as type, true as required;

set mail = json_object(
''to'', ''admin@example.com'',
''reply_to'', :email,
''subject'', ''Website contact form'',
''body'', :message
'to', 'admin@example.com',
'reply_to', :email,
'subject', 'Website contact form',
'body', :message
);
set result = (
select sqlpage.send_mail($mail)
where :message is not null
);

select ''alert'' as component,
case when json_extract($result, ''$.status'') = ''accepted'' then ''success'' else ''danger'' end as color,
case when json_extract($result, ''$.status'') = ''accepted'' then ''Message sent'' else ''Message could not be sent'' end as title,
json_extract($result, ''$.error'') as description
select 'alert' as component,
case when json_extract($result, '$.status') = 'accepted' then 'success' else 'danger' end as color,
case when json_extract($result, '$.status') = 'accepted' then 'Message sent' else 'Message could not be sent' end as title,
json_extract($result, '$.error') as description
where :message is not null;
```

Expand Down Expand Up @@ -279,20 +288,3 @@ SQLPage does not currently support:
| `SMTP_TIMEOUT` | The SMTP operation timed out. |
| `SMTP_REJECTED` | The SMTP server returned a temporary or permanent rejection, including authentication failures. |
| `SMTP_CONNECTION_FAILED` | SQLPage could not connect to or communicate with the SMTP server. |
'
);

INSERT INTO sqlpage_function_parameters (
"function",
"index",
"name",
"description_md",
"type"
)
VALUES (
'send_mail',
1,
'message',
'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.',
'JSON'
);
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ WITH copy(name, category, summary) AS (VALUES



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

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