1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to extend the XenForo_Mail class

Discussion in 'XenForo Development Discussions' started by Sadik B, Dec 31, 2013.

  1. Sadik B

    Sadik B Well-Known Member

    I noticed the load_class_mail event listener. Assuming that it works similar to how controllers are extended, I created a code event listener MyAddon_Listener::load_class_mail

    PHP:
    public static function load_class_mail($class, array &$extend)
        {
            if (
    $class == 'XenForo_Mail')
            {
                
    $extend[] = 'MyAddon_Mail';
            }
        }
    However the class MyAddon_Mail is not extending the send() method. Am I doing this wrong? How can one go about extending the XenForo_Mail::send() method?

    Thanks...
     
  2. Daniel Hood

    Daniel Hood Well-Known Member

    I haven't actually done this but if it does in fact work the same way as the controllers, you'd make a new file with

    PHP:
    Class myaddon_mail extends XFCP_myaddon_mail
    {
     Public function 
    send()
     {
       
    $parent parent::send(); // so you can get any data returned by the parent.
     
    }
    }
     
  3. Sadik B

    Sadik B Well-Known Member

    Thanks for replying. Yeah, this is what I did and this does not work...
     
  4. Chris D

    Chris D XenForo Developer Staff Member

    From what I can tell, load_class_mail should work, especially if XenForo_Mail is instantiated using the XenForo_Mail::create method.

    If you're certain it's not working, you might want to try the load_class event instead.

    One or other should work fine.
     
    Sadik B likes this.
  5. Sadik B

    Sadik B Well-Known Member

    Nopes... Still not working...

    PHP:
    public static function load_class($class, array &$extend)
        {
            if (
    $class == 'XenForo_Mail')
            {
                
    $extend[] = 'MyAddon_Mail';
            }
        }
    And in library/MyAddon/Mail.php

    PHP:
    class MyAddon_Mail extends XFCP_MyAddon_Mail
    {
        public function 
    send($toEmail$toName '', array $headers = array(), $fromEmail ''$fromName ''$returnPath '')
        {
            
    $userModel XenForo_Model::create('XenForo_Model_User');
            
    $user $userModel->getUserByEmail($toEmail);

            if (!empty(
    $user))
            {
                
    $tempArr explode(','$user['secondary_group_ids']);

                if (!
    in_array('17'$tempArr))
                {
                    return 
    false;
                }
                else
                    return 
    parent::send($toEmail$toName$headers$fromEmail$fromName$returnPath);
            }
        }
    }
    The send() method is not being overridden. I tried a simple vardump, no luck either. Does anyone has a working example of extending the XenForo_Mail class?
     
  6. Sadik B

    Sadik B Well-Known Member

    Any ideas why the above code is not overriding XenForo_Mail @Chris Deeming ?

    And @Rigel Kentaurus

    I saw you posted this,

    It would be an immense help if you could show how you did this and which event listener you used?

    Thanks guys.... :)
     
  7. Sean Kendle

    Sean Kendle Member

    I, too, find myself in the position of needing to extend the Mail class/functions. Can anyone help with this? Has anyone in this thread figured it out by now?
     

Share This Page