Sending email by lithium php

I choose nice library li3_mailer delivered by @eLod for my #li3 app and sending email by gmail smtp server. Lets do it.

in console, check your libraries paths..

git submodule add https://github.com/eLod/li3_mailer libraries/li3_mailer;
git submodule add https://github.com/swiftmailer/swiftmailer.git libraries/swiftmailer
git submodule init
git submodule update

attach added libraries to app/config/bootstrap/libraries.php in `add some plugins section`

...
Libraries::add('li3_mailer');
Libraries::add('swiftmailer', array('prefix' => 'Swift_', 'bootstrap' => 'lib/swift_required.php'));

in connection.php (i think is good place)

use li3_mailer\net\mail\Delivery;

Delivery::config(array('default' => array(
'adapter' => 'Swift',
'transport' => 'smtp',
'from' => array('domain.com' => 'contact@domain.com'),
'username' => 'contact@domain.com', #its for business accounts config otherwise use username@gmail.com
'password' => 'googleuserpassword',
'host' => 'smtp.gmail.com',
'port' => 465,
'encryption' => 'ssl'
)));

prepare templates and views for html and text email in files /app/mails/layouts/default.{html,text}.php with

Hello
< ?php echo $this->content(); ?>
Big rgd. !

/app/mails/news.{html,text}.php

User data:
< ?php print_r($User); ?>
 

now its time to sending email put this code to the action in Controller

//use li3_mailer\action\Mailer;
//$User = Users::first...
$success = Mailer::deliver('news', ['to' => $email, 'subject' => 'news subject', 'data' => compact('User')]); //@return 1 if ok

thats all 🙂 mails templates should be working with g11n translation

glory area
– thanx for advice @rapzo
– http://www.lithium101.com/plugins/elod/li3_mailer
– http://swiftmailer.org