Mail Configuration
Configure mail drivers, default settings, and environment variables for sending emails in your Khadem application.
Basic Configuration
Add mail configuration to your `config/app.dart` file.
dart
// config/app.dart
const config = {
// ... other configuration
'mail': {
// Default mail driver (smtp, mailgun, ses, postmark, log, array)
'default': env('MAIL_DRIVER', 'log'),
// Default 'from' address for all emails
'from': {
'address': env('MAIL_FROM_ADDRESS', 'noreply@example.com'),
'name': env('MAIL_FROM_NAME', 'Khadem Framework'),
},
// SMTP configuration
'smtp': {
'host': env('SMTP_HOST', 'smtp.mailtrap.io'),
'port': env.getInt('SMTP_PORT', defaultValue: 2525),
'username': env('SMTP_USERNAME'),
'password': env('SMTP_PASSWORD'),
'encryption': env('SMTP_ENCRYPTION', 'tls'), // tls, ssl, or none
'timeout': env.getInt('SMTP_TIMEOUT', defaultValue: 30),
},
// Log transport (for development)
'log': {
'channel': 'mail', // Optional: specific log channel
},
// Array transport (for testing)
'array': {
// No configuration needed
},
},
};
Environment Variables
Store sensitive mail credentials in your `.env` file.
bash
# Mail Configuration
MAIL_DRIVER=smtp
MAIL_FROM_ADDRESS=noreply@example.com
MAIL_FROM_NAME="My Application"
# SMTP Configuration
SMTP_HOST=smtp.mailtrap.io
SMTP_PORT=2525
SMTP_USERNAME=your_username
SMTP_PASSWORD=your_password
SMTP_ENCRYPTION=tls
SMTP_TIMEOUT=30
# Mailgun Configuration (optional)
MAILGUN_DOMAIN=mg.example.com
MAILGUN_API_KEY=key-xxxxxxxxxxxxx
MAILGUN_ENDPOINT=https://api.mailgun.net
# Amazon SES Configuration (optional)
AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
AWS_DEFAULT_REGION=us-east-1
AWS_SES_CONFIGURATION_SET=my-config-set
# Postmark Configuration (optional)
POSTMARK_SERVER_TOKEN=your-server-token-here
POSTMARK_MESSAGE_STREAM=outbound
SMTP Configuration
Configure SMTP for self-hosted or third-party mail servers (Gmail, Mailtrap, etc.).
dart
'smtp': {
'host': env('SMTP_HOST', 'smtp.gmail.com'),
'port': env.getInt('SMTP_PORT', defaultValue: 587),
'username': env('SMTP_USERNAME'),
'password': env('SMTP_PASSWORD'),
'encryption': env('SMTP_ENCRYPTION', 'tls'), // 'tls', 'ssl', or 'none'
'timeout': env.getInt('SMTP_TIMEOUT', defaultValue: 30),
}
// Gmail Example (.env)
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USERNAME=your-email@gmail.com
SMTP_PASSWORD=your-app-password # Use App Password, not regular password!
SMTP_ENCRYPTION=tls
// Mailtrap Example (.env) - for testing
SMTP_HOST=smtp.mailtrap.io
SMTP_PORT=2525
SMTP_USERNAME=your-mailtrap-username
SMTP_PASSWORD=your-mailtrap-password
SMTP_ENCRYPTION=tls
Popular SMTP Providers:
- Gmail: smtp.gmail.com:587 (TLS) or smtp.gmail.com:465 (SSL)
- Mailtrap: smtp.mailtrap.io:2525 (Development/Testing)
- SendGrid: smtp.sendgrid.net:587
- Mailgun: smtp.mailgun.org:587
Mailgun Configuration
Configure Mailgun for high-volume email delivery with API-based sending.
dart
'mailgun': {
'domain': env('MAILGUN_DOMAIN'), // e.g., mg.yourdomain.com
'api_key': env('MAILGUN_API_KEY'), // Your Mailgun API key
'endpoint': env('MAILGUN_ENDPOINT', 'https://api.mailgun.net'),
}
// .env
MAILGUN_DOMAIN=mg.example.com
MAILGUN_API_KEY=key-1234567890abcdef
MAILGUN_ENDPOINT=https://api.mailgun.net # or https://api.eu.mailgun.net for EU
Amazon SES Configuration
Configure Amazon Simple Email Service for cost-effective email delivery in AWS.
dart
'ses': {
'access_key_id': env('AWS_ACCESS_KEY_ID'),
'secret_access_key': env('AWS_SECRET_ACCESS_KEY'),
'region': env('AWS_DEFAULT_REGION', 'us-east-1'),
'configuration_set': env('AWS_SES_CONFIGURATION_SET'), // Optional
}
// .env
AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
AWS_DEFAULT_REGION=us-east-1
AWS_SES_CONFIGURATION_SET=my-config-set # Optional
Postmark Configuration
Configure Postmark for fast transactional email delivery.
dart
'postmark': {
'server_token': env('POSTMARK_SERVER_TOKEN'),
'message_stream': env('POSTMARK_MESSAGE_STREAM', 'outbound'),
}
// .env
POSTMARK_SERVER_TOKEN=your-server-token-here
POSTMARK_MESSAGE_STREAM=outbound # or 'broadcasts' for marketing emails
Development & Testing Configuration
Use Log and Array transports for development and testing environments.
dart
// Development environment - use 'log' driver
// config/development/app.dart
const config = {
'mail': {
'default': 'log', // Emails will be logged, not sent
'from': {
'address': 'dev@localhost',
'name': 'Dev Environment',
},
},
};
// Testing environment - use 'array' driver
// In your tests
import 'package:khadem/khadem.dart';
void main() {
test('email is sent', () async {
// ArrayTransport stores emails in memory
final transport = ArrayTransport();
final mailer = Mailer(transport);
await mailer
.to('test@example.com')
.subject('Test')
.text('Test content')
.send();
// Assert email was sent
expect(transport.wasSentTo('test@example.com'), isTrue);
});
}
Using Multiple Mailers
Configure and use different mail drivers for different purposes.
dart
// config/app.dart
const config = {
'mail': {
'default': 'smtp',
'mailers': {
// Primary mailer for transactional emails
'smtp': {
'driver': 'smtp',
'host': env('SMTP_HOST'),
'port': env.getInt('SMTP_PORT'),
'username': env('SMTP_USERNAME'),
'password': env('SMTP_PASSWORD'),
'encryption': 'tls',
},
// Secondary mailer for marketing emails
'marketing': {
'driver': 'mailgun',
'domain': env('MAILGUN_DOMAIN'),
'api_key': env('MAILGUN_API_KEY'),
},
// Mailer for system notifications
'notifications': {
'driver': 'ses',
'access_key_id': env('AWS_ACCESS_KEY_ID'),
'secret_access_key': env('AWS_SECRET_ACCESS_KEY'),
'region': 'us-east-1',
},
// Development mailer
'log': {
'driver': 'log',
},
},
'from': {
'address': env('MAIL_FROM_ADDRESS'),
'name': env('MAIL_FROM_NAME'),
},
},
};
dart
import 'package:khadem/khadem.dart';
final mailManager = container.resolve<MailManager>();
// Use default mailer (smtp)
await mailManager
.to('user@example.com')
.subject('Welcome')
.send();
// Use specific mailer for marketing
await mailManager.mailer('marketing')
.to('user@example.com')
.subject('Newsletter')
.send();
// Use specific mailer for notifications
await mailManager.mailer('notifications')
.to('admin@example.com')
.subject('System Alert')
.send();
// Use log mailer in development
if (env('APP_ENV') == 'development') {
await mailManager.mailer('log')
.to('dev@localhost')
.subject('Debug Email')
.send();
}
Registering Mail Service Provider
Register the MailServiceProvider in your application bootstrap.
dart
// lib/main.dart
import 'package:khadem/khadem.dart';
void main() async {
final app = await Khadem.init(
environment: 'development',
providers: [
// Core providers
RouteServiceProvider(),
// Mail provider
MailServiceProvider(), // Add this
// Your app providers
AppServiceProvider(),
],
);
await app.start();
}
