Giter Site home page Giter Site logo

yiimailer's Introduction

YiiMailer

Yii extension for sending emails with layouts using PHPMailer

Features

  • Based on latest PHPMailer (version 5.2.6 bundled)
  • Supports Yii layouts and translation
  • Supports web and console applications
  • Send full HTML emails with embedded images
  • Work with views like you usually do in Yii
  • Use test mode to save emails instead of sending them (useful when you don't have mail server installed locally)

Installation

  1. Copy YiiMailer folder to protected/extensions
  2. Add 'ext.YiiMailer.YiiMailer' line to your imports in main and/or console yii config
  3. Copy mail.php config file to protected/config or add configuration array in 'params' under the key 'YiiMailer'
  4. Create email layout file mail.php in protected/views/layouts/ (default path, can be changed in config)
  5. Create all the views you want to use in protected/views/mail/ (default path, can be changed in config)
  6. Put all images you want to embed in emails in images/mail/ (default path, can be changed in config)

Usage

Instantiate YiiMailer in your controller or console command and pass view and data array:

$mail = new YiiMailer('contact', array('message' => 'Message to send', 'name' => 'John Doe', 'description' => 'Contact form'));

or

$mail = new YiiMailer();
$mail->setView('contact');
$mail->setData(array('message' => 'Message to send', 'name' => 'John Doe', 'description' => 'Contact form'));

Layout is automatically set from config but you may override it with

$mail->setLayout('layoutName');

Set the properties:

$mail->setFrom('[email protected]', 'John Doe');
$mail->setTo(Yii::app()->params['adminEmail']);
$mail->setSubject('Mail subject');

You may use all PHPMailer properties you would usually use.

And finally send email(s):

if ($mail->send()) {
	Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
} else {
	Yii::app()->user->setFlash('error','Error while sending email: '.$mail->getError());
}

Sending simple messages

You can send email without both the layout and view by using:

$mail = new YiiMailer();
//$mail->clearLayout();//if layout is already set in config
$mail->setFrom('[email protected]', 'John Doe');
$mail->setTo(Yii::app()->params['adminEmail']);
$mail->setSubject('Mail subject');
$mail->setBody('Simple message');
$mail->send();

Alternatively, you may also send email message with layout but without specific view (set layout and set body) or with view but without layout (clear layout and set view).

Setting addresses

When using methods for setting addresses (setTo(), setCc(), setBcc(), setReplyTo()) any of the following is valid for arguments:

$mail->setTo('[email protected]');
$mail->setTo(array('[email protected]','[email protected]'));
$mail->setTo(array('[email protected]'=>'John Doe','[email protected]'));

Sending attachments

You may send one or more attachments using setAttachemnt() method:

$mail->setAttachment('something.pdf');
$mail->setAttachment(array('something.pdf','something_else.pdf','another.doc'));
$mail->setAttachment(array('something.pdf'=>'Some file','something_else.pdf'=>'Another file'));

Test mode

When working locally without mail server installed, it may be useful to save emails as files instead of trying to send them and getting errors in the process. To use test mode, you must specify path to directory where you want to save your emails and set 'testMode' property to 'true' in your config:

	'savePath' => 'webroot.assets.mail',
	'testMode' => true,

Emails are saved as .eml files and you can use software like Mozilla Thunderbird to open them.

Examples

Two examples included: one for standard contact form in yii web app and the other one for yii console app.

yiimailer's People

Contributors

vernes avatar

Watchers

James Cloos 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.