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 class.email.php 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?
Thanks
Franco Solerio
digitalia.fm
Comments
You have the
SendMail
event you could use, maybe swap out the PhpMailer property at this point with another class.grep is your friend.
sorry that won't work where there is no event name.
grep is your friend.
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.
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.
bootstrap.early.php:
<?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/class.email.php'; }
Can any problem arise from hooking Gdn_Dispatcher_BeforeDispatch_Handler?
nope
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.