Usage | laravel-newsletter-rust | Spatie       

 SPATIE  

  Laravel Newsletter 
=====================

spatie.be/open-source

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

 Version   v1      

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

  Usage    
- [ Introduction ](https://spatie.be/docs/laravel-newsletter-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-newsletter-rust/v1/support-us)
- [ Installation and setup ](https://spatie.be/docs/laravel-newsletter-rust/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/laravel-newsletter-rust/v1/usage)
- [ Custom drivers ](https://spatie.be/docs/laravel-newsletter-rust/v1/custom-drivers)
- [ Testing ](https://spatie.be/docs/laravel-newsletter-rust/v1/testing)
- [ Changelog ](https://spatie.be/docs/laravel-newsletter-rust/v1/changelog)
- [ Releasing ](https://spatie.be/docs/laravel-newsletter-rust/v1/releasing)

 Usage
=====

After installing the package and filling in the configuration, inject `Newsletter` into your handler or service:

```
use laravel::container::Inject;
use serde_json::json;
use spatie_newsletter::Newsletter;

pub async fn store(newsletter: Inject<Newsletter>) -> laravel::Result<()> {
    newsletter.subscribe("rincewind@discworld.com", json!({})).await?;
    Ok(())
}
```

### Subscribing, updating and unsubscribing

The second argument contains subscriber properties. For Mailcoach, pass `first_name` and `last_name`. For Mailchimp, pass merge fields such as `FNAME` and `LNAME`. For MailerLite, pass fields such as `name` and `last_name`. For Brevo, use existing attributes such as `FIRSTNAME`.

```
newsletter.subscribe("rincewind@discworld.com", json!({
    "first_name": "Rince",
    "last_name": "Wind",
})).await?;

newsletter.list("authors")
    .subscribe_or_update("nanny.ogg@discworld.com", json!({}))
    .await?;

newsletter.unsubscribe("the.luggage@discworld.com").await?;
```

`subscribe_or_update` creates a subscriber or updates an existing member. Mailcoach first looks up the email and only creates when that lookup succeeds and finds no member. An authentication, validation, or transport error is not a reason to try a different mutation. Updating an existing Mailcoach subscriber does not resubscribe them; use the underlying SDK's explicit resubscribe operation when that is intended.

You can pass additional API fields with `subscribe_with_options` or `subscribe_or_update_with_options`. For Mailchimp interests:

```
newsletter.list("authors").subscribe_or_update_with_options(
    "rincewind@discworld.com",
    json!({"FNAME": "Rince", "LNAME": "Wind"}),
    json!({"interests": {"interest-id": true}}),
).await?;
```

Use `false` to remove an interest. Mailchimp's `subscribe_pending(email, properties)` sets `status` to `pending` to request confirmation. `subscribe_pending_with_options` accepts additional API fields while keeping that pending status. Other live drivers return an error for these Mailchimp-only operations.

For MailerLite, `newsletter.list("authors").unsubscribe(email)` removes only that group. **Without an explicit list, `newsletter.unsubscribe(email)` marks the subscriber as unsubscribed across the account.** Both operations first require membership in the selected or default group.

For Brevo, unsubscribing removes the contact from only the selected or default list. It does not globally blocklist the contact. Subscribe, update, and unsubscribe refresh the contact afterward; a failed refresh returns an error even when the mutation already succeeded. Check the remote state before retrying.

### Deleting subscribers

Deleting is not the same as unsubscribing. Deleting can lose subscriber history. In most cases, use `unsubscribe` instead.

```
newsletter.delete("rincewind@discworld.com").await?;
```

**Brevo permanently deletes the contact across the account, regardless of the list name.** MailerLite also deletes the subscriber from the account after checking selected-group membership. Mailchimp archives the member through its list-member DELETE endpoint; use its API for permanent deletion if needed. Mailcoach deletes the subscriber identified within the selected list.

### Getting subscriber info

```
let member = newsletter.get_member("lord.vetinari@discworld.com").await?;
let exists = newsletter.has_member("nanny.ogg@discworld.com").await?;
let subscribed = newsletter.is_subscribed("lord.vetinari@discworld.com").await?;
```

Member data is a `serde_json::Value` containing provider fields. `get_member` returns `None` when a member is absent or belongs to a different selected group or list. HTTP 404 means absence for Mailchimp, MailerLite, and Brevo; Mailcoach uses a successful empty filtered page. Other HTTP failures, invalid JSON, and connection failures return `Error`.

`has_member` includes pending and unsubscribed members. `is_subscribed` checks Mailchimp's `subscribed` status, MailerLite's `active` status, or Mailcoach's confirmation and unsubscription timestamps. For Brevo it also checks email blocklisting and list-specific unsubscriptions.

### Need something else?

`newsletter.mailcoach()` returns the underlying Mailcoach SDK. `newsletter.api()` returns the authenticated `PendingRequest` for Mailchimp, MailerLite, or Brevo. Both return `Option` because custom and null drivers may have neither API. Use relative paths without a leading slash when extending an API base.

`newsletter.list("authors").get_members(parameters)` lists Mailchimp members and accepts pagination, filters, and other API query parameters.

Errors are returned as `Result`, not stored in a shared "last error" field. Match `Error::Api { status, body }` for HTTP drivers or `Error::Mailcoach` for SDK errors. Raw bodies may contain personal data; don't log them indiscriminately.

See [custom drivers](custom-drivers) to replace the integration itself.

 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)
