Usage | laravel-mailcoach-mailer-rust | Spatie       

 SPATIE  

laravel-mailcoach-mailer-rust
=============================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-mailcoach-mailer-rust](https://spatie.be/docs/laravel-mailcoach-mailer-rust/v1)  Usage

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-mailcoach-mailer-rust/v1) 

  Usage    
- [ Introduction ](https://spatie.be/docs/laravel-mailcoach-mailer-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-mailcoach-mailer-rust/v1/support-us)
- [ Installation ](https://spatie.be/docs/laravel-mailcoach-mailer-rust/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/laravel-mailcoach-mailer-rust/v1/usage)
- [ Testing ](https://spatie.be/docs/laravel-mailcoach-mailer-rust/v1/testing)
- [ Changelog ](https://spatie.be/docs/laravel-mailcoach-mailer-rust/v1/changelog)

 Usage
=====

###  On this page 

1. [ Build messages ](#content-build-messages)
2. [ Saved transactional mail and metadata ](#content-saved-transactional-mail-and-metadata)
3. [ Notifications and custom mailables ](#content-notifications-and-custom-mailables)
4. [ Attachments ](#content-attachments)
5. [ Queuing and failures ](#content-queuing-and-failures)
6. [ Send a test email ](#content-send-a-test-email)

Build messages
--------------------------------------------------------------------------------------------------

Use `laravel::mail::Message` or implement the framework's `Mailable` trait. Sender defaults come from `config.mail.from`; `Message::from` overrides them. The framework validates addresses and renders views before passing an immutable `Email` to the transport.

```
use laravel::mail::{Attachment, Message};

let message = Message::new()
    .subject("Your link was approved")
    .reply_to(("editor@example.test", "Editor"))
    .html("<p>Your link is live.</p>")
    .text("Your link is live.")
    .attach(Attachment::from_bytes(b"Link #42".to_vec(), "link.txt")
        .content_type("text/plain"));
```

Pass it to `mail::to(recipient).send(message).await?` inside an application context. `mail::mailer("name")` selects a configured named mailer. To, Cc, Bcc and reply-to lists retain their display names. Bcc recipients go into the API's Bcc field, not its To field.

Saved transactional mail and metadata
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Attach one typed `Options` value with `Message::with`. Replacements accept arbitrary JSON values. The framework serializes these options with queued messages.

```
use laravel::mail::Message;
use serde_json::json;
use spatie_mailcoach_mailer::Options;

let options = Options::new("link-approved")
    .replacement("first_name", "Freek")
    .replacement("link", json!({"id": 42, "title": "A Rust article"}))
    .mailer("transactional");
let message = Message::new().with(options);
```

The saved mail supplies its own content when no HTML or text is set. Generic message headers and tags are unsupported and return `DeliveryError::UnsupportedMetadata`. This includes custom tracking headers. `Options::mailer` chooses the Mailcoach-side sending mailer; it does not select the framework's named mailer.

- Use `Options::default()` for metadata without a saved mail name.
- Repeated `.replacement(name, value)` calls overwrite the earlier value for that name.
- Use `.fake(true)` to ask Mailcoach to record a fake send. This still makes an HTTP request; it is not a local fake.
- Use `mail::fake()` or the framework HTTP fake in tests. Neither needs a Mailcoach account.

Notifications and custom mailables
--------------------------------------------------------------------------------------------------------------------------------------------------------------

Return a native `Message` or your own `Mailable` from a notification's `mail` method. Attach `Options` in the same way as an ordinary email. Saved emails need no placeholder subject or body. There is no separate notification-message type to configure.

```
use laravel::mail::{Mailable, Message};
use spatie_mailcoach_mailer::Options;

struct Welcome;
impl Mailable for Welcome {
    fn build(&self) -> laravel::Result<Message> {
        Ok(Message::new().with(Options::new("welcome")))
    }
}
```

Attachments
-----------------------------------------------------------------------------------------

Use native `Attachment` constructors for bytes, paths, storage, or URLs. The framework reads local and storage files before delivery; this transport downloads URL attachments and base64-encodes their bytes. Files retain their names and content types. Use `Message::embed("logo", attachment)` with `cid:logo` in HTML for inline images. The combined attachment limit is 25 MiB, including URL downloads. Failures reading or downloading files propagate before submitting the email.

Queuing and failures
--------------------------------------------------------------------------------------------------------------------

`mail::to(recipient).queue(message).await?` uses the framework's configured queue connection. Register this package in worker applications too. The local tests exercise queue dispatch, encrypted message serialization and delivery on the framework's sync connection. They do not prove database/Redis persistence, worker restart, managed queue support or retries on a production worker.

The application owns its mailables and queue placement. Set `MAIL_QUEUE_CONNECTION` and `MAIL_QUEUE`, or override them on each dispatch with `.queue(message).on_connection("redis").on_queue("transactional").await?`. No package model or job type needs replacing. The native mail fake records your custom mailable and its connection, queue and transport options.

Mailcoach acceptance is HTTP 200 or 204. Other statuses return `DeliveryError::Api` with the full body and a framework HTTP error source. The framework treats 4xx responses other than 408, 409 and 429 as permanent rejection. Other API failures and connection problems remain retryable according to its queue policy. Configuration and unsupported-metadata failures have no HTTP status and use that policy too. Correct those errors before queuing.

There are no transport-level retries or idempotency guarantees. A timeout may mean the server accepted an email but did not answer; retrying can duplicate delivery. A returned `SentMessage` has no provider ID because the endpoint does not require one. Acceptance does not prove inbox delivery.

Send a test email
-----------------------------------------------------------------------------------------------------------

The service provider registers `mailcoach-mailer:send-test` with the native console. Options are:

- `--mailer`: named Mailcoach mailer; defaults to the application's default mailer.
- `--from`: sender address; defaults to `mail.from`.
- `--to`: recipient address; prompts when omitted, using the sender address as the default answer.

The command validates the selected driver's configuration, sends a short HTML and text email through the ordinary mail API, and confirms acceptance only after the send succeeds. Missing mailers, other drivers and delivery failures return a failing exit status.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
