Yii 1.1: smtp-mail

I created one SMTP mail extension using phpmailer class
11 followers

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

'components'=>array(
        'Smtpmail'=>array(
            'class'=>'application.extensions.smtpmail.PHPMailer',
            'Host'=>"mail.yourdomain.com",
            'Username'=>'test@yourdomain.com',
            'Password'=>'test',
            'Mailer'=>'smtp',
            'Port'=>26,
            'SMTPAuth'=>true, 
        ),
),

Create Mailfunction

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

Total 7 comments

#17291 report it
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

$this->mailsend($to,$from,$subject,$message);

OR

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

http://www.yiiframework.com/wiki/688/shortcut-usefull-access-roles-methods-rbac

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->MsgHTML($message);
        $mail->AddAddress("email_to", "");
 
        if(!$mail->Send()){
            return false;
        }
        else{
 
            return true;
        }
#13717 report it
samilo at 2013/06/19 10:05am
Gmail Smtp

@mbala

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

'Smtpmail'=>array(
            'class'=>'application.extensions.smtpmail.PHPMailer',
            'Host'=>"smtp.gmail.com",
            'Username'=>'your@gmail.com',
            'Password'=>'password here',
            'Mailer'=>'smtp',
            'Port'=>587,
            'SMTPAuth'=>true,
            'SMTPSecure' => 'tls',
        ),

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

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

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

Leave a comment

Please to leave your comment.

Create extension
Downloads