Notifying users | laravel-suspicious-login-go | Spatie       

 SPATIE  

laravel-suspicious-login-go
===========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-suspicious-login-go](https://spatie.be/docs/laravel-suspicious-login-go/v1)  Responding-to-suspicious-logins  Notifying users

 Version   v1      

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

  Notifying users    
- [ Introduction ](https://spatie.be/docs/laravel-suspicious-login-go/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-suspicious-login-go/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-suspicious-login-go/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-suspicious-login-go/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-suspicious-login-go/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-suspicious-login-go/v1/changelog)
- [ Testing ](https://spatie.be/docs/laravel-suspicious-login-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-suspicious-login-go/v1/releasing)
- [ About us ](https://spatie.be/docs/laravel-suspicious-login-go/v1/about-us)

Basic usage
-----------

- [ Getting started ](https://spatie.be/docs/laravel-suspicious-login-go/v1/basic-usage/getting-started)
- [ How a login is scored ](https://spatie.be/docs/laravel-suspicious-login-go/v1/basic-usage/how-a-login-is-scored)
- [ Determine the preset ](https://spatie.be/docs/laravel-suspicious-login-go/v1/basic-usage/choosing-a-preset)
- [ Customizing presets ](https://spatie.be/docs/laravel-suspicious-login-go/v1/basic-usage/customizing-presets)
- [ Working with login attempts ](https://spatie.be/docs/laravel-suspicious-login-go/v1/basic-usage/working-with-login-attempts)
- [ Adding custom properties ](https://spatie.be/docs/laravel-suspicious-login-go/v1/basic-usage/adding-custom-properties)
- [ Recording logins manually ](https://spatie.be/docs/laravel-suspicious-login-go/v1/basic-usage/recording-logins-manually)

Available checks
----------------

- [ Overview ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/overview)
- [ New device ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/new-device)
- [ New IP address ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/new-ip)
- [ New country ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/new-country)
- [ Impossible travel ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/impossible-travel)
- [ Allowed and blocked countries ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/country)
- [ Failed attempts ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/failed-attempts)
- [ Credential stuffing ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/credential-stuffing)
- [ Unusual time ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/unusual-time)
- [ Suspicious user agent ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/suspicious-user-agent)
- [ IP reputation ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/ip-reputation)
- [ Multiple countries ](https://spatie.be/docs/laravel-suspicious-login-go/v1/available-checks/multiple-countries)

Responding to suspicious logins
-------------------------------

- [ Notifying users ](https://spatie.be/docs/laravel-suspicious-login-go/v1/responding-to-suspicious-logins/notifying-users)
- [ Blocking logins ](https://spatie.be/docs/laravel-suspicious-login-go/v1/responding-to-suspicious-logins/blocking-logins)
- [ Writing your own actions ](https://spatie.be/docs/laravel-suspicious-login-go/v1/responding-to-suspicious-logins/writing-your-own-actions)
- [ Using events ](https://spatie.be/docs/laravel-suspicious-login-go/v1/responding-to-suspicious-logins/using-events)

Advanced usage
--------------

- [ Creating custom checks ](https://spatie.be/docs/laravel-suspicious-login-go/v1/advanced-usage/creating-custom-checks)
- [ Geolocation drivers ](https://spatie.be/docs/laravel-suspicious-login-go/v1/advanced-usage/geolocation-drivers)
- [ IP reputation drivers ](https://spatie.be/docs/laravel-suspicious-login-go/v1/advanced-usage/ip-reputation-drivers)
- [ Device fingerprints ](https://spatie.be/docs/laravel-suspicious-login-go/v1/advanced-usage/device-fingerprints)
- [ Using your own model ](https://spatie.be/docs/laravel-suspicious-login-go/v1/advanced-usage/using-your-own-model)

 Notifying users
===============

###  On this page 

1. [ Customizing the email view ](#content-customizing-the-email-view)
2. [ Customizing the subject ](#content-customizing-the-subject)
3. [ Using your own mailable ](#content-using-your-own-mailable)
4. [ Using Laravel notifications instead ](#content-using-laravel-notifications-instead)

`NotifyUserAction` emails the user about a suspicious login. `DefaultPreset` notifies at medium and high risk. To notify at both levels in your own preset:

```
p.OnMedium = []suspiciouslogin.Action{suspiciouslogin.NotifyUserAction{}}
p.OnHigh = []suspiciouslogin.Action{suspiciouslogin.NotifyUserAction{}}
```

The email lists the time, IP, location and device, together with reasons. Set `Config.Email` to resolve the current account email from its `User` reference. Return empty for deleted users and missing addresses, as shown in installation. Nil `Email` skips delivery entirely.

Warning mail uses Laravel's native queue. Configure mail, an asynchronous queue connection and a worker. With `sync`, delivery happens immediately. The default `log` mailer does not send to a recipient. Actions still run in order, and sending mail does not mean the login was blocked.

Customizing the email view
--------------------------------------------------------------------------------------------------------------------------------------

The Go default is plain text, not a published Blade view. Replace `Config.Mailable` with a native mailable factory to use an application-owned HTML or text view. The factory receives the whole attempt.

Customizing the subject
-----------------------------------------------------------------------------------------------------------------------------

Keep the default body while changing its subject:

```
type LoginAlert struct{ Attempt suspiciouslogin.LoginAttempt }

func (m LoginAlert) Build() mail.Message {
    message := (suspiciouslogin.SuspiciousLoginMail{Attempt: m.Attempt, AppName: "My app"}).Build()
    message.Subject = "Was this you?"
    return message
}
```

Register it:

```
cfg.Mailable = func(ctx context.Context, a suspiciouslogin.LoginAttempt) mail.Mailable {
    return LoginAlert{Attempt: a}
}
```

Using your own mailable
-----------------------------------------------------------------------------------------------------------------------------

Implement `Build() mail.Message` and return your own subject and `Text`, `HTML` or `View` with `Data`. Import `go.laravel.com/mail`. Let the view escape user agents and custom reasons. The default uses plain text to avoid HTML injection.

Set `Mailer`, `QueueConnection` and `Queue` in package config to choose delivery destinations. Laravel serializes the built email for its worker, so there is no package-specific job class to override or register. The factory need not be serialized. To send synchronously instead, replace this action with an `ActionFunc` that calls native `mail.To(...).Send`.

Using Laravel notifications instead
-----------------------------------------------------------------------------------------------------------------------------------------------------------------

Write a custom action that resolves your user and dispatches the application's notification. Choose channels, routing and queue behavior there. `Config.Mailable` only affects `NotifyUserAction`, not your own notification action.

 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)
