Mails
SveltePak includes support for sending emails.
To get started you just need to uncomment the mail provider you’d like to use in the src/modules/mail/index.ts
file.
export * from "./nodemailer";
//export * from './sendgrid'
Usage
To send an email, you can use the sendMail
function from the mail
package.
Please note that sendMail only works on the server. If you wish to send emails from the client, you will need to create an API endpoint / tRPC that sends the email.
interface SendMailOptions {
from?: string;
to: string;
subject: string;
text: string;
html: string;
options?: NodeMailerOptions;
}
export const sendMail = async (options: SendMailOptions) => {
// ...
};
Example
import { sendMail } from "$modules/mail";
await sendMail({
from: "sveltepak@sveltepak.com",
to: "example@example.com",
subject: "Hello",
text: "Hello world",
html: "<p>Hello world</p>"
});
Templates
We use svelte-email
to render email templates.
You can find the templates in the modules/mail/tpls
directory.
To render a template, you can use the render
function from the svelte-email
library.
import { render } from "svelte-email";
import { Otp } from "$modules/mail/tpls";
const html = await render({
template: Otp,
props
});
const plainText = await render({
template: Otp,
props,
options: {
plainText: true
}
});
sendMail({
to: input.email,
subject: `${APP_NAME}: ${otpCode} is your OTP code`,
html: html,
text: plainText
});
Providers
Nodemailer
To get started with Nodemailer, you need to set the following environment variables:
DEFAULT_FROM_ADDRESS = "ACME Support <support@example.com>";
SMTP_HOST = smtp.ethereal.email;
SMTP_PORT = 587;
SMTP_SECURE = false;
SMTP_USER = "matilda.christiansen@ethereal.email";
SMTP_PASS = "R8KRYTsHmG3s8MCrZX";
Alternatively, you can pass options to the sendMail
function.
import { sendMail } from "$modules/mail";
sendMail({
from: "",
to: "",
subject:"",
text: "",
html: "",
options: {
host: "smtp.ethereal.email",
port: 587,
secure: false,
auth: {
user: "",
pass: "",
}
});
SendGrid
Coming soon.