Sending Emails
With Wasp's email sending feature, you can easily integrate email functionality into your web application.
- JavaScript
- TypeScript
app Example {
...
emailSender: {
provider: <provider>,
defaultFrom: {
name: "Example",
email: "[email protected]"
},
}
}
app Example {
...
emailSender: {
provider: <provider>,
defaultFrom: {
name: "Example",
email: "[email protected]"
},
}
}
Choose from one of the providers:
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
When you run your app in development mode, the emails are not sent. Instead, they are logged to the console.
To enable sending emails in development mode, you need to set the SEND_EMAILS_IN_DEVELOPMENT
env variable to true
in your .env.server
file.
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/email
module and call the send
method on it.
- JavaScript
- TypeScript
import { emailSender } from "@wasp/email/index.js";
// In some action handler...
const info = await emailSender.send({
from: {
name: "John Doe",
email: "[email protected]",
},
to: "[email protected]",
subject: "Saying hello",
text: "Hello world",
html: "Hello <strong>world</strong>",
});
import { emailSender } from "@wasp/email/index.js";
// In some action handler...
const info = await emailSender.send({
from: {
name: "John Doe",
email: "[email protected]",
},
to: "[email protected]",
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
For each provider, you'll need to set up env variables in the .env.server
file at the root of your project.
Using the SMTP Provider
First, set the provider to SMTP
in your main.wasp
file.
- JavaScript
- TypeScript
app Example {
...
emailSender: {
provider: SMTP,
}
}
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.
- JavaScript
- TypeScript
app Example {
...
emailSender: {
provider: Mailgun,
}
}
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 API Keys and create a new API key.
- Copy the API key and add it to your
.env.server
file. - Go to Domains and create a new domain.
- Copy the domain and add it to your
.env.server
file.
MAILGUN_API_KEY=
MAILGUN_DOMAIN=
Using the SendGrid Provider
Set the provider field to SendGrid
in your main.wasp
file.
- JavaScript
- TypeScript
app Example {
...
emailSender: {
provider: SendGrid,
}
}
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.server
file.
SENDGRID_API_KEY=
API Reference
emailSender
dict
- JavaScript
- TypeScript
app Example {
...
emailSender: {
provider: <provider>,
defaultFrom: {
name: "Example",
email: "[email protected]"
},
}
}
app Example {
...
emailSender: {
provider: <provider>,
defaultFrom: {
name: "Example",
email: "[email protected]"
},
}
}
The emailSender
dict has the following fields:
provider: Provider
requiredThe provider you want to use. Choose from
SMTP
,Mailgun
orSendGrid
.defaultFrom: dict
The default sender's details. If you set this field, you don't need to provide the
from
field when sending an email.
JavaScript API
Using the emailSender
in :
- JavaScript
- TypeScript
import { emailSender } from "@wasp/email/index.js";
// In some action handler...
const info = await emailSender.send({
from: {
name: "John Doe",
email: "[email protected]",
},
to: "[email protected]",
subject: "Saying hello",
text: "Hello world",
html: "Hello <strong>world</strong>",
});
import { emailSender } from "@wasp/email/index.js";
// In some action handler...
const info = await emailSender.send({
from: {
name: "John Doe",
email: "[email protected]",
},
to: "[email protected]",
subject: "Saying hello",
text: "Hello world",
html: "Hello <strong>world</strong>",
});
The send
method accepts an object with the following fields:
from: object
The sender's details. If you set up
defaultFrom
field in theemailSender
dict in Wasp file, this field is optional.name: string
The name of the sender.
email: string
The email address of the sender.
to: string
requiredThe recipient's email address.
subject: string
requiredThe subject of the email.
text: string
requiredThe text version of the email.
html: string
requiredThe HTML version of the email