Email provider configuration for Better Auth email-backed flows.
Better Auth uses email for verification and OTP-based two-factor authentication.
Some routes also use explicit plugin callbacks for delivery. You configure an
email provider to handle the flows that call EmailProvider directly.
Email Provider Trait
Better Auth defines an EmailProvider trait that you implement to integrate with your email service:
#[async_trait]
pub trait EmailProvider: Send + Sync {
async fn send_email(
&self,
to: &str,
subject: &str,
body: &str,
) -> Result<(), Box<dyn std::error::Error>>;
}Setup
Using a Custom Provider
use better_auth::email::EmailProvider;
struct SmtpEmailProvider {
// your SMTP client configuration
}
#[async_trait::async_trait]
impl EmailProvider for SmtpEmailProvider {
async fn send_email(
&self,
to: &str,
subject: &str,
body: &str,
) -> Result<(), Box<dyn std::error::Error>> {
// Send email using your SMTP library
Ok(())
}
}
let auth = BetterAuth::new(config)
.database(database)
.email_provider(SmtpEmailProvider { /* ... */ })
.build()
.await?;Console Provider (Development)
For development, you can use a simple provider that logs emails to the console:
struct ConsoleEmailProvider;
#[async_trait::async_trait]
impl EmailProvider for ConsoleEmailProvider {
async fn send_email(
&self,
to: &str,
subject: &str,
body: &str,
) -> Result<(), Box<dyn std::error::Error>> {
println!("=== Email to: {} ===", to);
println!("Subject: {}", subject);
println!("{}", body);
println!("==================");
Ok(())
}
}When Emails Are Sent
Better Auth sends emails in the following scenarios:
Email Verification
When require_email_verification is enabled on the EmailPasswordPlugin, a verification email is sent after sign-up:
- Subject: Email verification
- Content: Contains a verification URL with a token
- Token expiry: Configurable, typically 24 hours
See Email Verification for details.
Password Reset
POST /request-password-reset does not use EmailProvider directly. To enable
that route, configure PasswordManagementPlugin::send_reset_password(...).
See Password Management for the route-specific sender setup.
Two-Factor OTP
When using the OTP method of the TwoFactorPlugin:
- Subject: Your one-time password
- Content: Contains a 6-digit OTP code
- Code expiry: 5 minutes
See Two-Factor Authentication for details.
Email Content
Email content is generated internally by Better Auth. The default templates produce simple text-based emails. The content varies by email type:
| Email Type | Key Content |
|---|---|
| Verification | Verification URL with token |
| Password Reset | Reset URL with token |
| OTP | 6-digit numeric code |
Configuration
Email-related settings are spread across the plugins that use email:
Email Verification Config
EmailVerificationPlugin::new()
.send_on_signup(true) // Auto-send verification email on sign-up
.auto_signin_after_verify(true) // Create session after email verificationPassword Reset Config
PasswordManagementPlugin::new()
// Password reset is enabled when the plugin is registered2FA OTP Config
TwoFactorPlugin::new()
// OTP emails are sent via the configured email providerError Handling
If no email provider is configured, any operation that requires sending an email will return an error:
{
"code": "EMAIL_PROVIDER_NOT_CONFIGURED",
"message": "Email provider is not configured"
}Make sure to configure an email provider if you use any email-dependent features.
See Also
- Email Verification — Verification flow details
- Password Management — Reset flow details
- Two-Factor Authentication — OTP via email