All restdb.io databases have a REST endpoint for sending emails.
Email sending is relayed and copies are stored in a database system collection called email_outbound
.
Set up your own account at Mailgun. They have a free trial plan. After you've added your account, add your domain and apikey in the Settings. That's all there is to do.
All outbound email from your database will now be securely relayed via your own Mailgun account.
{
"mailgun": {
"domain": "mydomain.com",
"apikey": "xxxyyyzzz"
}
}
This example sends an email to a client from an imaginary database mydatabase-ad0c
.
The REST endpoint for sending email would then be:
https://mydatabase-ad0c.restdb.io/mail
The request.body should contain the email fields that are used to send the mail.
Request body example:
{
"to":"jonnie.appleseed@company.com",
"subject":"Your order is ready",
"html": "<p>Lorem ipsum dolor..., <b>vel</b> luctu.</p>",
"company": "Acme Inc",
"sendername": "Acme customer support"
}
HTTP example:
POST /mail HTTP/1.1
Host: mydatabase-ad0c.restdb.io
Content-Type: application/json
x-apikey: 588ca78ff2c06c15467a12cc
Cache-Control: no-cache
{"to":"jonnie.appleseed@gmail.com","subject":"Your order is ready", "html": "<p>Lorem ipsum dolor sit amet... suscipit quis, <b>vel</b> luctu.</p>", "company": "Acme Inc", "sendername": "Acme customer support"}
The output from the example above looks something like this screenshot:
Http Verb | Resource | Functionality |
---|---|---|
POST | https://<dburl>.restdb.io/mail | Send email. Request body contains one JSON document, or array (max 1000) of JSON documents:{"to": "...", "subject": "...", "html": "..." ,[options]} |
Allowed properties in a mail JSON body:
Option | Example | Comment |
---|---|---|
to | "to": "jonnie@xmail.com" | One email address. Must be a valid email address. Not allowed with multiple spearated with space, comma or semicolon. |
subject | "subject": "Important subject" | Email subject field. |
html | "html": "<p>Hi there …</p>" | A html email message. When using a custom template this value must be a JSON document. E.g. {"body": "<p>Hi there …</p>", "a-field": "a-value", …} |
company | "company": "Acme" | Add the company name to the default template footer. |
sendername | "sendername": "Customer support" | Hides the from email address, and use a friendly name instead. Works in most email client applications. |
transport | "transport": "Gmail" | Use Gmail SMTP as transport. Require a paid plan. |
template | "template": "<MyPageName>" | Use a Custom Page as email template. Replaces {{name}} with values from the html property. Require a paid plan. |
Pages can be used to automate and customize your emails. Pages are plain HTML with Handlebars tags for dynamic fields.
E.g. this simple Page template:
Sending emails with templates is an extra option ("template": "<name>"
) provided in the API. It's also important to notice that the email message is not a text string, but a JSON object with the dynamic fields that will be replaced with the Page template field.
{
"to":"jones@restdb.io",
"subject":"Trigger email from you database",
"html": {"name":"Jones", "body": "..."},
"company": "Acme Inc",
"sendername": "Acme support",
"template": "MyTemplate"
}
In this example, we've used an email template from zurb.com. The resulting email will then look something like this:
You can also use your own Gmail account for sending email (only for paid plans). Follow the steps below to set this up.
To use Google as smtp transport you need to store your username and passord in the database settings. In Development mode, navigate to the Settings tab (from the top level) and add the following JSON setting:
{
"restdb": {
"Gmail-service": {
"auth": {
"user": "<your gmail email address>",
"pass": "<your gmail password>"
}
}
}
}
Gmail does not accept to relay smtp email based on username and password by default. To override this, navigate on your Gmail account to: My account / Sign-in & security / Connected apps & sites. Then click the setting to ON for allowing access from your database:
Important: we strongly recommend to create a separate Gmail account for relaying smtp email.
The only difference from sending mail with Gmail is that you need to provide this as a property on the mail JSON document:
{
"transport": "gmail",
"to":"jonnie.appleseed@company.com",
"subject":"Your order is done",
"html": "<h2>Lorem ipsum dolor</h2><p>..., <b>vel</b> luctu.</p><p>Greetings from the restdb.io team :)</p>",
"company": "Acme Inc",
"sendername": "Acme customer support"
}
This example email results in the following email to the user: