Yii 1.1: kemail

SMTP interaction class that avoids using mail()


Yii Application Component to interact with smtp servers without relying on PHP's mail or PEAR's Mail. Simply a wrapper for http://www.phpclasses.org/package/14-PHP-Sends-e-mail-messages-via-SMTP-protocol.html


Download from here Easiest way
Download from github https://github.com/mrkmg/KEmail Not easy, unless you are familiar with git

To Install

  1. Copy KEmail into approot/protected/extenstions

  2. Under "import", add:


  3. Under "components", add:

    'host_name'=>'smtp_server', //Hostname or IP of smtp server

Basic Usage

  1. Single recipient

  2. Multiple recipients
    $to = array(

Full API Documentation on GITHUB


The following is an outline of all the avaible options, and their default options

host_name="localhost"           Host name of smtp server
host_port=25                    Port of smtp server
ssl=false                       Force SSL
http_proxy_host_name=''         Set to enable using an http proxy to access smtp server
http_proxy_host_port=3128       Port of http proxy
socks_host_name=''              Set to enable using a socks proxy
socks_host_port=1080            Port of socks proxy
socks_version='5'               Version of socks proxy
start_tls=false                 Force `start_tls`
localhost='localhost'           Given hostname of client
direct_delivery=false           Skip smtp server and delevier directly to recipients smtp
timeout=10                      Time in seconds to timeout for all smtp connections
data_timeout=0                  Time in seconds to timeout for data transfer to SMTP server, if 0 uses timeout
debug=false                     Output Debug information to browser
html_debug=true                 Format Debug information as html
pop3_auth_host=''               Set to authenticate to a pop3 server
user=""                         Username for smtp authentication
realm=""                        Realm for smtp authentication
password=""                     Password for smtp authentication
workstation=""                  Workstation for smtp authentication
authentication_mechanism=""     Force a specific smtp authentication mechanism ('LOGIN','PLAIN','CRAM-MD5','NTLM')
enable_queue=false              Enabled the queue
autocreate_db_table=true        Check for and create if needed the queue table in the database
queue_table_name='kemail_queue' Name of the queue database table

Advanced Usage

You can also customize headers

For example, to send an HTML email, do the following

$headers = array(
    'MIME-Version: 1.0',
    'Content-type: text/html; charset=iso-8859-1'

Queue Support

In order to use the built in queue feature, you must have a database accessable via Yii::app()->db;

To Enable to queue, set the following options

    'host_name'=>'smtp_server',  //Hostname or IP of smtp server
    'enable_queue'=>true, //This enabled to queue

autocreate_db_table should be set to false once the table has been created.

To queue an email, do the following

$headers = array(
    'MIME-Version: 1.0',
    'Content-type: text/html; charset=iso-8859-1'
$priority = 2; //INT between 0 and 9. Higher the number, the sooner it will be sent.

Then, maybe via a cron script or by random chance,

Yii::app()->email->processQueue(5); //Process 5 emails out of the queue
Yii::app()->email->processQueue(8,true) //Process 8 emails from the queue, and ignore priorities

TODO/Upcoming Features

  • Improve mail queue
    • Allow for use of a variety of storage methods, flatfile, sqlite.
    • Implement yiic access to process mail queue
    • Ability to get total number of items in queue
  • Error detection and reporting

Total 11 comments

#15990 report it
FOX Creation at 2014/01/07 10:30am
Additional changes

Some extra changes made:

In the queue function, the return function was not working correctly. Furthermore, a Strict error was shown when the function json_encode was called when binding the parameters, as you may only pass a variable.

$to     = json_encode($to);
$additional_headers = json_encode($additional_headers);
$exec = $command->execute();
return ($exec > 0);

In the send function I added the functionality to send including a personal name, then the receiver will also see a name, instead of only a mail address.

    //from 0 = name, 1 = email
    return $this->smtp_object->SendMessage(
                    "From: $from[0] <{$from[1]}>",
                    "To: $to_h",
                    "Subject: ".$subject,
                    "Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z")
    return $this->smtp_object->SendMessage(
                "From: $from",
                "To: $to_h",
                "Subject: ".$subject,
                "Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z")
#15987 report it
FOX Creation at 2014/01/07 09:51am
How to: display errors and add attachments

Error handling I created the function in KEmail.php file :

public function getErrors(){
   return $this->smtp_object->error;

This allows you to to call


Attachments: I've created a model Mail which sets all content and in which I call the send function:"

function setContentWithAttachment($files){
  $this->mailSubject = "subject";
  $this->mailContent = "message";
  $separator = md5(time());
  $eol = PHP_EOL;
  $this->mailHeaders = array(   "Reply-To: ". $this->mailFrom,
    "MIME-Version: 1.0",
        "Content-Type: multipart/mixed; boundary=\"".$separator."\""
  // message        
  $body = "--".$separator.$eol;
  $body .= "Content-Type: text/html; charset=\"utf-8\"".$eol;
  $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
  $body .= $this->mailContent .$eol;
  // attachment
  $num = count($files);
  foreach($files as $i => $file){
    $filename = $file['filename'] .'.pdf';
    $data = ""; /* e.g. fread or HTML2PDF output to String */
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
    $body .= "Content-Transfer-Encoding: base64".$eol;
    $body .= "Content-Disposition: attachment; filename=\"".$filename."\"".$eol.$eol;   
    $body .= chunk_split(base64_encode($data))).$eol;
    $body .= "--".$separator. (($i == $num) ? "--" : "") .$eol;
  $this->mailContent = $body;

Then you can easily post $this->mailContent as $body and $this->mailHeaders as $additional_headers. Hope this can help you.

#14732 report it
xNicox at 2013/09/06 09:07am

hi, how to add attachments ? I only know that is a feature os MIME and not simply SMTP protocol....

Best Regards

#13319 report it
mrkmg at 2013/05/22 09:32am
re: Still being updated?

Yes, I am still working on this project. Development is slow though. If you would like to help, feel free to clone the github project and send a pull request.

#13096 report it
ktwbc at 2013/05/04 10:18am
Still being updated?

Is this extension still being updated? Error messages are desperately needed to log why an email failed other than a simple Boolean true/false.

#12341 report it
mrkmg at 2013/03/14 03:27pm
In Reply To: CC and BCC

You can add CC and BCC recipients via the headers.

For Example:

$headers = array(
    'MIME-Version: 1.0',
    'Content-type: text/html; charset=iso-8859-1',
    'Cc: person@email.address',
    'Bcc: hidden@email.address'
#10853 report it
teguh11 at 2012/11/27 11:59pm
CC and BCC

How to use CC and BCC??

#8479 report it
mrkmg at 2012/06/06 11:58am
Reply to GMail as SMTP

The following setup works for GMAIL's smtp server

#8478 report it
mrkmg at 2012/06/06 11:50am
In Reply to "Can't get it to work"

I sent you a PM on the forums. I have tried this on 3 different installations on different machines, and all of them worked perfect. My assumption is based on your error that you placed something in the config file in the wrong location.

#8465 report it
beltrajp at 2012/06/05 10:43pm
Can't get it work

I did the steps outlined.

I get this error:

Property "CWebApplication.email" is not defined.

#8464 report it
Daniel at 2012/06/05 10:41pm
GMail as SMTP


Any example on using GMail as SMTP?



Leave a comment

Please to leave your comment.

Create extension
  • Yii Version: 1.1
  • License: New BSD License
  • Developed by: mrkmg
  • Category: Mail
  • Votes: +6
  • Downloaded: 1,813 times
  • Created on: Jun 5, 2012
  • Last updated: Sep 10, 2012
  • Tags: email, smtp, mail