Question

I am using the Mailgun Yii extension(https://github.com/baibaratsky/php-mailgun) and am able to send a test email from inside the 'views/site/SiteController.php' file using the following code:

    $message = Yii::app()->mailgun->newMessage();
    $message->setFrom('sender@domain.com', 'Sender Name');
    $message->addTo('recipient@domain.com', 'Recipient Name');
    $message->setSubject('Mailgun API library test');
    $message->setText('Test Email Content Text');
    $message->send();

Now I am trying to extend the CEmailLogRoute class so that I can send any log emails using mailgun with no success. This is the class I wrote to extend it:

class CMailGunLogRoute extends CEmailLogRoute {
    protected function sendEmail($email, $subject, $message) {
        $message = Yii::app()->mailgun->newMessage();
        $message->setFrom('sender@domain.com', 'Sender Name');
        $message->addTo($email);
        $message->setSubject($subject);
        $message->setText($message);
        $message->send();
    }
}

And this is what I added to the 'config/main.php' file:

'log'=>array(
   'class'=>'CLogRouter',
   'routes'=>array(
       array(
          'class'=>'CEmailLogRoute',
          'levels'=>'info',
          'emails'=>'recipient@domain.com',
          'sentFrom'=>'sender@domain.com',
          'subject'=>'Email Log File Message',
       ),
    ),
),

And this is the logging function I am declaring on the root index file:

function d2l($what, $where='fb.somewhere') {
   $what = print_r($what,true);
   Yii::log($what, 'info','application.'.$where);
}

And this is where I am calling that function from within the 'SiteController.php' file:

d2l('Test Log Message','site.index');

Unfortunately, none of this seems to get it to send the log email. Initially I tried to send the log email without using mailgun and that didn't work either, so perhaps the issue is with the code I wrote for the mail logging.

Was it helpful?

Solution

I figured out how to do this:

CMailGunLogRoute.php

// components/CMailGunLogRoute.php
class CMailGunLogRoute extends CEmailLogRoute {
  protected function sendEmail($email, $subject, $message) {
    $mail = Yii::app()->mailgun->newMessage();
    $mail->setFrom($this->getSentFrom());
    $mail->addTo($email);
    $mail->setSubject($subject);
    $mail->setText($message);
    $mail->send();
  }
}


SiteController.php

// controllers/SiteController.php
Yii::log('Test Log Message', 'info','application');


main.php

// config/main.php
'import'=>array(
    'application.components.*',
),

'mailgun' => array(
    'class' => 'application.extensions.php-mailgun.MailgunYii',
    'domain' => 'mydomain.com',
    'key' => 'API_KEY_NUM',
    'tags' => array('yii'), // You may also specify some Mailgun parameters
    'enableTracking' => true,
),

'log'=>array(
   'class'=>'CLogRouter',
   'routes'=>array(
       array(
          'class'=>'CMailGunLogRoute',
          'levels'=>'info',
          'filter'=>'CLogFilter',
          'emails'=>'recipient@domain.com',
          'sentFrom'=>'sender@domain.com',
          'subject'=>'Email Log File Message',
       ),
    ),
),


If you have any additional tips on how to improve this, please feel free to add them below.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top