Skip to main content

Mail

Vania provides a mail system for sending transactional emails through SMTP. Emails can use raw content or template-rendered views.

Configuration

Set mail credentials in .env:

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=587
MAIL_ENCRYPTION=tls
MAIL_USERNAME=your_username
MAIL_PASSWORD=your_password
[email protected]
MAIL_FROM_NAME=MyApp

Writing a Mailable

Create a class that extends Mailable:

import 'package:vania/mail.dart';

class WelcomeEmail extends Mailable {
final String userName;

WelcomeEmail(this.userName);

@override
Envelope envelope() {
return Envelope(
from: Address('[email protected]', 'MyApp'),
to: [Address('[email protected]', userName)],
subject: 'Welcome to MyApp',
);
}

@override
Content content() {
return Content(
html: '<h1>Welcome, $userName!</h1><p>Thanks for joining.</p>',
);
}
}

Using Templates

Render a view template for the email body:

@override
Content content() {
return Content(
view: MailView('emails/welcome', {'name': userName}),
);
}

This renders views/emails/welcome.html through the template engine.

Attachments

@override
List<Attachment> attachments() {
return [
Attachment(
filename: 'guide.pdf',
data: guideBytes,
),
];
}

Sending Mail

import 'package:vania/mail.dart';

await WelcomeEmail('Alice').send();

Or from a controller:

Future<Response> register(Request req) async {
// ... create user ...
await WelcomeEmail(req.input('name') as String).send();
return Response.json({'message': 'Registered'}, 201);
}

Envelope Options

The Envelope class supports:

Envelope(
from: Address('[email protected]', 'MyApp'),
to: [Address('[email protected]')],
cc: [Address('[email protected]')],
bcc: [Address('[email protected]')],
subject: 'Your Order Confirmation',
replyTo: Address('[email protected]'),
);

Generating a Mailable via CLI

vania make:mail order_confirmation