Send email after login with one-time-login plugin

Hello everyone, I tried to reply in my original post, but I cant.
I try to implement a way to send a email after somebody use the one time login plugin, I modified the plugin to keep the token, so I can use more than 1 time, and also remove the option to remove it after 15 minutes, and works fine, but now I try to use the email plugin to send a email when its used, Same like my previous topic I using the email plugin and in login plugin is working fine, its was a grammatical mistake, but now when I try to use in the same way in the one time login plugin, Send me this error:

This is the code I used in the one-time-login.php:

private function authenticateOtl()
{
$username = $this->grav['uri']->param('user');
$firstName = $this->grav['uri']->param('firstname');
$lastName = $this->grav['uri']->param('lastname');
$otl_nonce = $this->grav['uri']->param('otl_nonce');
$rolUsername = $this->grav['uri']->param('username');
$sitename = $this->grav['uri']->param('sitename');

$this->redirect = "/";

// Load user object.
$user = !empty($username) ? User::load($username) : null;
if (empty($user) || !$user->otl_nonce) {
    $this->grav['log']->info("Invalid OTL URL.");
    $this->grav['messages']->add('Invalid OTL URL.', 'error');
}
if ($user) {
    $otl_nonce_expire = $user->otl_nonce_expire;

    if (($user->otl_nonce == $otl_nonce) /* && (time() < $otl_nonce_expire) */) {
        // Remove OTL user entries.
        /* unset($user->otl_nonce);
        unset($user->otl_nonce_expire);
        unset($user->otl_admin_logon); */
        $user->save();
        
        $user->authenticated = true;
        $user->authorized = $user->authorize('admin.login');
                
        // Authenticate user to website.
        $this->grav['session']->user = $user;
        unset($this->grav['user']);
        $this->grav['user'] = $user; 
        
        date_default_timezone_set("America/Costa_Rica");
        $time = date("l / d-m-Y(H:i:s)");
        
                $to = 'email@email.com';
                $from = 'email@email.com';
        
                $subject = $time . " | Inicio de sesión";
                $content = '
                <html> 
                <head>
                <style> 
                table {
                width:100%;
                }
                table, th, td {
                border: 1px solid black;
                border-collapse: collapse;
                }
                th, td {
                padding: 15px;
                text-align: left;
                } 
                table#t01 tr:nth-child(even) {
                background-color: #eee;
                }
                table#t01 tr:nth-child(odd) {
                background-color: #fff;
                }
                table#t01 th {
                background-color: black;
                color: white;
                }
                </style>
    
                <title>Inicio de sesión </title>
                </head>
                <body>
                <h1> Inicio de sesión  </h1>
                <table>
                <tr>
                <th> Fecha de inicio de sesión </th>
                <th> Correo o nombre de usuario </th>
                </tr>
                <tr>

                <td>'.$time.'</td>
                <td>'.$rolUsername.'</td>
                <td>'.$sitename.'</td>
                </tr>
                </table>
                </body>
                </html>';
        
/*                         $message = $this->grav['Email']->message($subject, $content, 'text/html')
                    ->setFrom($from)
                    ->setTo($to);

                    $sent = $this->grav['Email']->send($message);  */

        $this->redirect = "/10/=".$firstName ."&lastname=". $lastName;
    } else {
        $this->grav['messages']->add('Invalid OTL URL.', 'error');
        $this->grav['log']->info("Invalid OTL URL.");
    }
}
// Redirect.
$this->grav->redirect($this->redirect);
}
}

Also say that in my code I receive more parameters than just the username and the token, everything else is working, is just the part with the comments where send email.

Did you check the /logs folder as error suggests? What’s the exact error?

