Problem with email template in custom notification

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
Appaulling
Registered User
Posts: 7
Joined: Mon Mar 11, 2019 9:33 pm

Problem with email template in custom notification

Post by Appaulling » Mon Mar 11, 2019 9:48 pm

Hi everyone,

I am trying to develop an extension that sends a custom notification type every time a new user registers on the board. So far everything is working great, however I have a problem with the email templates.

I have a template file located in the ext folder at at

Code: Select all

Paul/notifyregistration/language/en/email/new_user_registration.txt
I want to use this template for sending mails. As described in this post I implemented this method in my notification class:

Code: Select all

public function get_email_template()
{
    return '@Paul_notifyregistration/new_user_registration';
}
However whenever I try to register a new account I now get this error:

Code: Select all

PHP Fatal error:  Uncaught exception 'Twig_Error_Loader' with message 'Unable to find template "@Paul_notifyregistration/new_user_registration.txt" (looked into: ).'
If I simply return false in the get_email_template() function everything works as expected (apart from the fact that I obviously get no mails or jabber messages). I am kind of at a loss here what I am doing wrong and would appreciate some pointers in the right direction.

User avatar
mrgoldy
Jr. Extension Validator
Posts: 1044
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: Problem with email template in custom notification

Post by mrgoldy » Mon Mar 11, 2019 10:49 pm

First of all, vendor and extension should all be lowercase.
So I'm afraid I am the bringer of bad news and you'll have to change your namespaces to use a lowercase "p" in paul.

Secondly, I don't think you'll have to use the registered namespace approach, which is the @vendor_extension.
As this is for template paths style/../template.

Looking at the code, you do not have the specify your extension, the language or the email part.
So I think you can simply use new_user_registration.

If that's the case and it works out, I would highly suggest prefixing that specific file with your vendor and extension.
What I mean with that is renaming new_user_registration to paul_notifyregistration_something.
This to ensure you are not accidentaly overriding any future core files and/or other extension files.
However unlikely, better safe than sorry.

Lastly, it's always better to be complete. And what I mean with that, that the full error message would be rather interesting.
For example, in this instance, it would help out to see what actually comes after Looked into: ....
phpBB Studio / ''Proud member of the Studio"

Appaulling
Registered User
Posts: 7
Joined: Mon Mar 11, 2019 9:33 pm

Re: Problem with email template in custom notification

Post by Appaulling » Tue Mar 12, 2019 10:55 am

