Yii 1.1: smtp-mail

I created one SMTP mail extension using phpmailer class

Add the SMTP Details in main.php

Download the extension and add it into protected/extension folder. Now configure the smtp details in protected/config/main.php


Create Mailfunction

public function mailsend($to,$from,$subject,$message){
        $mail->SetFrom($from, 'From NAme');
        $mail->Subject    = $subject;
        $mail->AddAddress($to, "");
        if(!$mail->Send()) {
            echo "Mailer Error: " . $mail->ErrorInfo;
        }else {
            echo "Message sent!";

Total 9 comments

#19404 report it
lvsan at 2015/06/22 12:54am
how to hide the sender email address?


the code is work for me, but i would like to hide the sender email address from display.

Can anyone help me on it?


#18389 report it
eduardoarandah at 2014/10/22 10:55pm
IMPORTANT: Add $mail->ClearAddresses();

If you use the function in a loop, the addresses will accumulate one over another. The proper function is:

public function mailsend($to,$from,$subject,$message){
        $mail->SetFrom($from, 'From NAme');
        $mail->Subject    = $subject;
        $mail->AddAddress($to, "");
        if(!$mail->Send()) {
            echo "Mailer Error: " . $mail->ErrorInfo;
        }else {
            echo "Message sent!";
        $mail->ClearAddresses(); //clear addresses for next email sending
#17291 report it
Kostas Apazidis (KonApaz) at 2014/05/20 02:23am
Create Mailfunction where ?

A lot of members ask where we can put the function mailsend

So, there are two best practice

Either you can put this function in components/Controller.php and call it in an controller/action as



if you are use a module that its controllers not extend the components/Controller.php you can make the mailsend as a static function

public static function mailsend($to,$from,$subject,$message) ...

and call it as Controller::mailsend($to,$from,$subject,$message);

Best of all:

Make a globally class with static function in combonents like it described in this wiki


and call the function like this

SYii::mailsend($to,$from,$subject,$message); ..anywhere in controllers/extensions/modules/combonents/models/views

#16377 report it
sabila_ry at 2014/02/16 11:56pm
Thank you

I can send email from localhost WAMP using this without editing php.ini. Thank you @mbala :D And thank you @reptildarat for gmail config that you shared, i use that, terima kasih ^^

#13793 report it
sheraz.s at 2013/06/26 10:06am
I am unable to send HTML in email

Hi, can you please guide me how to send html in email via this extension. i have used $mail->IsHTML(true); but it is not sending htm. please guide me ho i can do this. code is given below.

$mail = Yii::app()->Smtpmail;
        $mail->SetFrom("from", 'from');
        //$recipient = yii::app()->Smtpmail->mail_recipient;
        $mail->Subject    = "Welcome to Karmora";       
        $message = '<img src='logo'><br>welcome to YII';
        $mail->AddAddress("email_to", "");
            return false;
            return true;
#13717 report it
samilo at 2013/06/19 10:05am
Gmail Smtp


Gmail smtp is :
    Gmail SMTP server address: smtp.gmail.com
    Gmail SMTP user name: Your full Gmail address (e.g. example@gmail.com)
    Gmail SMTP password: Your Gmail password
    Gmail SMTP port: 465
    Gmail SMTP TLS/SSL required: yes
#13643 report it
priyanka06 at 2013/06/13 08:06am
regarding file attachment

can anyone please tell me how to attach pdf file using this extension?

#13154 report it
reptildarat at 2013/05/08 09:47pm
Gmail Configuration

Just configure like this in your config/main

            'Password'=>'password here',
            'SMTPSecure' => 'tls',

And Don't forget to activate your php_openssl in PHP.ini

#12309 report it
mbala at 2013/03/13 03:03am

If you know How to handle mail using Gmail host, Please post here

Leave a comment

Please to leave your comment.

Create extension