Sending Emails
With Wasp's email-sending feature, you can easily integrate email functionality into your web application.
app Example {
...
emailSender: {
provider: <provider>,
defaultFrom: {
name: "Example",
email: "hello@itsme.com"
},
}
}
Choose from one of the providers:
Dummy(development only),Mailgun,SendGrid- or the good old
SMTP.
Optionally, define the defaultFrom field, so you don't need to provide it whenever sending an email.
Sending Emails
Before jumping into details about setting up various providers, let's see how easy it is to send emails.
You import the emailSender that is provided by the wasp/server/email module and call the send method on it.
- JavaScript
- TypeScript
import { emailSender } from "wasp/server/email";
// In some action handler...
const info = await emailSender.send({
from: {
name: "John Doe",
email: "john@doe.com",
},
to: "user@domain.com",
subject: "Saying hello",
text: "Hello world",
html: "Hello <strong>world</strong>",
});
import { emailSender } from "wasp/server/email";
// In some action handler...
const info = await emailSender.send({
from: {
name: "John Doe",
email: "john@doe.com",
},
to: "user@domain.com",
subject: "Saying hello",
text: "Hello world",
html: "Hello <strong>world</strong>",
});
Read more about the send method in the API Reference.
The send method returns an object with the status of the sent email. It varies depending on the provider you use.
Providers
We'll go over all of the available providers in the next section. For some of them, you'll need to set up some env variables. You can do that in the .env.server file.
Using the Dummy Provider
The Dummy provider is not for production use. It is only meant to be used during development. If you try building your app with the Dummy provider, the build will fail.
To speed up development, Wasp offers a Dummy email sender that console.logs the emails in the console. Since it doesn't send emails for real, it doesn't require any setup.
Set the provider to Dummy in your main.wasp file.
app Example {
...
emailSender: {
provider: Dummy,
}
}
Using the SMTP Provider
First, set the provider to SMTP in your main.wasp file.
app Example {
...
emailSender: {
provider: SMTP,
}
}
Then, add the following env variables to your .env.server file.
SMTP_HOST=
SMTP_USERNAME=
SMTP_PASSWORD=
SMTP_PORT=
Many transactional email providers (e.g. Mailgun, SendGrid but also others) can also use SMTP, so you can use them as well.
Using the Mailgun Provider
Set the provider to Mailgun in the main.wasp file.
app Example {
...
emailSender: {
provider: Mailgun,
}
}
Then, get the Mailgun API key and domain and add them to your .env.server file.
Getting the API Key and Domain
- Go to Mailgun and create an account.
- Go to Domains and create a new domain.
- Copy the domain and add it to your
.env.serverfile. - Create a new Sending API key under
Send > Sending > Domain settingsand findSending API keys. - Copy the API key and add it to your
.env.serverfile.
MAILGUN_API_KEY=
MAILGUN_DOMAIN=
Using the EU Region
If your domain region is in the EU, you need to set the MAILGUN_API_URL variable in your .env.server file:
MAILGUN_API_URL=https://api.eu.mailgun.net
Using the SendGrid Provider
Set the provider field to SendGrid in your main.wasp file.
app Example {
...
emailSender: {
provider: SendGrid,
}
}
Then, get the SendGrid API key and add it to your .env.server file.
Getting the API Key
- Go to SendGrid and create an account.
- Go to API Keys and create a new API key.
- Copy the API key and add it to your
.env.serverfile.
SENDGRID_API_KEY=
API Reference
emailSender dict
app Example {
...
emailSender: {
provider: <provider>,
defaultFrom: {
name: "Example",
email: "hello@itsme.com"
},
}
}
The emailSender dict has the following fields:
-
provider: ProviderrequiredThe provider you want to use. Choose from
Dummy,SMTP,MailgunorSendGrid.Dummy Provider is not for production useThe
Dummyprovider is not for production use. It is only meant to be used during development. If you try building your app with theDummyprovider, the build will fail. -
defaultFrom: dictThe default sender's details. If you set this field, you don't need to provide the
fromfield when sending an email.
JavaScript API
Using the emailSender in :
- JavaScript
- TypeScript
import { emailSender } from "wasp/server/email";
// In some action handler...
const info = await emailSender.send({
from: {
name: "John Doe",
email: "john@doe.com",
},
to: "user@domain.com",
subject: "Saying hello",
text: "Hello world",
html: "Hello <strong>world</strong>",
});
import { emailSender } from "wasp/server/email";
// In some action handler...
const info = await emailSender.send({
from: {
name: "John Doe",
email: "john@doe.com",
},
to: "user@domain.com",
subject: "Saying hello",
text: "Hello world",
html: "Hello <strong>world</strong>",
});
The send method accepts an object with the following fields:
-
from: objectThe sender's details. If you set up
defaultFromfield in theemailSenderdict in Wasp file, this field is optional.-
name: stringThe name of the sender.
-
email: stringThe email address of the sender.
-
-
to: stringrequiredThe recipient's email address.
-
subject: stringrequiredThe subject of the email.
-
text: stringrequiredThe text version of the email.
-
html: stringrequiredThe HTML version of the email