Yii 1.1: multimailer

Yii extension for sending or saving emails with templates and attachments.
2 followers

What it does

This extension allows to send emails with the help of PHPMailer class or to store emails in database (to send them later using Java workers for example).

The last version

This is the last version of MultiMailer (excluding bug fixes).

This version does not include PHPMailer files because with the last critical issue being fixed I don't want to open your server to exploits with older versions. Please get the latest version of PHPMailer at https://github.com/PHPMailer/PHPMailer and place all the folders and files in MultiMailer/PHPMailer folder. I recommend switching to Yii 2.

Installation

Place the MultiMailer folder inside your Yii /protected/extensions directory. Modify the main.php config file. For example:

return array(
    'components' => array(
        'MultiMailer' => array(
            'class' => 'ext.MultiMailer.MultiMailer',
            'setFromAddress' => 'from@yourwebsite.com',
            'setFromName' => 'Your Website',
        ),
    ),
);

This sets MultiMailer to send email using PHP mail() function with sender 'Your Website from@yourwebsite.com'. You can find more configuration examples in Examples folder.

How to use it

$mailer = Yii::app()->MultiMailer->to('example@server.com', 'Recipient');
$mailer->subject('Example email subject');
$mailer->body('<h1>Hello</h1><p>This is test.<br>MultiMailer test.</p>');
 
if ($mailer->send()) {
    // success
}
else {
    // error
}

You can chain the methods (except attachment() and attachments()) like:

Yii::app()->MultiMailer->to('example@server.com', 'Recipient')->subject('Example email subject')->body('<h1>Hello</h1><p>This is test.<br>MultiMailer test.</p>')->send();

Special case: emails in database

You can use MultiMailer to save email in database instead of sending it immediately. Email will be prepared using the PHPMailer as well.

First the configuration:

return array(
    'components' => array(
        'MultiMailer' => array(
            'class' => 'ext.MultiMailer.MultiMailer',
            'setFromAddress' => 'from@yourwebsite.com',
            'setFromName' => 'Your Website',
            'setMethod' => 'DB',
            'setDbModel' => 'Email',
        ),
    ),
);

You need to prepare the database table for email storage with Active Record model class ('Email' in the example above). Default table columns are:

  • 'email'
  • 'name'
  • 'subject'
  • 'body'
  • 'alt'

See the documentation for information about adding or switching the columns off.

The usage in this case is the same as before but remember that method send() will not actually send the email but will save it in database.

List of quick-start extension methods

  • attachment() - add an attachment (option: attachments() - add the list of attachments)
  • bcc() - add a blind carbon copy recipient (options: bccs() - add the list of bcc recipients)
  • body() - set the email body text (option: set the list of prameters for template here and use template() to add template)
  • cc() - add a carbon copy recipient (option: ccs() - add the list of cc recipients)
  • send() - send the email
  • subject() - set the email subject
  • to() - add a recipient (option: tos() - add the list of recipients)

Requirements

Yii 1.1 or above.

Resources

Source code:

Added in version 1.6

  • PHPMailer updated to 5.2.14

Added in version 1.5

  • PHPMailer class is now extended with ProxyPHPMailer class.
  • Library files are included using Yii::import() now.
  • New property setPattern allows to set validation pattern for PHPMailer in case of problems with PCRE8 regex (default 'auto').
  • New public method getPhpmailer() allows to control PHPMailer directly.
  • New public method clearAllRecipients() allows to remove all recipients (including CC and BCC recipients).
  • Method _processBody() is using Yii::renderFile() instead of Yii::renderPartial() in case of command line action and properly throws exception instead of using setMultiError().

Added in version 1.4.1

  • PHPMailer updated to 5.2.10

Added in version 1.4

  • new methods for adding attachments and group lists of recipients
  • new parameter setLanguage - sets the language for PHPMailer errors with optional language data file. Default language is English (en).

Added in version 1.3

  • PHPMailer updated to 5.2.9

Added in version 1.2

  • additional method for altering DB model columns
  • messages are translated with Yii:t now (can be switched off)
  • DB model validation errors can be tracked now with getMultiError(true)

Added in version 1.1

  • Gmail method
  • Sendmail method
  • qmail method
  • POP before SMTP method
  • CC and BCC headers

Total 3 comments

#19359 report it
Yair at 2015/05/31 06:29pm
simple way for embeding images

One more thing worth mentioning:

You can embed images into your html, simple by using the image physical address.

For example:

// for PC
<img alt="alt text..." src="c:\\tmp\\Screenshot.png" />
 
//or for Unix
<img alt="alt text..." src="/home/yourdomain/public_html/images/Screenshot.png" />

PHPMailer will do the rest... :)

#19355 report it
Bizley at 2015/05/29 02:39am
Re: multimailer attachment notice

You are right. attachments() method returns boolean so you can check if file has been properly attached. In the example for email with attachments you can see how it works ( https://github.com/bizley-code/Yii-MultiMailer/blob/master/Examples/mail-with-attachment.php ).

#19354 report it
Yair at 2015/05/29 01:51am
multimailer attachment notice

To save someone few minutes...

attachments() returns boolean and NOT the usual phpmailer object, so you cannot use it like I tried:

$rc = $mailer
->subject($subject)
->body($templateVars)->template($templateName)
->to('example@server.com', 'Recipient')
->attachments($attachments)
->send();

It would give an error: Call to a member function send() on a non-object

The way to do it is:

$mailer
->subject($subject)
->body($templateVars)->template($templateName)
->to('example@server.com', 'Recipient');
 
$mailer->attachments($attachments);
 
$rc = $mailer->send();
attatchments parameters are: (from addAttachment() in class.phpmailer.php in PHPMailer-5.2.10)
     * @param string $path Path to the attachment.
     * @param string $name Overrides the attachment name.
     * @param string $encoding File encoding (see $Encoding).
     * @param string $type File extension (MIME) type.
     * @param string $disposition Disposition to use

Leave a comment

Please to leave your comment.

Create extension