KEmail ¶
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 ¶
Download from here Easiest way
Download from github https://github.com/mrkmg/KEmail Not easy, unless you are familiar with git
To Install ¶
Copy KEmail into approot/protected/extenstions
Under "import", add:
application.extensions.KEmail.KEmail
Under "components", add:
'email'=>array(
'class'=>'KEmail', 'host_name'=>'smtp_server', //Hostname or IP of smtp server
),
Basic Usage ¶
Single recipient
Yii::app()->email->send('from@email.address','to@email.address','Subject','Body');Multiple recipients
$to = array(
'user1@email.address',
'user2@emai.address',
);
Yii::app()->email->send('from@email.address',$to,'Subject','Body');
Full API Documentation on GITHUB
Configuration ¶
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'
);
Yii::app()->email->send('from@email.address','to@email.address','Subject','<html><head><title>Subject</title></head><body>BODY</body></html>',$headers);
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
'email'=>array(
'class'=>'KEmail',
'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.
Yii::app()->email->queue('from@email.address','to@email.address','Subject','<html><head><title>Subject</title></head><body>BODY</body></html>',$headers,$priority);
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
GMail as SMTP
Hi,
Any example on using GMail as SMTP?
Cheers,
Daniel
Can't get it work
I did the steps outlined.
I get this error:
Property "CWebApplication.email" is not defined.
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.
Reply to GMail as SMTP
The following setup works for GMAIL's smtp server
'email'=>array( 'class'=>'KEmail', 'host_name'=>'smtp.gmail.com', 'user'=>'user@gmail.com', 'password'=>'password', 'host_port'=>465, 'ssl'=>'true', ),
CC and BCC
How to use CC and BCC??
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' ); Yii::app()->email->send('from@email.address','to@email.address','Subject','<html><head><title>Subject</title></head><body>BODY</body></html>',$headers);
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.
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.
Attachments?
hi, how to add attachments ?
I only know that is a feature os MIME and not simply SMTP protocol....
Best Regards
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.
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); .... $command->bindParam(':additional_headers',$additional_headers,PDO::PARAM_STR); ... $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.
... if(is_array($from)){ //from 0 = name, 1 = email return $this->smtp_object->SendMessage( $from[1], $to_f, array_merge(array( "From: $from[0] <{$from[1]}>", "To: $to_h", "Subject: ".$subject, "Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z") ),$additional_headers), $body); }else{ return $this->smtp_object->SendMessage( $from, $to_f, array_merge(array( "From: $from", "To: $to_h", "Subject: ".$subject, "Date: ".strftime("%a, %d %b %Y %H:%M:%S %Z") ),$additional_headers), $body); }
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.