mrgoldy wrote:
Mon Mar 11, 2019 10:49 pm
First of all, vendor and extension should all be lowercase.
So I'm afraid I am the bringer of bad news and you'll have to change your namespaces to use a lowercase "p" in paul.
Thanks for the note, I've changed that.
mrgoldy wrote:
Mon Mar 11, 2019 10:49 pm
Looking at the code, you do not have the specify your extension, the language or the email part.
So I think you can simply use new_user_registration.
I have tried that, but it did not work either. However now the error message says that twig at least tried to look for the mail template file:
PHP Fatal error: Uncaught exception 'Twig_Error_Loader' with message 'Unable to find template "new_user_registration.txt" (looked into: <INSTALL_ROOT>/language/de/email, <INSTALL_ROOT>/language/en/email).' in <INSTALL_ROOT>/vendor/twig/twig/lib/Twig/Loader/Filesystem.php:232\nStack trace:\n#0 <INSTALL_ROOT>/phpbb/template/twig/loader.php(135): Twig_Loader_Filesystem->findTemplate('new_registratio...')\n#1 <INSTALL_ROOT>/vendor/twig/twig/lib/Twig/Loader/Filesystem.php(150): phpbb\\template\\twig\\loader->findTemplate('new_registratio...')\n#2 <INSTALL_ROOT>/vendor/twig/twig/lib/Twig/Environment.php(330): Twig_Loader_Filesystem->getCacheKey('new_registratio...')\n#3 <INSTALL_ROOT>/vendor/twig/twig/lib/Twig/Environment.php(424): Twig_Environment->getTemplateClass('new_registratio...')\n#4 <INSTALL_ROOT>/phpbb/template/twig/environment.php(277): Twig_Environment->loadTemplate('new_registratio...', NULL)\n#5 /va in <INSTALL_ROOT>/vendor/twig/twig/lib/Twig/Loader/Filesystem.php on line 232
mrgoldy wrote:
Mon Mar 11, 2019 10:49 pm
Lastly, it's always better to be complete. And what I mean with that, that the full error message would be rather interesting.
For example, in this instance, it would help out to see what actually comes after Looked into: ....
Sorry about that, here is the complete original error message:
PHP Fatal error: Uncaught exception 'Twig_Error_Loader' with message 'Unable to find template "@paul_notifyregistration/new_user_registration.txt" (looked into: ).' in <INSTALL_ROOT>/vendor/twig/twig/lib/Twig/Loader/Filesystem.php:232\nStack trace:\n#0 <INSTALL_ROOT>/phpbb/template/twig/loader.php(135): Twig_Loader_Filesystem->findTemplate('@paul_notifyregi...')\n#1 <INSTALL_ROOT>/vendor/twig/twig/lib/Twig/Loader/Filesystem.php(150): phpbb\\template\\twig\\loader->findTemplate('@paul_notifyregi...')\n#2 <INSTALL_ROOT>/vendor/twig/twig/lib/Twig/Environment.php(330): Twig_Loader_Filesystem->getCacheKey('@paul_notifyregi...')\n#3 <INSTALL_ROOT>/vendor/twig/twig/lib/Twig/Environment.php(424): Twig_Environment->getTemplateClass('@paul_notifyregi...')\n#4 <INSTALL_ROOT>/phpbb/template/twig/environment.php(292): Twig_Environment->loadTemplate('@paul_notifyregi...', NULL)\n#5 <INSTALL_ROOT>/vendor/twig/twig/lib/Twig/Environment.php(363): in <INSTALL_ROOT>/vendor/twig/twig/lib/Twig/Loader/Filesystem.php on line 232
Considering that twig does not even attempt to look in any folders I am wondering whether maybe the syntax of the template identifier @paul_notifyregistration/new_user_registration might be wrong?

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3704
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Problem with email template in custom notification

Post by kasimi » Tue Mar 12, 2019 11:12 am

The template identifier is correct. When and where are you creating the notification?

Appaulling
Registered User
Posts: 7
Joined: Mon Mar 11, 2019 9:33 pm

Re: Problem with email template in custom notification

Post by Appaulling » Tue Mar 12, 2019 11:24 am

In the ucp_register_after event. This is my listener class:

Code: Select all

class listener implements EventSubscriberInterface
{
  /** @var \phpbb\notification\manager */
  protected $notification_manager;

  public function __construct(\phpbb\notification\manager $manager)
  {
    $this->notification_manager = $manager;
  }

  static public function getSubscribedEvents()
  {
    return array(
      'core.user_setup'                             => 'user_setup',
      'core.ucp_register_register_after'            => 'ucp_register_register_after',
    );
  }


  public function user_setup($event)
  {
    // Load the language file
    $lang_set_ext = $event['lang_set_ext'];
    $lang_set_ext[] = array(
      'ext_name' => 'paul/notifyregistration',
      'lang_set' => 'notifyregistration',
	);
    $event['lang_set_ext'] = $lang_set_ext;
  }

  public function ucp_register_register_after($event)
  {
    $this->notification_manager->add_notifications(
      'paul.notifyregistration.notification.type.new_registration', array(
        'user_id' => $event['user_id'],
        'user_actkey' => $event['user_actkey'],
        'user_regdate' => time()
      )
    );
  }

}

Appaulling
Registered User
Posts: 7
Joined: Mon Mar 11, 2019 9:33 pm

Re: Problem with email template in custom notification

Post by Appaulling » Sun Mar 17, 2019 12:23 am

Out of desperation I tried to simply use the existing template admin_activate. This did not work either (same error message), however twig now at least tried to look at <INSTALL_ROOT>/language/de/email/short. If I change it however to a template that does not exist (saldfkjowe) twig then tries to look in <INSTALL_ROOT>/language/de/email/. :?

