Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

firefly-iii/firefly-iii

21505 stars
2 code files
View firefly-iii/firefly-iii on GitHub

app/Mail/NewIPAddressWarningMail.php

Open in GitHub
use 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 GitHub
use 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);
}
//
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.