The default mailer is log. A new app writes the sender, recipients, subject, and plain-text body to the log and does not contact a provider. Keep that until the share path works. Resend is the guide’s short path to a real inbox: set MAIL_MAILER=resend and RESEND_API_KEY. There is no bundled SMTP transport. A custom transport is a separate section of the mail guide, and this series does not write one.
The mailable
Derive Mailable in src/mail/note_shared.rs, and declare pub mod mail; plus the file in src/mail/mod.rs. The view name is the struct in kebab case: resources/views/mail/note-shared.html.
use laravel::mail::Mailable;
use laravel::prelude::*;
use crate::models::Note;
#[derive(Mailable)]
#[mail(subject = "A Field Note was shared")]
pub struct NoteShared {
pub note: Note,
pub url: String,
}
impl NoteShared {
pub fn new(note: Note) -> laravel::Result<Self> {
let url = route!(notes::share, ¬e)?;
Ok(Self { note, url })
}
}
The view reads each field by name, and each field must implement Serialize. Note already does. Point the template at {{ note.title }} and {{ url }}. It extends the framework layout the mail guide uses for its own example:
{% extends "mail.base" %}
{% import "laravel::mail.components" as mail %}
{% block content %}
<h1>{{ note.title }}</h1>
{{ mail.button("Read the note", url) }}
{% endblock %}
#[derive(Mailable)] also builds the plain-text part from that HTML. Returning the mailable from a route implements IntoResponse, which is the guide’s preview. Put that route behind something you will delete, not on the public share URL:
pub async fn preview(note: Note) -> laravel::Result<NoteShared> {
NoteShared::new(note)
}
Queue it
queue renders and validates the message immediately, then pushes it. The HTTP response does not wait on the provider. Workers retry transient failures. send waits for the provider. Use send inside a command or a job that already has retries. Do not use it in the request that just saved a note.
use laravel::mail;
mail::to(&user).queue(NoteShared::new(note)?).await?;
mail::to(&user) is the call the mail guide uses with a user model. Load the user before this line. The welcome workflow from the previous lesson can call the same function once NoteShared exists.
One notification, one channel
A notification describes an event and builds a message per channel. Field Notes only implements mail. SMS and Slack stay in their guides.
User needs ReceivesMail. A channel method that returns None skips that channel without an error.
use laravel::mail::Address;
use laravel::notification::ReceivesMail;
impl ReceivesMail for User {
fn mail_address(&self) -> Option<Address> {
Some(Address::new(&self.email).with_name(&self.name))
}
}
use laravel::mail::Mailable;
use laravel::notification;
use crate::mail::NoteShared;
use crate::models::{Note, User};
pub struct NoteSharedNotice {
pub note: Note,
pub url: String,
}
#[notification]
impl NoteSharedNotice {
fn mail(&self, _user: &User) -> impl Mailable {
NoteShared {
note: self.note.clone(),
url: self.url.clone(),
}
}
}
fn mail returns a mailable, not a Result. Build the URL in the handler with route!, where ? is allowed, and store the string on the notice. #[model] derives Clone, which is why the note can be copied into the mailable.
Prefer mail::to(&user).queue(...) for the share email itself. The notification is the shape to keep when one event later grows a second channel. Queue it so the handler still does not wait:
use laravel::notification;
let url = route!(notes::share, ¬e)?;
notification::to(&user)
.queue(NoteSharedNotice { note: note.clone(), url })
.await?;
send on a notification waits for each provider. The notifications guide tells HTTP handlers to queue. Declare the module. The structure guide’s usual file is src/notifications.rs, with pub mod notifications; in src/lib.rs.
Check your work
With MAIL_MAILER=log, share a note and find the subject in the laravel dev log without the browser waiting on a network call.