Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

How to create custom event hooks?

Hi, I wondered if someone could help me. I want to create an event hook/handler and have been reading the docs here and here but am getting nowhere.

I'd like to hook into the Gdn_Email::SendMail event, with the intention of wrapping a template around email content.

So far I have created a class..

class MyHooks extends Gdn_Plugin {
    public function Base_sendmail_handler($sender, $args) {

        var_dump($sender);
        die;

    }
}

But it doesn't seem to be getting called. Any ideas?

Answers

  • I've tried adding...

        public function Gdn_Email_Sendmail_Handler($sender, $args) {
    
            var_dump($sender);
            die;
    
        }
    

    ..to my theme hooks class as well but still getting nothing

  • hgtonighthgtonight ∞ · New Moderator

    You cannot create events via a plugin method name. You can hook into events via a plugin method name.

    Check out the Eventi addon. It hooks into every event and spits out some markup detailing the event name and arguments.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • pavsidpavsid New
    edited January 2016

    Hi @hgtonight - i'm not trying to create an event, i want to create an event hook/handler. I've checked out the Eventi addon but it's not really helping me here as the Gdn_Email::SendMail event i'm testing this on is triggered by posting a comment on someone's wall, which is done by AJAX, and doesn't work at all if i enable that plugin.

    Any ideas how I format my function name so that it is recognised as the handler for Gdn_Email::SendMail?

  • R_JR_J Ex-Fanboy Munich Admin

    Your second version is more correct. But as far as I can see, it will fail nevertheless. In class.email.php there is this code:

        public function send($EventName = '') {
            $this->fireEvent('BeforeSendMail');
    [...]
            if ($EventName != '') {
                $this->EventArguments['EventName'] = $EventName;
                $this->fireEvent('SendMail');
            }
    

    Looking through the code, the send() method is nearly always called without passing any parameter, so I guess the "SendMail" Event is never fired. If you want to make sure you never miss it, you would have to create a public function base_sendMail_handler($sender, $args) { function in your themehooks.

    But even if your code gets called, it might be quite useless. You can only alter what has been passed as an "EventArgument" and none of the critical information (nearly no information at all!) has been passed here.

    So wrapping all emails will not work that way. Maybe you could "override" the class with that factory-something (ask hgtonight since he knows about things like that) but that would be no fun.

    You could also make a pull request on GitHub so that this event would be useful, maybe something like

    $this->EventArguments['PhpMailer'] = &$this->PhpMailer;
    $this->fireEvent('SendMail');
    

    And you could already implement that in your own copy of Vanilla, always needing to remember, that you have to check this after any updates.

  • hgtonighthgtonight ∞ · New Moderator
    edited January 2016

    @R_J said:

    $this->EventArguments['PhpMailer'] = &$this->PhpMailer;
    $this->fireEvent('SendMail');
    

    This is not required as the PhpMailer is a public member of the Gdn_Email class which is sent as the $Sender object.

    @pavsid said:

    Any ideas how I format my function name so that it is recognised as the handler for Gdn_Email::SendMail?

    Sorry for misunderstanding you.

    gdn_email_sendMail_handler($sender)
    

    This will work with the caveats R_J pointed out above.

    Considering nothing changes between beforeSendMail and sendMail except phpMailer configurations, I would use the beforeSendMail event.

    gdn_email_beforeSendMail_handler($sender)
    

    Hope this helps.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • Dammit, seems that the $this->fireEvent('BeforeSendMail'); event was only added in v2.2. Guess I could just add this event myself.

    However, I see that there's reference to email.master masterview, and although the setter method for this value is down as a TODO, I wonder if i could just edit the email.master itself to my liking? I've tried searching for this file though and can't find it anywhere.

Sign In or Register to comment.