Please upgrade here. These earlier versions are no longer being updated and have security issues.

How to override Gdn_Email/PhpMailer?

Hello there,
I'm trying to extend PhpMailer functionality to send email via Mandrill API. I managed to do it directly modifying the original PhpMailer file (Library/Vendors/phpmailer/class.phpmailer.php) and it works beautifully.

Now I'd like to write a plugin so other users can have the same functionality just installing it, without modifying original core files, but I cannot manage to override PhpMailer or Gdn_Email in any way.

I tried this in bootstrap.early.php:
Gdn::FactoryInstall('PHPMailer', 'MyPHPMailer', NULL, Gdn::FactoryInstance); Gdn::FactoryInstall('Gdn_Email', 'My_Email', NULL, Gdn::FactoryInstance);
With MyPHPMailer and My_Email classes in class.myphpmailer.php and class.myemail.php files inside my plugin directory, but they never get called.
class.myphpmailer.php and class.myemail.php are exact copies of original class.phpmailer.php and with some LogMessage() added to check if they get called and the name classes changed.

They never get called.

I tried to override just Gdn_Email::send() with _create:
public function Gdn_Email_Send_Create($Sender) { LogMessage(basename(__FILE__),__LINE__,__CLASS__,__METHOD__,"IT IS WORKING!"); }
But this didn't work either.

I'm working on Vanilla 2.1.

Does anyone have some suggestions about the correct way to extend this part of core functionality?


Franco Solerio


  • x00x00 MVP
    edited May 2014

    You have the SendMail event you could use, maybe swap out the PhpMailer property at this point with another class.

    public function Base_SendMail_Handler($Sender){
       $Sender->PhpMailer = new CustomMailer($Sender->PhpMailer);

    grep is your friend.

  • sorry that won't work where there is no event name.

    grep is your friend.

  • hgtonighthgtonight ∞ · New Moderator

    Gdn::FactoryInstall('PHPMailer', 'MyPHPMailer', '~/plugins/YourPluginName/path/to/class.php'); in your bootstrap should work.

    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.

  • x00x00 MVP
    edited May 2014

    it is not a a factory. However as vanilla uses an autoloader.

    You simply have to define Gdn_Email class before it is used. You can include it the old fashioned way from an early hook. it will ignore the core file.

    grep is your friend.

  • @hgtonight‌: Tried right now, but it doesn't work.


    <?php if (!defined('APPLICATION')) exit(); Gdn::FactoryInstall('PHPMailer', 'MyPHPMailer', '~/plugins/MandrillMailer/class.myphpmailer.php'); Gdn::FactoryInstall('Gdn_Email', 'My_Email', '~/plugins/MandrillMailer/class.myemail.php');

  • It is not a factory. See above post.

    grep is your friend.

  • Thanks @x00‌, this worked!

    public function Gdn_Dispatcher_BeforeDispatch_Handler($Sender) { require_once 'plugins/MandrillMailer/'; }
    Can any problem arise from hooking Gdn_Dispatcher_BeforeDispatch_Handler?

  • x00x00 MVP
    edited May 2014


    this technique should be used sparingly though. Gnd_Email is a fairly benign helper class, so it is ok for that. it is fairly well decoupled from the framework.

    The core class does extend Gdn_Pluggable, but really, there is limited mileage in the case of this class.

    grep is your friend.

Sign In or Register to comment.