Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bashkarev/yii-swiftmailer


https://github.com/bashkarev/yii-swiftmailer

mail php swiftmailer yii yii-framework

Last synced: 4 months ago
JSON representation

Awesome Lists containing this project

README

        

SwiftMailer Extension for Yii1, imported yii2-swiftmailer
===============================

This extension provides a [SwiftMailer](http://swiftmailer.org/) mail solution for [Yii framework 1](http://www.yiiframework.com).

Installation
------------

Either run

```
composer require bashkarev/yii-swiftmailer
```

or add

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

to the require section of your composer.json.

Configuration
-------------

Mail component configuration depends on the extension you have chosen.
In general your application configuration should look like:

```php
return [
//....
'components' => [
'mailer' => [
'class' => 'bashkarev\swiftmailer\swift\Mailer',
],
],
];
```
example SMTP:
```php
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' => ['[email protected]' => 'Example Name']
]
],
],
];

```

Debug panel
-----
usage panel [zhuravljov/yii2-debug](https://github.com/zhuravljov/yii2-debug)
```php
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:

```php
Yii::app()->mailer->compose()
->setFrom('[email protected]')
->setTo('[email protected]')
->setSubject('Message subject')
->setTextBody('Plain text content')
->setHtmlBody('HTML content')
->send();
```
You may also send several messages at once:

```php
$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.

Example mail view file layout:
/protected/mail/layouts/html.php
```php



.heading {...}
.list {...}
.footer {...}

= $content ?>

```

Example mail view file content:

/protected/mail/test.php
```php


User id:= $model->id ?>

```

```php
Yii::app()->mailer->compose('test', ['model'=>User::model()->findByPk(1)])
->setFrom('[email protected]')
->setTo('[email protected]')
->setSubject('Message subject')
->send();
```

File attachment
---------------

You can add attachments to message using methods `attach()` and `attachContent()`:

```php
$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:

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

Then inside the view file you can use the following code:

```php

```