expand_less

Mail

Introduction

DreamFork provides a clean and simple email API powered by the popular Symfony Mailer component. Emails are handled via "mailable" classes, which allow you to keep your email logic organized and separated from your controllers.

Configuration

The email configuration is stored in your config/mail.php file. In this file, you may configure multiple mailers and their respective transports (like SMTP).

                                
                                    
MAIL_MAILER=smtp
MAIL_HOST=sandbox.smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"

Writing Mailables

All mailable classes should extend the Framework\Mail\Mailable class. Within the build method, you can define the subject and the view that should be rendered.

                                
                                    
namespace App\Mail;
 
use Framework\Mail\Mailable;
 
class WelcomeMail extends Mailable
{
public function build()
{
return $this->subject('Welcome to DreamFork')
->view('emails.welcome');
}
}

Mail Templates

Vision Engine Support

DreamFork's mailer seamlessly integrates with the Vision template engine. This allows you to use the familiar curly brace syntax {{ }} to display data, protecting your application against XSS attacks automatically.

You can also use Vision directives like @resource to inject CSS styles, which is particularly useful for email styling where inline styles are often preferred.

                                
                                    
<!DOCTYPE html>
<html>
<head>
<style>
/* Load external CSS for the email */
@resource(css/email.css);
</style>
</head>
<body>
<h1>Hello, {{ $name }}!</h1>
<p>Welcome to the application.</p>
</body>
</html>

Passing Data to Views

Typically, you will want to pass data to your view directly when creating the Mailable. You can accept this data in the mailable's constructor and pass it to the view method.

                                
                                    
public function __construct(public string $userName) {}
 
public function build()
{
return $this->view('emails.welcome', [
'name' => $this->userName
]);
}

Sending Mail

To send an email, use the to method on the Mail facade. The to method accepts an email address, and the send method accepts an instance of your mailable class:

                                
                                    
use Framework\Support\Facades\Mail;
use App\Mail\WelcomeMail;
 
Mail::to('user@example.com')->send(new WelcomeMail('John Doe'));