Code Monkey home page Code Monkey logo

yii-swiftmailer's Introduction

SwiftMailer Extension for Yii1, imported yii2-swiftmailer

This extension provides a SwiftMailer mail solution for Yii framework 1.

Installation

Either run

composer require bashkarev/yii-swiftmailer

or add

"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:

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

example SMTP:

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

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:

Yii::app()->mailer->compose()
    ->setFrom('[email protected]')
    ->setTo('[email protected]')
    ->setSubject('Message subject')
    ->setTextBody('Plain text content')
    ->setHtmlBody('<b>HTML content</b>')
    ->send();

You may also send several messages at once:

$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
/* @var $this \bashkarev\swiftmailer\View */
/* @var $message \bashkarev\swiftmailer\MessageInterface */
/* @var $content string main view render result */
?>

<!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:

/protected/mail/test.php

<?php
/* @var $this \bashkarev\swiftmailer\View */
/* @var $message \bashkarev\swiftmailer\MessageInterface */
?>

<div class="mail-test">
    User id:<?= $model->id ?>
</div>
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():

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

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

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

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

yii-swiftmailer's People

Contributors

bashkarev avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.