So it seems that twig behaves as follows:
  • Return a vendor_extension template: twig looks nowhere
  • Return a non-existing template: twig looks in language/de/email
  • Return an existing template: twig looks in language/de/email/short
I can't really make any sense of this. Am I missing something fundamental?

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3704
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Problem with email template in custom notification

Post by kasimi » Sun Mar 17, 2019 11:03 am

It's possible that the location where the event is triggered has something to do with it, although I couldn't spot anything suspicious at a first glance. Try using a different event to send your notification, for example core.search_modify_forum_select_list. Make sure to temporarily adjust your handler method to not access undefined event data.

Appaulling
Registered User
Posts: 7
Joined: Mon Mar 11, 2019 9:33 pm

Re: Problem with email template in custom notification

Post by Appaulling » Sun Mar 17, 2019 2:27 pm

Just tried that out, same error message unfortunately :(

Clearing the cache, reinstalling the extension and deactivating all other extensions also has no effect. Could this be a bug in phpBB?

User avatar
3Di
Former Team Member
Posts: 13686
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Problem with email template in custom notification

Post by 3Di » Sun Mar 17, 2019 5:59 pm

DId you create a DE folder in your extension?
Paul/notifyregistration/language/de/email/new_user_registration.txt
Please PM me only to request paid works. Thx.
Want to compensate me for my interest? Donate
My development's activity º PhpStorm's proud user
Extensions, Scripts, MOD porting, Update/Upgrades
👨‍🏫 | Take a tour to | The Studio | 👨‍🏫

Appaulling
Registered User
Posts: 7
Joined: Mon Mar 11, 2019 9:33 pm

Re: Problem with email template in custom notification

Post by Appaulling » Sun Mar 17, 2019 10:45 pm

Yes. Here is my full folder structure:
Attachments
Screenshot_2019-03-17_23-42-16.png

User avatar
3Di
Former Team Member
Posts: 13686
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Problem with email template in custom notification

Post by 3Di » Sun Mar 17, 2019 11:19 pm

The best way to help you is to have all the code in front of our eyes, I suggest you put everything on GitHub so we can get a good deal out of it.
Please PM me only to request paid works. Thx.
Want to compensate me for my interest? Donate
My development's activity º PhpStorm's proud user
Extensions, Scripts, MOD porting, Update/Upgrades
👨‍🏫 | Take a tour to | The Studio | 👨‍🏫

User avatar
david63
Registered User
Posts: 16093
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Problem with email template in custom notification

Post by david63 » Mon Mar 18, 2019 8:31 am

It might be worth taking a look at the Digests extension as I believe that Mark does something along these lines
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

Appaulling
Registered User
Posts: 7
Joined: Mon Mar 11, 2019 9:33 pm

Re: Problem with email template in custom notification

Post by Appaulling » Mon Mar 18, 2019 1:13 pm

I finally figured it out! :D

The problem was actually jabber. Turns out that for sending notifications via IM, phpBB uses templates in language/de/email/short. If that folder is not there then phpBB simply fails with the above error message. I solved the problem by simply creating the short folder and copying the email templates there.

Thanks to everyone who tried to help me. I did not expect such a simple extension to cause me so much confusion :D

User avatar
3Di
Former Team Member
Posts: 13686
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Problem with email template in custom notification

Post by 3Di » Mon Mar 18, 2019 9:54 pm

Thanks for sharing BTW. :)
Please PM me only to request paid works. Thx.
Want to compensate me for my interest? Donate
My development's activity º PhpStorm's proud user
Extensions, Scripts, MOD porting, Update/Upgrades
👨‍🏫 | Take a tour to | The Studio | 👨‍🏫

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3704
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Problem with email template in custom notification

Post by kasimi » Wed Mar 20, 2019 4:06 pm

Appaulling wrote:
Sun Mar 17, 2019 2:27 pm
Could this be a bug in phpBB?
https://tracker.phpbb.com/browse/PHPBB3-15998

Post Reply

Return to “Extension Writers Discussion”