Usage | laravel-newsletter-go | Spatie       

 SPATIE  

  Laravel Newsletter Go 
========================

spatie.be/open-source

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

 Version   v1      

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

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

 Usage
=====

###  On this page 

1. [ Subscribing, updating and unsubscribing ](#content-subscribing-updating-and-unsubscribing)
2. [ Deleting subscribers ](#content-deleting-subscribers)
3. [ Getting subscriber info ](#content-getting-subscriber-info)
4. [ Need something else? ](#content-need-something-else)
5. [ Handling errors ](#content-handling-errors)

After you've installed the package and filled in the configuration, working with this package will be a breeze. Import the package and retrieve its application-scoped service with `newsletter.From(ctx)`. In a handler, pass `r`, the `*web.Request`. In a job, listener or command, pass the supplied context.

Subscribing, updating and unsubscribing
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Subscribing an email address can be done with one call:

```
member, err := newsletter.Subscribe(ctx, "rincewind@discworld.com")
```

Let's unsubscribe someone:

```
member, err := newsletter.From(ctx).Unsubscribe(ctx, "the.luggage@discworld.com", "")
```

For Mailcoach, you can pass extra attributes:

```
member, err := newsletter.Subscribe(ctx, "rincewind@discworld.com", newsletter.Subscription{
    Properties: map[string]any{"first_name": "Rince", "last_name": "Wind"},
})
```

For MailChimp you can pass merge fields such as `FNAME` and `LNAME`. For MailerLite you can pass subscriber fields such as `name` and `last_name`. Brevo uses uppercase attribute names such as `FIRSTNAME`.

You can subscribe someone to a specific list by passing its configured name:

```
member, err := newsletter.Subscribe(ctx, "rincewind@discworld.com", newsletter.Subscription{
    List: "subscribers",
})
```

You can also subscribe and/or update someone. The person will be subscribed or updated if they are already subscribed:

```
member, err := newsletter.From(ctx).SubscribeOrUpdate(ctx, "rincewind@discworld.com", newsletter.Subscription{
    Properties: map[string]any{"first_name": "Rince", "last_name": "Wind"},
})
```

For MailChimp, you can subscribe someone to specific groups or interests using the options map:

```
member, err := newsletter.From(ctx).SubscribeOrUpdate(ctx, "rincewind@discworld.com", newsletter.Subscription{
    Properties: map[string]any{"FNAME": "Rince", "LNAME": "Wind"},
    List: "subscribers",
    Options: map[string]any{
        "interests": map[string]bool{"first-interest-id": true, "second-interest-id": false},
    },
})
```

Use `false` to remove someone from an interest. Options can also supply MailChimp's `marketing_permissions`. The list's `Properties` map remains available to custom drivers and does not automatically become an API payload.

Here's how to unsubscribe someone from a specific list:

```
member, err := newsletter.From(ctx).Unsubscribe(ctx, "rincewind@discworld.com", "subscribers")
```

For MailerLite, passing a list name removes the subscriber from the matching group. Without a list name, the subscriber is marked as unsubscribed across the account. After group removal, the returned member is looked up in the default group, matching PHP. It may be nil if that default group was removed. Brevo removes only the selected or default list membership.

Deleting subscribers
--------------------------------------------------------------------------------------------------------------------

Deleting is not the same as unsubscribing. Unlike unsubscribing, deleting a member will result in the loss of all history as well as removing them from the list. In most cases, you want to use `Unsubscribe` instead of `Delete`.

```
err := newsletter.From(ctx).Delete(ctx, "rincewind@discworld.com", "")
```

Brevo deletes the contact across the entire account, regardless of the list name. MailerLite also deletes the account-wide subscriber after finding them in the selected group. MailChimp archives the member through its DELETE endpoint. Mailcoach deletes the subscriber found in the selected email list.

Getting subscriber info
-----------------------------------------------------------------------------------------------------------------------------

You can get information on a subscriber using `GetMember`:

```
member, err := newsletter.From(ctx).GetMember(ctx, "lord.vetinari@discworld.com", "")
if err != nil {
    return web.Fail(err)
}
if member == nil {
    return web.Abort(404)
}
return web.JSON(member.Data)
```

Every driver returns `*newsletter.Member` with the complete provider response in `Data`. `Subscribed` contains the provider's subscription state. Mailcoach checks `subscribed_at`, MailChimp checks `status == subscribed`, MailerLite checks `status == active`, and Brevo checks list membership, blocklisting and list unsubscriptions.

A missing member returns `nil, nil`. A Mailcoach list lookup returns an empty member only when its filtered subscriber array is empty. An invalid or inaccessible list still returns the API error.

There's also a convenient method to check if someone is already a member, including an unsubscribed member:

```
found, err := newsletter.From(ctx).HasMember(ctx, "nanny.ogg@discworld.com", "")
```

You can also check if a user is subscribed to your list:

```
subscribed, err := newsletter.From(ctx).IsSubscribed(ctx, "lord.vetinari@discworld.com", "")
```

Need something else?
------------------------------------------------------------------------------------------------------------------

If you need more functionality, get the authenticated Laravel HTTP client:

```
api := newsletter.From(ctx).GetAPI()
response, err := api.Get(ctx, "campaigns")
```

It uses the selected provider's base URL and credentials. Laravel's application HTTP settings and fakes still apply. A custom driver may expose a client through `GetAPI() *httpclient.Client`. A driver without this method returns nil.

MailChimp has two extra methods:

```
driver := newsletter.From(ctx).Driver.(*newsletter.MailchimpDriver)
member, err := driver.SubscribePending(ctx, "rincewind@discworld.com", newsletter.Subscription{})
page, err := driver.GetMembers(ctx, "subscribers", url.Values{"count": {"50"}, "offset": {"0"}})
```

Only use that type assertion when your application is configured for MailChimp. `GetMembers` returns the full paginated response. `SubscribePending` forces pending status, even if options specify another status.

Handling errors
-----------------------------------------------------------------------------------------------------

Operations return errors directly rather than storing PHP's mutable `getLastError` or `lastActionSucceeded` state. This keeps concurrent requests independent. Handle errors before using the returned member.

```
var status *httpclient.StatusError
if errors.As(err, &status) {
    slog.ErrorContext(ctx, "Newsletter request failed", "status", status.Response.Status)
}
```

Cancellation is preserved for `errors.Is(err, context.Canceled)`. Mutations are not automatically retried. A failed follow-up read does not undo a successful mutation.

 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)
