XF 2.1 Parsing Smilies in Emails

Sonnie

Active member
Trying my first add-on in an attempt to parse smilies in HTML emails.

I have created a Listener.php file and uploaded it to the SonnieMail folder... there are a few smilies in there for testing, as we mainly want these for media reviews.

Code:
namespace SonnieMail;

class Listener
{
   public static templatePostRender(\XF\Template\Templater $templater, $type, $template, &$output)
   {
      if ($type !== 'email') {
         return;
      }
      $output = strtr($output, [
         ':halfstar:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/half_star.png" border="0" />',
         ':1star:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/1_star.png" border="0" />,
         ':1.5stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/1.5_stars.png" border="0" />,
         ':2stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/2_stars.png" border="0" />,
         ':2.5stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/2.5_stars.png" border="0" />,
         ':3stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/3_stars.png" border="0" />,
         ':3.5stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/3.5_stars.png" border="0" />,
         ':4stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/4_stars.png" border="0" />,
         ':4.5stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/4.5_stars.png" border="0" />,
         ':5stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/5_stars.png" border="0" />,
        
      ]);
   }
}

The addon.json file is showing up in the SonnieMail folder after creating the addon via the cPanel Terminal.

Now I am trying Add code event listener... and for the Callback I've tried several combinations, but get an error:
204250

204249

Needing some guidance... thanks!
 
public static templatePostRender(\XF\Template\Templater $templater, $type, $template, &$output)

Should be...

public static function templatePostRender(\XF\Template\Templater $templater, $type, $template, &$output)
 
What is the path to the php file on your server?

And what are you entering in the listener?
 
It shows an example: Example: AddOn\Path\To\ClassName :: methodName

So I entered src\addons\SonnieMail\Listener

But I've tried several paths. I was instructed by someone else to enter SonnieMail\Listener, but that did not work either.

For ::methodname I have been entering templatePostRender.

The Listener.php file contents are shown above in the code.

First time doing this... so in the learning stages. Thanks!
 
Is this the ACTUAL path to your add-on and the Listener.php file?
src\addons\SonnieMail\Listener

If it is, then just SonnieMail\Listener should work. And the method templatePostRender is correct.

If that is NOT the actual path to your add-on, then that could be the problem (in addition to the "function" missing in the code in the first post).
 
Also check this section of your code. It isn't formatted properly..

Code:
$output = strtr($output, [
    ':halfstar:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/half_star.png" border="0" />',
    ':1star:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/1_star.png" border="0" />,
    ':1.5stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/1.5_stars.png" border="0" />,
    ':2stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/2_stars.png" border="0" />,
    ':2.5stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/2.5_stars.png" border="0" />,
    ':3stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/3_stars.png" border="0" />,
    ':3.5stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/3.5_stars.png" border="0" />,
    ':4stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/4_stars.png" border="0" />,
    ':4.5stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/4.5_stars.png" border="0" />,
    ':5stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/5_stars.png" border="0" />,
]);
 
The Listener.php file I created is showing up in public_html/src/addons/SonnieMail ... within the SonnieMail folder.

I tried just using SonnieMail and SonnieMail\Listener in the callback, but still got the error.

And of course I did add "function" to the Listener.php file.

Missing the closing apostrophe on the URL's?
 
I have to be missing something... can you copy/paste a screen shot of your input fields in ACP please?

Here is my updated code:

Code:
<?php

namespace SonnieMail;

class Listener
{
   public static function templatePostRender(\XF\Template\Templater $templater, $type, $template, &$output)
   {
      if ($type !== 'email') {
         return;
      }
      $output = strtr($output, [
         ':halfstar:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/half_star.png" border="0" />',
         ':1star:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/1_star.png" border="0" />',
         ':1.5stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/1.5_stars.png" border="0" />',
         ':2stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/2_stars.png" border="0" />',
         ':2.5stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/2.5_stars.png" border="0" />',
         ':3stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/3_stars.png" border="0" />',
         ':3.5stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/3.5_stars.png" border="0" />',
         ':4stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/4_stars.png" border="0" />',
         ':4.5stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/4.5_stars.png" border="0" />',
         ':5stars:' => '<img src="https://www.avnirvana.com/styles/default/xenforo/smilies/5_stars.png" border="0" />',
        
      ]);
   }
}
 
Okay... so I was using a capital L on listener... that's why it was not taking. duh
Well, that's what you show in your code. But if the file is called "listener.php" and not "Listener.php" that would explain it.
 
Now I have files in the output folder... SonnieMail/_output/code_event_listeners/_metadata.json ... and /templater_template_post_render_f62e4c953e36c021d8c336b8695f4e3e.json

Now we just need to see if it works.

Should I change the capital L in the file to lower case? Is that going to cause an issue?

Also... do you know the proper HTML to replace [float_left] and [float_right] bbcode?
 
I tried... but neither worked.

Code:
'[float_left]' => '<span class="image-left" padding="5px">',
'[/float_left]' => '</span>',

Code:
'[float_left]' => '<span style="float:left; padding:5px;">',
'[/float_left]' => '</span>',
 
Should I change the capital L in the file to lower case? Is that going to cause an issue?
Class name should match file name.

I tried... but neither worked.

Code:
'[float_left]' => '<span class="image-left" padding="5px">',
'[/float_left]' => '</span>',

Code:
'[float_left]' => '<span style="float:left; padding:5px;">',
'[/float_left]' => '</span>',
First one is wrong. Second one might work, but it needs to be done in admin panel, not in this file. Go to admin panel -> content -> custom bbcodes, find entry for that bbcode. Click advanced options and you'll see html email replacement. Put this as value:
Code:
<span style="float:left; padding:5px;">{text}</span>
 
Back
Top Bottom