• 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

Sadik B

Well-known member
#1
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...
 

Daniel Hood

Well-known member
#2
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.
 }
}
 

Sadik B

Well-known member
#3
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.
}
}
Thanks for replying. Yeah, this is what I did and this does not work...
 

Chris D

XenForo developer
Staff member
#4
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

Well-known member
#5
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?
 
#7
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?