2017 © Pedro Peláez
 

library yii-swiftmailer

The SwiftMailer integration for the Yii framework

image

bashkarev/yii-swiftmailer

The SwiftMailer integration for the Yii framework

  • Thursday, July 14, 2016
  • by bashkarev
  • Repository
  • 1 Watchers
  • 3 Stars
  • 152 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 13 % Grown

The README.md

SwiftMailer Extension for Yii1, imported yii2-swiftmailer

This extension provides a SwiftMailer mail solution for Yii framework 1., (*1)

Installation

Either run, (*2)

composer require bashkarev/yii-swiftmailer

or add, (*3)

"bashkarev/yii-swiftmailer": "~1.0.0"

to the require section of your composer.json., (*4)

Configuration

Mail component configuration depends on the extension you have chosen. In general your application configuration should look like:, (*5)

return [
    //....
    'components' => [
        'mailer' => [
            'class' => 'bashkarev\swiftmailer\swift\Mailer',
        ],
    ],
];

example SMTP:, (*6)

return [
    //....
    'components' => [
        'mailer' => [
            'class' => 'bashkarev\swiftmailer\swift\Mailer',
            //'viewPath' => 'application.mail' //default path to views
            'transport' => [
                'host' => 'smtp.example.ru.',
                'username' => 'username',
                'password' => 'password',
                'port' => '465',
                'encryption' => 'ssl',
            ],
            'messageConfig' => [
                'from' => ['example@example.ru' => 'Example Name']
            ]
        ],
    ],
];

Debug panel

usage panel zhuravljov/yii2-debug, (*7)

return [
    //....
    'components' => [
        'debug' => [
            'class' => 'application.vendor.zhuravljov.yii2-debug.Yii2Debug',
            'enabled' => YII_DEBUG,
            'panels' =>[
                'mail' => [
                    'class' => 'bashkarev\swiftmailer\debug\MailPanel'
                ]
            ],
        ],
    ],
];

Basic usage

Once the 'mailer' component is configured, you can use the following code to send an email message:, (*8)

Yii::app()->mailer->compose()
    ->setFrom('from@domain.com')
    ->setTo('to@domain.com')
    ->setSubject('Message subject')
    ->setTextBody('Plain text content')
    ->setHtmlBody('<b>HTML content</b>')
    ->send();

You may also send several messages at once:, (*9)

$messages = [];
foreach ($users as $user) {
    $messages[] = Yii::app()->mailer->compose()
        // ...
        ->setTo($user->email);
}
Yii::$app->mailer->sendMultiple($messages);

Composing mail content

Yii allows composition of the actual mail messages content via special view files. By default these files should be located at 'application.mail' path., (*10)

Example mail view file layout: /protected/mail/layouts/html.php, (*11)



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=<?= Yii::app()->charset ?>" />
    <style type="text/css">
        .heading {...}
        .list {...}
        .footer {...}
    </style>
</head>
<body>
    <?= $content ?>
    <div class="footer">With kind regards, <?= Yii::app()->name ?> team</div>
</body>
</html>

Example mail view file content:, (*12)

/protected/mail/test.php, (*13)





User id:= $model->id ?>
Yii::app()->mailer->compose('test', ['model'=>User::model()->findByPk(1)])
    ->setFrom('from@domain.com')
    ->setTo('to@domain.com')
    ->setSubject('Message subject')
    ->send();

File attachment

You can add attachments to message using methods attach() and attachContent():, (*14)

$message = Yii::app()->mailer->compose();

// Attach file from local file system:
$message->attach('/path/to/source/file.pdf');

// Create attachment on-the-fly
$message->attachContent('Attachment content', ['fileName' => 'attach.txt', 'contentType' => 'text/plain']);

Embedding images

You can embed images into the message content using embed() method. This method returns the attachment id, which should be then used at 'img' tag. This method is easy to use when composing message content via view file:, (*15)

Yii::app()->mailer->compose('embed-email', ['imageFileName' => '/path/to/image.jpg'])
    // ...
    ->send();

Then inside the view file you can use the following code:, (*16)

<img src="<?= $message->embed($imageFileName); ?>">

The Versions

14/07 2016

dev-master

9999999-dev

The SwiftMailer integration for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

by Dmitriy Bashkarev

mail email swiftmailer mailer swift yii1

14/07 2016

1.0.0

1.0.0.0

The SwiftMailer integration for the Yii framework

  Sources   Download

BSD-3-Clause

The Requires

 

by Dmitriy Bashkarev

mail email swiftmailer mailer swift yii1