Search code examples
yiiyii-extensionsyii-components

how to use mail function in yii


I am a newbie in Yii Framework and creating a first yii app. I am trying to send a mail in yii 1.1.14 but not understand how to use in yii.


Solution

  • There is an extension called yii mailer which is composed of PhpMailer.

    Here you can Download -- Yii-Mailer

    Copy YiiMailer folder to protected/extensions Then Add the below code to your imports in config/main.php

    'import'=>array(
            'ext.YiiMailer.YiiMailer',
            'ext.yii-mail.YiiMailMessage',
    

    Then you can simply create a function in your controller to send email like this.

    public static function SendEmail($emailContent){
        $mail = new YiiMailer();
        $mail->setFrom('info@example.com', 'Example Site Name');
        $mail->setTo('Info@example.com');
        $mail->setSubject('Example Site Name');
        $mail->setBody($emailContent);
        $mail->send();
        echo "Successfully send";
    }
    

    You can replace the setTo line with code below for getting admin email if you have specified.

    $mail->setTo(Yii::app()->params['adminEmail']);
    

    and setBody line with your own message or content.

    $mail->setBody('Simple message');
    

    More details can be found on how to use yii mailer here