web/versioned_docs/version-0.14/advanced/email/email.md
import { Required } from '@site/src/components/Tag' import { ShowForTs, ShowForJs } from '@site/src/components/TsJsHelpers' import DummyProviderNote from './_dummy-provider-note.md'
With Wasp's email-sending feature, you can easily integrate email functionality into your web application.
<Tabs groupId="js-ts"> <TabItem value="js" label="JavaScript"> ```wasp title="main.wasp" app Example { ... emailSender: { provider: <provider>, defaultFrom: { name: "Example", email: "[email protected]" }, } } ``` </TabItem> <TabItem value="ts" label="TypeScript"> ```wasp title="main.wasp" app Example { ... emailSender: { provider: <provider>, defaultFrom: { name: "Example", email: "[email protected]" }, } } ``` </TabItem> </Tabs>Choose from one of the providers:
Dummy (development only),Mailgun,SendGridSMTP.Optionally, define the defaultFrom field, so you don't need to provide it whenever sending an email.
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.
// 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>",
});
```
// 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.
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.
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.
First, set the provider to SMTP in your main.wasp file.
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.
Set the provider to Mailgun in the main.wasp file.
Then, get the Mailgun API key and domain and add them to your .env.server file.
.env.server file..env.server file.MAILGUN_API_KEY=
MAILGUN_DOMAIN=
Set the provider field to SendGrid in your main.wasp file.
Then, get the SendGrid API key and add it to your .env.server file.
.env.server file.SENDGRID_API_KEY=
emailSender dictThe emailSender dict has the following fields:
provider: Provider <Required />
The provider you want to use. Choose from Dummy, SMTP, Mailgun or SendGrid.
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.
Using the emailSender in <ShowForTs>Typescript</ShowForTs><ShowForJs>JavaScript</ShowForJs>:
// 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>",
});
```
// 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 the emailSender 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 <Required />
The recipient's email address.
subject: string <Required />
The subject of the email.
text: string <Required />
The text version of the email.
html: string <Required />
The HTML version of the email