Usage | laravel-mailcoach-mailer-go | Spatie       

 SPATIE  

laravel-mailcoach-mailer-go
===========================

spatie.be/open-source

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

 Version   v1      

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

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

 Usage
=====

###  On this page 

1. [ Send regular mail ](#content-send-regular-mail)
2. [ Use a Mailcoach template ](#content-use-a-mailcoach-template)
3. [ Notifications ](#content-notifications)
4. [ Attachments ](#content-attachments)
5. [ Queuing and failures ](#content-queuing-and-failures)
6. [ Send a test email ](#content-send-a-test-email)

Send regular mail
-----------------------------------------------------------------------------------------------------------

Use a native `mail.Message` or implement `Build() mail.Message` on your own mailable. Sender defaults come from `MAIL_FROM_ADDRESS` and `MAIL_FROM_NAME`. Laravel renders views and validates addresses before sending.

```
err := mail.To(mail.Addr("john@example.com")).Send(ctx, mail.Message{
    Subject: "Your order shipped",
    HTML: "<p>Your order is on its way.</p>",
    Text: "Your order is on its way.",
    ReplyTo: []mail.Address{mail.Addr("support@example.com", "Support")},
    Attachments: []mail.Attachment{
        mail.AttachData(invoicePDF, "invoice.pdf").WithMime("application/pdf"),
    },
})
```

In a handler, pass `r`, the `*web.Request`, as the context. In a job, listener or command, pass its `ctx`. Use `mail.Use("mailcoach")` to select the named Laravel mailer explicitly. Display names are preserved. Cc and Bcc remain in their own fields and are not copied into To.

Use a Mailcoach template
--------------------------------------------------------------------------------------------------------------------------------

Create a transactional mail in Mailcoach, then name it in the mailable's options:

```
func (OrderShipped) Build() mail.Message {
    return mail.Message{
        Options: []mail.TransportOption{mailcoachmailer.Options{
            MailName: "order-shipped",
            Replacements: map[string]any{
                "first_name": "Freek",
                "order": map[string]any{"number": 42},
            },
            Mailer: "transactional",
        }},
    }
}
```

Attach one `Options` value per message. `MailName` replaces PHP's `mailcoachMail` or `usingMail` call. `Replacements` replaces `replacing` and accepts JSON-serializable values, including arrays and objects. Map keys are literal placeholder names, not nested paths. Go preserves a `nil` value as JSON null, unlike PHP's replacement header, which turns null into an empty string.

`Mailer` chooses Mailcoach's sending mailer, not Laravel's named mailer. Leave it empty to use Mailcoach's default. Set `Fake: true` to ask Mailcoach to record a fake send. This still contacts Mailcoach. False omits the fake flag and uses normal delivery.

The saved mail supplies the content and subject when those fields are empty. Go needs no placeholder body or subject. Set an explicit subject or body if you want to override it. You may also use options without a saved template name.

The endpoint has no fields for arbitrary headers or tags. This port rejects nonempty `Headers` and `Tags` rather than silently discarding them. Use typed options instead of PHP's special `X-Mailcoach-*` header classes.

Notifications
-----------------------------------------------------------------------------------------------

Return a native mailable or `mail.Message` from your notification's `Messages` method. Attach the same options. There is no separate `MailcoachMessage` type, and Laravel Go does not infer a subject from the notification's class name.

```
func (Welcome) Messages(user *User) (notification.Messages, error) {
    return notification.Messages{
        Mail: mail.Message{Options: []mail.TransportOption{
            mailcoachmailer.Options{MailName: "welcome"},
        }},
    }, nil
}
```

Your recipient's `MailAddress() mail.Address` method supplies the address. Send with `notification.Send(ctx, Welcome{}, user)` or queue it using Laravel's notification API.

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

Use Laravel's native attachment constructors for memory, files, storage, embedded files or URLs. The transport reads attachments before submitting the mail, preserves names and MIME types, and base64-encodes the content. The combined byte limit is 25 MiB. Read failures stop delivery.

For inline images, use `mail.Message.Inline` with a content ID as the key and `cid:your-key` in the HTML. The API receives `content_id: "cid:your-key"`. URL attachments must use HTTPS, even when your Mailcoach API base URL uses internal HTTP. Laravel's attachment reader downloads them with the request context. That reader uses Go's standard HTTP client, not the framework HTTP fake. Use a trusted local TLS test server for URL attachment tests.

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

```
err := mail.To(mail.Addr("john@example.com")).
    OnConnection("redis").OnQueue("transactional").
    Queue(ctx, OrderShipped{})
```

Laravel owns queue placement, encrypted serialization, retries and worker execution. Mailcoach options survive JSON serialization. Register this package in worker applications too. Application-defined mailables and jobs remain your own types. No package job needs replacing.

Only HTTP 200 and 204 mean Mailcoach accepted the request. Other statuses return `*mail.RejectedError`, inspectable with `errors.As`. Its `Status` retains 403 for authorization failures and 422 for invalid mail, while `Permanent()` follows Laravel's queue policy. Other 4xx responses are permanent except 408, 409 and 429. Server and connection errors remain retryable. Error bodies may contain personal data.

The transport does not retry or promise idempotency. A timeout may occur after acceptance, so retrying can send twice. Acceptance does not prove inbox delivery.

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

```
laravel mailcoach-mailer:send-test --to=john@example.com
```

The command accepts `--from`, `--to` and `--mailer`. The sender and mailer default to the application's mail settings. If `--to` is absent, it prompts with the application's default sender address. The selected mailer must match the name registered by the package.

The command sends a short HTML and text email through the regular mail API. Invalid addresses, a different mailer or a delivery failure return a failing exit status. It confirms acceptance only after sending succeeds.

 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)
