mauricerenck / komments

A Kirby comment plugin
MIT License
44 stars 8 forks source link

E-Mail notification for new comments #37

Closed alexanderhacke closed 2 years ago

alexanderhacke commented 2 years ago

Thanks for the great plugin!

Would it be possible to receive an email notification as soon as a new comment has been made?

mauricerenck commented 2 years ago

Glad you like it. That's indeed a feature on my list. I'll give it a higher prio then.

alexanderhacke commented 2 years ago

Sounds awesome! Thank you so much. If i can do a donation or something like this, let me know :)

mauricerenck commented 2 years ago

This feature was just released with version 1.5.0 :)

MaluNoPeleke commented 2 years ago

@mauricerenck Thanks a lot for this helpful feature. I tried to set it up with Komments 1.5.1 but didn't receive an email when I added a test comment by myself. What could be missing here?

config.php:

    'email' => [
      'transport' => [
        'type' => 'smtp',
        'host' => 'smtp.gmail.com',
        'port' => 587,
        'security' => true,
        'auth' => true,
      ],
      'beforeSend' => function ($mailer) {
        $mailer->AuthType = 'XOAUTH2';
      // if the kirby3-dotenv plugin is installed, 
      // env() reads your credentials from a .env file
        $email = ('my.email@gmail.com');
        $clientId = ('workingclientid.apps.googleusercontent.com');
        $clientSecret = ('myclientsecret');
        $refreshToken = ('verylongrefreshtokenhere');

        $provider = new Google(
          [
            'clientId' => $clientId,
            'clientSecret' => $clientSecret,
          ]
        );

        $mailer->setOAuth(
          new OAuth(
            [
              'provider' => $provider,
              'clientId' => $clientId,
              'clientSecret' => $clientSecret,
              'refreshToken' => $refreshToken,
              'userName' => $email,
            ]
          )
        );

        return $mailer;
      },
    ],
    'mauricerenck.komments.notifications.email.enable' => true,
    'mauricerenck.komments.notifications.email.sender' => 'komments@domaint.tld',
    'mauricerenck.komments.notifications.email.emailReceiverList' => ['kontakt@domain.tld'],
mauricerenck commented 2 years ago

Can you send emails from that address from within google mail?

MaluNoPeleke commented 2 years ago

I changed it to my.email@gmail.com but without success.

'mauricerenck.komments.notifications.email.sender' => 'my.email@gmail.com'

I tested email sending with this simple code and it works as expected:

<?php
require __DIR__ . '/kirby/bootstrap.php';

$kirby = new Kirby;

try {
  $kirby->email([
    'from' => 'my.email@gmail.com',
    'replyTo' => 'no-reply@supercompany.com',
    'to' => 'kontakt@domain.tld',
    'subject' => 'Welcome!',
    'body'=> 'It\'s great to have you with us',
  ]);
} catch (Exception $error) {
  echo $error;
}
?>

By default it should send out an email instantly, right? What other possibilities are there to debug it further?

mauricerenck commented 2 years ago

If you didn't change it to "cron" this should send out an email immediately. Also errors within the plugin will be shown below the form. But I have the feeling, that the nested config string isn't working this way. Can you please write your settings like so:

'mauricerenck' => [
        'komments' => [
            EXISTINGSETTINGS,
            'notifications' => [
                'email' => [
                    'enable' => true,
                    'sender' => 'test@domain.tld',
                    'emailReceiverList' => ['test@domain.tld'],
                ]
            ]
        ]
    ]

And check if this is working? I fear kirby doesn't split the config string after mauricerenck.komments.notifications in seperate parts.

MaluNoPeleke commented 2 years ago

Now we are getting somewhere ;) A new error pops up now when I write a new comment:

foreach() argument must be of type array|object, null given

mauricerenck commented 2 years ago

can you look into the console, maybe there is a more detailed message. unfortunately this could happen nearly anywhere

MaluNoPeleke commented 2 years ago

POST https://dev.domain.tld/komments/send 500 sendKomment @ komments.js:78 (anonymous) @ komments.js:122

line 78 of the JS file is this:

    window.fetch(kommentFormAction, {
        method: 'post',
        headers: { 'Content-Type': 'application/json', 'X-Return-Type': 'json' },
        body: JSON.stringify(Object.fromEntries(new FormData(event.target))),
    })
mauricerenck commented 2 years ago

Please have a look into the response tab of the request, you may see some php error message in there

MaluNoPeleke commented 2 years ago

{"status":"error","exception":"Whoops\Exception\ErrorException","code":2,"message":"foreach() argument must be of type array|object, null given","details":null,"file":"dev.doman.tld\/site\/plugins\/komments\/utils\/sendWebmention.php","line":131}

mauricerenck commented 2 years ago

Gna… I have to finish moving that webmention sending code to indieconnector… this doesn't have anything todo with the email stuff, I guess. Can you try commenting another post, where sending webmention wouldn't get into the way, just to ensure emails are working?

MaluNoPeleke commented 2 years ago

Since I changed the config to the one you suggested in comment 997893562 this error appears always on every comment page. Before it never happened.

My integration in the template looks like this:

    <div class="w-full max-w-none flex flex-col text-center md:text-left shadow bg-white mb-10 p-6 nomark">
    <p class="font-semibold text-2xl"><?php echo t('Comments', 'Kommentare') ?></p>
    <a class="font-medium text-xl text-center py-2" href="#kommentform"><?php echo t('Comment', 'Jetzt kommentieren ⬇️') ?></a>
    <?php
     snippet('komments/webmention-splitted');
     snippet('komments/kommentform'); 
    ?>
    </div>

This is the current config:

    'mauricerenck' => [
      'komments' => [
          'auto-delete-spam' => false,
          'notifications' => [
              'email' => [
                  'enable' => true,
                  'sender' => 'my.email@gmail.com',
                  'emailReceiverList' => ['kontakt@domain.tld'],
              ]
          ]
      ]
    ],
    'mauricerenck.indieConnector.secret' => 'webkomments',
    'mauricerenck.indieConnector.stats' => true,
    'mauricerenck.indieConnector.sqlitePath' => 'site/logs/',
mauricerenck commented 2 years ago

🤔 So the error occures, where the plugin looks for fields to find urls in. That setting can be found here: https://github.com/mauricerenck/komments/blob/main/docs/options.md#soon-deprecated and is called send-mention-url-fields.

Is that the full komments config you posted above? Did you set those fields before and forgot to move them to the new config?

So what happens is: You submit the form. A new comment ist created and stored to the markdown file. This triggers the page-update-hook, which tries to send webmentions.

You must either hand in the send-mention-url-fields option in your config or disable sending webmentions via `send-mention-on-update´.

As I look into the docs this isn't documented very well. But this feature will be removed from the plugin anyway.

MaluNoPeleke commented 2 years ago

I haven't touched the options regarding the sendmention feature because it says "Soon deprecated" in the documentation. Now after adding 'send-mention-on-update' => false, finally receiving email notifications about new comments works, thanks a lot for helping me debugging it!

alexanderhacke commented 2 years ago

The email notification works perfectly for me. Thanks a lot!

One little thing I noticed. In the notification email is the link: https://domain.de/panel/site/komments

However, this should read: https://domain.de/panel/komments