[SOLVED] CEmailLogRouter

When occur an error in my application, if I was configured the CEmailLogRouter it will send to me automaticly or need I to configure anything to send when the error occur?

My conguration in /protected/config/main.php


'log'          => array(

            'class'  => 'CLogRouter',

            'routes' => array(

                array(

                    'class'  => 'CFileLogRoute',

                    'levels' => 'trace, info',

                ),

                array(

                    'class'      => 'CEmailLogRoute',

                    'levels'     => 'error', 

                    'emails'     => '<nome>@<domain>.com',

                    ),


            ),

        ),

It should work (I am using similiar configuration and it does). just bear in mind that CEmailLogRoute uses mail() function to send email, so your system (php, smtp service, etc) must be capable to properly handle them.

But when can I to configure all the other things, I said like SMTP, UserAccount, Password … etc?

PHP’s mail() function does not support authentication, so you cannot use it to send emails using GMail’s SMTP servers.

If you want to log Yii errors to an email account, Yii uses mail() by default, so you have to override this. Here’s how:

Download and install PHP Mailer from http://phpmailer.worxware.com/. Put the PHP files in your protected/components folder

Create a new file in your protected/components folder called CPhpMailerLogRoute.php, with the following code:


class CPhpMailerLogRoute extends CEmailLogRoute

{

    protected function sendEmail($email, $subject, $message)

    {

        $mail = new phpmailer();

        $mail->IsSMTP();

        $mail->Host = "smtp.gmail.com";

        $mail->SMTPAuth = true;

        $mail->SMTPSecure = "tls";

        $mail->Port = 587;

        $mail->Username = "your gmail username";

        $mail->Password = "your gmail password"; //best to keep this in your config file

        $mail->Subject = $subject;

        $mail->Body = $message;

        $mail->addAddress($email);

        $mail->send();

    }

}

and then, in your configuration file [protected/config/main.php], in the ‘components’=>‘log’=>‘routes’ section, add the following:

array(‘class’=>‘CPhpMailerLogRoute’, ‘levels’=>‘error’, ‘emails’=>‘user@example.com’)

There you have it. Now you can send Yii errors via your GMail account.

from: here

adlersd,

Thank you! I solved like this.


<?php

/**

 * CPHPMailerLogRoute class file.

 *

 * This file does the configuration necessary with

 * user's preferences and sends the email.

 * PHPMailerLogRoute could be used by 4 methods:

 * 'SMTP', 'SENDMAIL', 'MAIL and 'QMAIL'. All the

 * methods needs to be configured on computer.

 * Example:

 *  config => array (

 *          'SMTP',

 *          'Host' => "your.host.com",

 *          'SMTPAuth' => true,

 *          'SMTPSecure' => "ssl",

 *          'Host' => "smtp.<local>.com",

 *          'Port' => <port>,

 *          'Username' => "<user>@<domain>.com",

 *          'Password' => "<password>",

 *          'Charset' => "UTF-8",

 *  )

 *

 * @author    Léo Willian Kölln <leo@opasoft.com.br>

 * @author    Giovanna Garcia Basilio <giovanna@opasoft.com.br>

 * @copyright 2012 Opasoft Soluções Ltda.

 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD Licence

 */


require_once (dirname(__FILE__) . '/class.phpmailer.php');


/**

 * CPHPMailerLogRoute sends selected log messages to email addresses using the PHPMailer system.

 *

 * @property array  $emails   List of destination email addresses.

 * @property string $subject  Email subject.

 * @property string $sentFrom Send from address of the email.

 * @property array  $headers  Additional headers to use when sending an email.

 * @property array  $config   Configurations for the send mechanism of PHPMailer.

 *

 * @author    Léo Willian Kölln <leo@opasoft.com.br>

 * @author    Giovanna Garcia Basilio <giovanna@opasoft.com.br>

 */

class CPHPMailerLogRoute extends CEmailLogRoute

{

    /**

     * @var array list of configurations to use when sending an email.

     */

    private $_config = array();


    /**

     * Sends an email.

     *

     * @param string $email   single email address

     * @param string $subject email subject

     * @param string $message email content

     *

     * @return void

     */

    protected function sendEmail($email, $subject, $message)

    {

        $configuration = $this->getConfig();


        $sendMethod = $configuration[0];

        unset($configuration[0]);


        $mailer = new PHPMailer();


        switch ($sendMethod) {

        case "SMTP":

            $mailer->IsSMTP();

            break;

        case "SENDMAIL":

            $mailer->IsSendmail();

            break;

        case "QMAIL":

            $mailer->IsQmail();

            break;

        case "MAIL":

        default:

            $mailer->IsMail();

        }


        foreach ($configuration as $attribute => $value) {

            $mailer->$attribute = $value;

        }


        $mailer->SetFrom($this->getSentFrom());

        $mailer->AddAddress($email);

        $mailer->Subject = $subject;

        $mailer->Body = $message;


        //  TODO Add support for custom headers


        $mailer->Send();

    }


    /**

     * Get the configurations

     *

     * @return array configurations for the send mechanism.

     */

    public function getConfig()

    {

        return $this->_config;

    }

 

    /**

     * Set all configurations inserted

     *

     * @param mixed $value list of configurations to the PHPMailer send mechanism.

     *

     * @return void

     */

    public function setConfig($value)

    {

        if (is_array($value)) {

            $this->_config = $value;

        } else {

            $this->_config = array($value);

        }

    }

}



And at the app/protected/config/main.php:


'components'        => array(

            'log'          => array(

                'class'  => 'CLogRouter',

                'routes' => array(

                    array(

                        'class'  => 'CFileLogRoute',

                        'levels' => 'error, warning',

                    ),

                    array(

                        'class'      => 'CPHPMailerLogRoute',

                        'levels'     => 'error, warning',

                        'emails'     => array('<name>@<domain>.com.br'),

                        'categories' => 'exception.*, system.*',

                        'sentFrom'   => '<name>@<domain>.com.br',

                        'subject'    => 'Error',

                        'config'     => array(

                            'SMTP',

                            'Host'       => "mail.<domain>.com.br",

                            'SMTPAuth'   => true, // enable SMTP authentication

                            'SMTPSecure' => "ssl", // sets the prefix to the server

                             //   Attention: Remember to enable the SSL module in PHP

                            'Host'       => "smtp.gmail.com", // sets GMAIL as the SMTP server

                            'Port'       => <port>, // set the SMTP port for the GMAIL server

                            'Username'   => "<name>@<domain>.com", // GMAIL username

                            'Password'   => "<password>", // GMAIL password

                            'CharSet'    => "UTF-8",

                        )

                    ),

                ),

            ),

        )

I tried to create a new extension, but I recieved this notification:

[b]

[size="3"]Error[/size]

[color="#FF0000"]Sorry, you are too new to add an extension to the repository. Please try posting it in our forum first.[/color]

[/b]

Following the files in .tgz: 3104

phpmailerlogroute.tar.gz

// Giovanna Garcia