-
app/Mail/NewIPAddressWarningMail.php
Open in GitHubuse Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; class NewIPAddressWarningMail extends Mailable { use Queueable, SerializesModels; public string $host; public string $ipAddress; public string $time; public function __construct(string $ipAddress) { $this->ipAddress = $ipAddress; } public function build(): self { $this->time = now()->formatLocalized((string)trans('config.date_time')); $this->host = ''; $hostName = gethostbyaddr($this->ipAddress); if ($hostName !== $this->ipAddress) { $this->host = $hostName; } return $this->view('emails.new-ip-html')->text('emails.new-ip-text') ->subject((string)trans('email.login_from_new_ip')); } }
-
app/Handlers/Events/UserEventHandler.php
Open in GitHubuse FireflyIII\Events\DetectedNewIPAddress; use FireflyIII\Mail\NewIPAddressWarningMail; use Log; use Mail; class UserEventHandler { // public function notifyNewIPAddress(DetectedNewIPAddress $event): void { $user = $event->user; $email = $user->email; $ipAddress = $event->ipAddress; if ($user->hasRole('demo')) { return; // do not email demo user. } $list = app('preferences')->getForUser($user, 'login_ip_history', [])->data; $pref = app('preferences')->getForUser($user, 'remote_guard_alt_email'); if (null !== $pref) { $email = $pref->data; } foreach ($list as $index => $entry) { if (false === $entry['notified']) { try { Mail::to($email)->send(new NewIPAddressWarningMail($ipAddress)); } catch (Exception $e) { // @phpstan-ignore-line Log::error($e->getMessage()); } } $list[$index]['notified'] = true; } app('preferences')->setForUser($user, 'login_ip_history', $list); } // }