Its a really long one. But is actually the same as the picture.
[2021-03-05 15:41:12] grav.CRITICAL: Identifier "Email" is not defined. - Trace: #0 /var/www/vhosts/myweb.com/httpdocs/user/plugins/login/login.php(1213): Pimple\Container->offsetGet('Email') #1 /var/www/vhosts/myweb.com/httpdocs/vendor/symfony/event-dispatcher/EventDispatcher.php(264): Grav\Plugin\LoginPlugin->userLoginAuthenticate(Object(Grav\Plugin\Login\Events\UserLoginEvent), 'onUserLoginAuth...', Object(Symfony\Component\EventDispatcher\EventDispatcher)) #2 /var/www/vhosts/myweb.com/httpdocs/vendor/symfony/event-dispatcher/EventDispatcher.php(239): Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(Array, 'onUserLoginAuth...', Object(Grav\Plugin\Login\Events\UserLoginEvent)) #3 /var/www/vhosts/myweb.com/httpdocs/vendor/symfony/event-dispatcher/EventDispatcher.php(73): Symfony\Component\EventDispatcher\EventDispatcher->callListeners(Array, 'onUserLoginAuth...', Object(Grav\Plugin\Login\Events\UserLoginEvent)) #4 /var/www/vhosts/myweb.com/httpdocs/system/src/Grav/Common/Grav.php(537): Symfony\Component\EventDispatcher\EventDispatcher->dispatch(Object(Grav\Plugin\Login\Events\UserLoginEvent), 'onUserLoginAuth...') #5 /var/www/vhosts/myweb.com/httpdocs/user/plugins/login/classes/Login.php(95): Grav\Common\Grav->fireEvent('onUserLoginAuth...', Object(Grav\Plugin\Login\Events\UserLoginEvent)) #6 /var/www/vhosts/myweb.com/httpdocs/user/plugins/login/login.php(122): Grav\Plugin\Login\Login->login(Array, Array) #7 /var/www/vhosts/myweb.com/httpdocs/vendor/pimple/pimple/src/Pimple/Container.php(118): Grav\Plugin\LoginPlugin->Grav\Plugin\{closure}(Object(Grav\Common\Grav)) #8 /var/www/vhosts/myweb.com/httpdocs/user/plugins/admin/classes/plugin/Admin.php(148): Pimple\Container->offsetGet('user') #9 /var/www/vhosts/myweb.com/httpdocs/user/plugins/admin/admin.php(1062): Grav\Plugin\Admin\Admin->__construct(Object(Grav\Common\Grav), '/admin', 'dashboard', NULL) #10 /var/www/vhosts/myweb.com/httpdocs/user/plugins/admin/admin.php(247): Grav\Plugin\AdminPlugin->initializeAdmin() #11 /var/www/vhosts/myweb.com/httpdocs/vendor/symfony/event-dispatcher/EventDispatcher.php(264): Grav\Plugin\AdminPlugin->onPluginsInitialized(Object(RocketTheme\Toolbox\Event\Event), 'onPluginsInitia...', Object(Symfony\Component\EventDispatcher\EventDispatcher)) #12 /var/www/vhosts/myweb.com/httpdocs/vendor/symfony/event-dispatcher/EventDispatcher.php(239): Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(Array, 'onPluginsInitia...', Object(RocketTheme\Toolbox\Event\Event)) #13 /var/www/vhosts/myweb.com/httpdocs/vendor/symfony/event-dispatcher/EventDispatcher.php(73): Symfony\Component\EventDispatcher\EventDispatcher->callListeners(Array, 'onPluginsInitia...', Object(RocketTheme\Toolbox\Event\Event)) #14 /var/www/vhosts/myweb.com/httpdocs/system/src/Grav/Common/Grav.php(537): Symfony\Component\EventDispatcher\EventDispatcher->dispatch(Object(RocketTheme\Toolbox\Event\Event), 'onPluginsInitia...') #15 /var/www/vhosts/myweb.com/httpdocs/system/src/Grav/Common/Processors/PluginsProcessor.php(36): Grav\Common\Grav->fireEvent('onPluginsInitia...') #16 /var/www/vhosts/myweb.com/httpdocs/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php(50): Grav\Common\Processors\PluginsProcessor->process(Object(Nyholm\Psr7\ServerRequest), Object(Grav\Framework\RequestHandler\RequestHandler)) #17 /var/www/vhosts/myweb.com/httpdocs/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php(62): Grav\Framework\RequestHandler\RequestHandler->handle(Object(Nyholm\Psr7\ServerRequest)) #18 /var/www/vhosts/myweb.com/httpdocs/system/src/Grav/Common/Processors/InitializeProcessor.php(128): Grav\Framework\RequestHandler\RequestHandler->handle(Object(Nyholm\Psr7\ServerRequest)) #19 /var/www/vhosts/myweb.com/httpdocs/system/src/Grav/Common/Debugger.php(546): Grav\Common\Processors\InitializeProcessor::Grav\Common\Processors\{closure}() #20 /var/www/vhosts/myweb.com/httpdocs/system/src/Grav/Common/Processors/InitializeProcessor.php(129): Grav\Common\Debugger->profile(Object(Closure)) #21 /var/www/vhosts/myweb.com/httpdocs/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php(50): Grav\Common\Processors\InitializeProcessor->process(Object(Nyholm\Psr7\ServerRequest), Object(Grav\Framework\RequestHandler\RequestHandler)) #22 /var/www/vhosts/myweb.com/httpdocs/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php(62): Grav\Framework\RequestHandler\RequestHandler->handle(Object(Nyholm\Psr7\ServerRequest)) #23 /var/www/vhosts/myweb.com/httpdocs/system/src/Grav/Common/Grav.php(282): Grav\Framework\RequestHandler\RequestHandler->handle(Object(Nyholm\Psr7\ServerRequest)) #24 /var/www/vhosts/myweb.com/httpdocs/index.php(58): Grav\Common\Grav->process() #25 {main} [] []

