Atom's mail API separates composing a message from delivering it. A Mailable describes the recipient, subject, and bodies. An injected MailerInterface sends it through the application's configured transport. An application can use that flow to welcome a new member after signup succeeds.
Mailables
Create app/Mail/WelcomeMail.php. Pass the member's email address and display name to the constructor. The plain-text body gives mail clients an alternative to the HTML version.
<?php
namespace App\Mail;
use Atom\Mail\Mailable;
use Atom\Mail\MailMessage;
final class WelcomeMail extends Mailable
{
public function __construct(
private readonly string $email,
private readonly string $name,
) {
}
public function build(): MailMessage
{
return MailMessage::create()
->to($this->email, $this->name)
->subject("Welcome to our application")
->text("Hello {$this->name}, your membership is ready.")
->htmlTemplate(__DIR__ . "/welcome.php", ["name" => $this->name]);
}
}
The name key passed to htmlTemplate() becomes $name in the PHP template. Escape the display name where it enters the markup. Use html() when you already have the HTML string.
<p>
Hello <strong><?= htmlspecialchars($name, ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8") ?></strong>,
your membership is ready.
</p>
Send from the application
Inject MailerInterface into the service responsible for the welcome email. Call send() after signup succeeds and the member record has been saved.
<?php
namespace App\Mail;
use Atom\Mail\MailerInterface;
final readonly class WelcomeNotifier
{
public function __construct(private MailerInterface $mailer)
{
}
public function send(string $email, string $name): void
{
$this->mailer->send(new WelcomeMail($email, $name));
}
}
Configure the mail transport
Atom's built-in mail services bind MailerInterface to an adapter for Symfony Mailer. When the injector resolves the interface, Atom reads its mail options and gives MAIL_DSN to Symfony to construct the transport. The DSN tells Symfony how to deliver the message, for example by connecting to an SMTP server. Routes and services can keep the same constructor when the transport changes.
The default null://null transport discards messages. To deliver mail, set the DSN and default sender in the application's environment using values from your mail provider.
MAIL_DSN=smtp://username:[email protected]:587
[email protected]
MAIL_FROM_NAME="Example App"
How Atom sends mail
- The mailer calls
WelcomeMail::build()to get the recipient, subject, plain-text body, and HTML template. - Atom renders the HTML template with the supplied variables and prepares the email for Symfony Mailer. It uses the configured sender unless the message sets one with
from(). If you add a Reply-To address or attachments, Atom includes those settings in the same email. - Symfony hands the message to the configured transport. If composing or sending fails during
send(), Atom throws aMailExceptionthat wraps the underlying error.
Preview the welcome email
Use ArrayMailer in a test to inspect what the notifier sends. It captures the built message in memory. Use MailTemplateRenderer to render a stored template view when you want to check its output.
use App\Mail\WelcomeNotifier;
use Atom\Mail\ArrayMailer;
use Atom\Mail\MailTemplateRenderer;
$mailer = new ArrayMailer();
(new WelcomeNotifier($mailer))->send("[email protected]", "Alex & Sam");
$message = $mailer->messages()[0];
$html = (new MailTemplateRenderer())->render($message->htmlView());
The message contains the recipient, subject, and plain-text body. The rendered HTML contains Alex & Sam, which displays as Alex & Sam in an email client.
Set a Reply-To address
Use replyTo() when replies should go to a different address from the sender. Add this call to the MailMessage chain and use a monitored mailbox.
->replyTo("[email protected]", "Support")
Add an attachment
To include a file with the welcome email, add attach() to the same chain. This example uses a PDF stored beside WelcomeMail.php.
->attach(__DIR__ . "/welcome-guide.pdf", "getting-started.pdf", "application/pdf")
The arguments specify the server file, the filename the recipient sees, and its MIME type. The file must be readable when the mailer prepares the message.