Revision #2 has been created by MikeT on Dec 9, 2013, 10:10:45 PM with the memo:
Included php highlighting in code blocks
« previous (#1) next (#3) »
Changes
Title
unchanged
Using Apostle.io to send formatted, trackable emails from Yii applications
Category
unchanged
How-tos
Yii version
unchanged
Tags
unchanged
email, mail
Content
changed
[...]
Start by installing using Composer, not using Composer leads to dependency hell since Apostle uses Guzzle which uses bits of Symfony..... The composer.json entry is on Apostle's Github page at [https://github.com/apostle/apostle-php](https://github.com/apostle/apostle-php). Place this in your application/protected directory and run Composer.
You then end up with a vendor directory (if you didn't have one already) containing apostle/apostle-php/src, guzzle/guzzle/src and symfony/event-dispatcher/Symfony. In your protected/config/main.php include aliases for each of their namespaces:
```php
Yii::setPathOfAlias('Apostle', dirname(__FILE__).'/../vendor/apostle/apostle-php/src/Apostle');
Yii::setPathOfAlias('Guzzle', dirname(__FILE__).'/../vendor/guzzle/guzzle/src/Guzzle');
Yii::setPathOfAlias('Symfony', dirname(__FILE__).'/../vendor/symfony/event-dispatcher/Symfony');
```
also include an import line for the main Apostle module:
```php
// autoloading model and component classes
'import' => array(
.......[...]
'application.vendor.apostle.apostle-php.src.*',
.........
),
```
Then, in whatever controller you're sending the email from, call the Apostle Module and set the domain key (only do this once), which you will get when you sign up to Apostle (it's free for up to 5,000 emails a month!).
```php
.........
Apostle::setup("1234567890abcdef1234567890abcdef");
```
Create an Apostle mail instance using a template that you have already set up using the Apostle site:
```php
........
$mail = new Apostle\Mail('welcome');
```
and add the email and any other values you want to use in your email
```php
.........
$mail->email = $model->email;
// Add any values our template needs
$mail->firstname = $profile->firstname;
```
Finally, send it to Apostle....
```php
.....
// Send to Apostle.io
$mail->deliver();
```
Simple as that! No need to log emails sent, no need to set up hard coded email templates, no need to have mailing facilities set up on your application server. So long as you have the aliases set correctly for the namespaces used by Apostle, it works a treat.
There's more documentation at [apostle.io/developers](http://apostle.io/developers) and Mal, the founder of Apostle.io, provides very timely and friendly support!