mintao/yii-mailer

v1.0.1 2013-01-05 00:00 UTC

README

This extension uses the feature rich Swiftmailer

  • Embeds automatically locally stored images, if included into html mail
  • Handling of file attachments
  • Encryption support
  • Mailserver authentication support
  • MIME compliant HTML/multipart emails
  • i18n Support
  • Template support

How to activate it in yii?

To enable the yii mailer extension, simply add this entry into the components section of your yii configuration file.

<?php
'components' => array(
    'mailer' => array(
        'class'           => 'ext.MntMailer.MntMailer',
        'swiftmailerPath' => 'application.library.swiftmailer',
        'returnPath'      => 'bounce@myapp.com',
        'fromEmail'       => 'noreply@myapp.com',
        'fromName'        => 'My App.com',
        'host'            => 'smtp.googlemail.com',
        'port'            => 587,
        'username'        => 'username@domain.com',
        'password'        => 'youWontGuessIt',
        'encryption'      => 'tls',
    ),
),
?>

Simple Mail Example

Yii::app()->mailer->attributes = array(
    'fromEmail' => 'webmaster@crayzapps.com',
    'to'        => 'new.user@example.org',
    'subject'   => 'Welcome to crayz apps!',
    'plainText' => 'Welcome, New user!',
    'htmlText'  => '<p>Welcome, <b>New User!</b></p>',
);
Yii::app()->mailer->send();
print_r(Yii::app()->mailer->getErrors());

How to use a template and dynamic content?

Well, that's simple, too. The template location is up to you, but in this case, we'll put it under protected/views/mailer.

Create the folder structure for mail tempaltes

mailer
├── layout
│   ├── html.php
│   └── plain.php
└── welcome
    ├── html.php
    └── plain.php
    

Add the template location to the mailer configuration file

'components' => array(
    'mailer' => array(
        …
        'templateBasePath' => 'application.views.mailer',
        … 
    ),
),

Content of the layout/html.php file

<!DOCTYPE HTML>
<html>
    <head>
      <title>Mail Template</title>
    </head>
    <body>
      <img src="/img/banner/128x128.png" alt="Logo">
      <hr>
      <?php echo $content ?>
      <hr>
      <p>FOOTER INFORMATION COMES HERE.</p>
    </body>
</html>

Content of the layout/plain.php file

<?php echo Html::encode(Yii::app()->name) ?>
=================================
<?php echo $content?>
=================================
FOOTER INFORMATION COMES HERE.

Content of the welcome/html.php file

<h1>Welcome, <?php echo CHtml::encode($user) ?>!</h1>
<p>This is a html welcome mail.</p>

Content of the welcome/plain.php file

WELCOME <?php echo $user ?>

This is the plain text version of the welcome mail.

And now, send the mail!

Yii::app()->mailer->attributes = array(
  'fromEmail' => 'florian.fackler@mintao.com',
  'to' => 'fred.userman@example.org',
  'subject' => 'Welcome to our glorious app',
  'template' => 'welcome',
  'placeholder' => array('user' => 'Fred'),
);
Yii::app()->mailer->send();
print_r(Yii::app()->mailer->getErrors());

How can I embedd an image into my mail?

Let's say, there is the logo of your company which should appear at the top of every email. You would create a HTML template e.g.

<table>
    <tr>
        <th><img src="/images/logo.png" alt="ACME - The IT Experts"></th>
    </tr>
    <tr>
        <td><?php echo $content ?></td>
    </tr>
    <tr>
        <td>FOOTER TEXT COMES HERE</td>
    </tr>
</table>

That's it! The Yii Mailer Extension will find the logo.png and embedd the image it into your email as "inline attachment". This way the images in your mails are displayed instantly without forcing the recipient to allow showing external assets.

Note: Right now this only works for templated mails.

Here are some smtp services

Enjoy!