Mailer Generator

Generate email functionality using the Carbon email library with support for HTML/text templates and async delivery via background jobs.

Synopsis

azu generate mailer <name> [methods...] [options]

Description

The mailer generator creates email functionality for your Azu application using the Carbon email library. It generates mailer classes with methods for sending emails, template files for email content, and optional background job integration for async email delivery.

Features

  • 📧 HTML & Text Emails: Dual-format support for all email clients

  • 🎨 Template System: Jinja/ECR templates for email content

  • 🚀 Async Delivery: Background job integration via JoobQ

  • 🔧 Multiple Adapters: SMTP, SendGrid, development, and custom adapters

  • 📎 Attachments: File attachment support

  • 🔐 Secure: Built-in security best practices

  • 🧪 Testable: Easy to test email functionality

Usage

Basic Usage

Generate a mailer with default welcome email:

This creates:

  • src/mailers/user_mailer.cr - Mailer class

  • src/mailers/templates/user/welcome.text.ecr - Plain text template

  • src/mailers/templates/user/welcome.html.ecr - HTML template

Custom Email Methods

Generate mailer with specific email methods:

Common Mailer Types

User Notifications

Order/Transaction Emails

Newsletter/Marketing

System Notifications

Arguments

Argument
Type
Description
Required

<name>

string

Mailer name (PascalCase)

Yes

[methods...]

strings

Email method names

No (defaults to welcome)

Options

Option
Description
Default

--async

Enable async delivery

true

--no-async

Disable async delivery

--force

Overwrite existing files

false

Generated Files

Directory Structure

Mailer Class

Base Mailer

Email Templates

HTML Template (welcome.html.ecr)

Text Template (welcome.text.ecr)

Async Job (with --async)

Email Adapter Configuration

Development Adapter

Prints emails to console (no external services required):

SMTP Adapter

For production use with any SMTP server:

SendGrid Adapter

For SendGrid email service:

Custom Adapter

Create your own adapter:

Usage Examples

Send Welcome Email

Send Password Reset

Send Order Confirmation

Send with Attachments

Advanced Features

Multiple Recipients

CC and BCC

Custom Headers

Reply-To

Email Priorities

Template Helpers

Common Helpers

Add to BaseMailer:

Testing

Unit Tests

Integration Tests

Best Practices

1. Use Plain Text + HTML

Always provide both formats:

2. Personalize Emails

Use recipient's name:

3. Clear Call-to-Action

Make primary action obvious:

4. Responsive Design

Use mobile-friendly HTML:

Always include for marketing emails:

6. Test in Multiple Clients

Test emails in:

  • Gmail

  • Outlook

  • Apple Mail

  • Mobile devices

7. Monitor Delivery

Track:

  • Delivery rate

  • Open rate

  • Click rate

  • Bounce rate

Environment Configuration

Add to .env:

Dependencies

Add to shard.yml:

Troubleshooting

Emails Not Sending

Check adapter configuration:

Check environment variables:

Enable debug logging:

Templates Not Found

Verify template paths:

Async Delivery Not Working

Ensure JoobQ workers are running:

Check job queue:

See Also

Last updated