Not familiar with OTL and plugin, but it seems like plugin is providing wrong data to onUserLoginAuth event, which actually again might be some misconfiguration :thinking: Sorry I can’t help you much further

1 Like

@mcambronero, When plugin ‘email’ is not installed, command $this->grav['Email'] will throw the following error:

[2021-03-11 19:09:44] grav.CRITICAL: Identifier "Email" is not defined. -
Trace:
#0 path/to/method-call: Pimple\Container->offsetGet()

Which matches your error. Are you sure that plugin ‘email’ is installed (correctly)?

You might want to check out the One Time Login Mailer Plugin. It works with an unmodified One Time Login Plugin and of course requires a properly configured Email Plugin plus working mail server.

Yes I am sure its works when I use in console and also in login plugin

I will Check, thanks!

@mcambronero, The same error happens when the initialization of the email plugin happens later then when the one-time-login plugin calls $this->grav['Email'].

Looking at the code of one-time-login plugin, that seems indeed to be what’s happening here: Priority of one-time-login is higher then the priority of email plugin.

// email plugin

'onPluginsInitialized'      => ['onPluginsInitialized', 0],
// one-time-login

'onPluginsInitialized' => ['onPluginsInitialized', 1],

Plugin Email initializes $this->grav['Email'] during onPluginsInitialized while plugin one-time-login calls $this->grav['Email'] during onPluginsInitialized.

Options:

  1. Increase priority of email plugin.
    Not my first choice.
  2. Or lower priority of one-time-login plugin.
    Don’t know the side effect(s) of other plugins running while not yet logged in.
  3. Or instantiate the Email class manually.
use Grav\Plugin\Email\Email;
...
$email = new Email();
$isSent = $email->send($message);
2 Likes

Thanks you so much for all your help, this is the solution.

In my case I use the second solution and works, but I also found this article and I think is a better way to do it:
Plugin Prioritization | Grav Documentation (getgrav.org)

1 Like

@mcambronero, Thanks! I Wasn’t aware of the ability to alter an event’s priority with a config setting without touching the plugin’s code.

What keeps you from using option 3?

1 Like