Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

Send email after login with one-time-login plugin

first-time plugins

Solved by pamtbaau View solution

Started by Malcon 5 years ago · 12 replies · 652 views
5 years ago

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:
image|690x432

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

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 protected]';
                $from = '[email protected]';

                $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);
}
}
last edited 03/11/21 by Malcon
5 years ago

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.

5 years ago

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

5 years ago

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} [] []

5 years ago

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 🤔 Sorry I can't help you much further

👍 1
5 years ago

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

TXT
[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)?

5 years ago

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

5 years ago Solution

@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.

JS
// email plugin

'onPluginsInitialized'      => ['onPluginsInitialized', 0],
JS
// 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.
    PHP
    use Grav\Plugin\Email\Email;
    ...
    $email = new Email();
    $isSent = $email->send($message);
    
👍 2
last edited 03/15/21 by pamtbaau
5 years ago

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

5 years ago

@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

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 54 1 week ago
Plugins · by Xavier, 4 weeks ago
2 61 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1188 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 54 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 79 2 months ago