mail

SwiftMailer wrapper
115 followers

This is an emailing extension that wraps SwiftMailer. This extension also allows you to create emails from view files. For more information about SwiftMailer can do, see http://swiftmailer.org

Resources

Requirements

  • Yii 1.1.x

Documentation

Please see the phpdocs (it is in fact well-documented). First read through the main phpdoc on the Mail.php file and then the Message.php file (both in the root directory).

Quick example to catch your interest

This is an advanced example. It is not required that you use view files to generate the emails.

$message = new YiiMailMessage;
$message->view = 'registrationFollowup';
 
//userModel is passed to the view
$message->setBody(array('userModel'=>$userModel), 'text/html');
 
 
$message->addTo($userModel->email);
$message->from = Yii::app()->params['adminEmail'];
Yii::app()->mail->send($message);

Change Log

See Google Code hosting page

Matt Kantor has been so gracious as to provide an update, including:

  • Removed the "debug" option and associated behavior. In its place are the "logging" and "dryRun" options. Logging uses Yii::log() (you can set up a LogRoute to show flashes for the old behavior).
  • Changed class names to be less likely to cause conflicts (requested in several bug reports).
  • Made the extension usable from CConsoleApplication.
  • Plenty of formatting and documentation fixes.

Total 20 comments

#12450 report it
SomethingWicked at 2013/03/21 09:22am
Embed image

Using the view you can easily show images. THe only thing that comes to my mind is that probably you have a problem with the src of the image, which has to be an absolute url.

Just guessing.

#12448 report it
staticblue at 2013/03/21 09:03am
Thanks for the extension !

Thanks for the extension !

I too, would like to know how to embed images in the e-mail. Right now, when using a view, the images used in the view are not displayed in the e-mail. Do you have a guide on how to do this ?

Thanks, Jonathan

#12432 report it
Scott Deagan at 2013/03/20 08:08am
Adding attachments, embedding images

How do you:

a) Add an attachment (or multiple attachments). b) Embed an image within the email.

#12350 report it
Cleverson at 2013/03/15 08:45am
How can I send an attachment?

I've trying this:

$uploadedFile = CUploadedFile::getInstance($model,'anexo'); $msg->attach($uploadedFile);

But doesn't work and give no errors... Just a blank page...

#12234 report it
MrJ at 2013/03/08 12:13pm
Setting up a connector to Mircosoft Exchange

This post helped me set up a trusted mail connector to MS Exchange: http://blogs.technet.com/b/sbs/archive/2008/09/18/how-to-configure-trusted-smtp-relay-in-exchange-on-sbs-2008.aspx

#11419 report it
TomaszKane at 2013/01/13 06:11am
Gmail smtp config
'mail' => array(
    'class' => 'ext.yii-mail.YiiMail',
    'transportType' => 'smtp',
    'transportOptions' => array(
        'host' => 'smtp.gmail.com',
        'username' => 'XXXX@gmail.com',
        'password' => 'XXXX',
        'port' => '465',
        'encryption'=>'tls',
    ),
    'viewPath' => 'application.views.mail',
    'logging' => true,
    'dryRun' => false
),

works

#10893 report it
teguh11 at 2012/11/30 02:50am
found how to setFrom

just set like this

$message = new YiiMailMessage;
$message->getHeaders()->addMailboxHeader('From');

and then you can set from like this

$message->setFrom('xxx@gmail.com');

you can read this

#10854 report it
teguh11 at 2012/11/28 12:29am
use setFrom / setSender

how to use setFrom / setSender?? because no effect for me.

#10670 report it
zdenekca at 2012/11/13 11:07am
@Renaud

I believe that you can't set 'from' to be anything you like otherwise you could impersonate organizations (and people)?

Why don't you setup noreply@ account and send emails from this account. I see this being done by everyone else.

cheers

#10669 report it
Renaud at 2012/11/13 08:10am
@zdenekca

Thanks zdenekca for your answer But actually what I want to do is set the sender as an email address that does not exist (like noreply@example.com) From what you're telling me it seems not possible, is it ?

#10664 report it
zdenekca at 2012/11/12 08:39pm
using setFrom

If you want to use $message->setFrom($email) you need to remove username (password) from the main.php and instead set it manually in your code:

Yii::app()->mail->getTransport()->setUsername('admin@example.com');
Yii::app()->mail->getTransport()->setPassword('adminpassword...');

    ....
    $message->setFrom('admin@example.com');

I believe this is because you cannot have sender from one email address while you are actually sending the email from a different email account.

If your sender is always 'admin' then just set it in the main.php file. Otherwise use the method above.

#10662 report it
Vicente Russo at 2012/11/12 06:19pm
@Renaud

Try:

$message->from = "admin@example.com";
#10660 report it
Renaud at 2012/11/12 06:00pm
From field cannot be set

$message->setFrom("admin@example.com"); has no effect for me. All the other options are fine (subject, body...) but the "from" is always the email address used in the username of main.php. Any idea why ?

#10449 report it
fleuryc at 2012/10/29 12:56pm
Gmail SMTP error

Hi!

How to configure this ext with Gmail?

I tried :

'mail' => array(
    'class' => 'YiiMail',
    'transportType' => 'smtp',
    'transportOptions' => array(
        'host'=>'smtp.gmail.com',
        'username'=>'********',
        'password'=>'********',
        'port'=>'587',
        'encryption'=>'tls'
    ),
    'viewPath' => 'application.views.mail',
    'logging' => true,
    'dryRun' => false
),

I get :

fsockopen() [<a href='function.fsockopen'>function.fsockopen</a>]: SSL operation failed with code 1. OpenSSL Error messages:
error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number

I also tried :

'mail' => array(
    'class' => 'YiiMail',
    'transportType' => 'smtp',
    'transportOptions' => array(
        'host'=>'smtp.gmail.com',
        'username'=>'********',
        'password'=>'********',
        'port'=>'465',
        'encryption'=>'ssl'
    ),
    'viewPath' => 'application.views.mail',
    'logging' => true,
    'dryRun' => false
),

wich gives this :

fsockopen() [<a href='function.fsockopen'>function.fsockopen</a>]: unable to connect to ssl://smtp.gmail.com:465 (Connection timed out)

Help, please?

Cheers!

#9941 report it
Wiseon3 at 2012/09/24 09:26am
About Subject character encoding

@jdm instead of modifying the setSubject($subject) function, simply add the following code at the end of registerScripts() function in YiiMail.php

// set default preferrences
        Swift::init(function () {
            Swift_DependencyContainer::getInstance()
                ->register('mime.qpheaderencoder')
                ->asAliasOf('mime.base64headerencoder');
 
            Swift_Preferences::getInstance()->setCharset(Yii::app()->charset);
        });

What this does is set correct encoding (i assume you have charset defined in main config) and encodes the subject automatically (the same way you did).

#9940 report it
Wiseon3 at 2012/09/24 08:37am
Fix sendSimple() function

To be able to use the sendSimple() function replace line 181 in YiiMail.php:

else return $this->getMailer()->send($message);

with

else return $this->getMailer()->send($message->message);
#9468 report it
bennouna at 2012/08/14 07:32am
About Subject character encoding

@jedi-m

'=?utf-8?B?'.base64_encode($subject).'?='

is not only an ugly solution, it is THE solution :) at least with mail() when you have to use it if you have accented / non-latin characters.

The explanation is here. Quote:

_subject_

Subject of the email to be sent.

Caution

Subject must satisfy » RFC 2047.

#9341 report it
codatavern at 2012/08/05 05:36am
File Translation

In order to use file translation for views you should include this line at YiiMailMessage.php:

$viewPath = Yii::getPathOfAlias(Yii::app()->mail->viewPath . '.' . $this->view) . '.php';
$viewPath = Yii::app()->findLocalizedFile($viewPath); // New line
$body = $controller->renderInternal($viewPath, array_merge($body, array('mail' => $this)), true);
#9155 report it
Jimlam at 2012/07/24 12:02pm
Great extension

First of all, I would like to thank you for this extension.

I would like to know if there is a way to configure it so as to start my defaul mail application with the mail so that I may view it in my mail application and do any modification there before sending. This would be so nice.

Thanks for your reply beforehand

#9141 report it
yiqing95 at 2012/07/23 11:28pm
about cc, bcc

@ploaiza

the YiiMailMessage wrapper the Swift_message instance . and you can pass any method to the underlying swift_message obj from yiimailmessage obj :

$msg = new YiiMailMessage();
..
$msg->addCc('xx@xx.com','jhon');
$msg->setBcc(array('xx@xx.com','jj@jj.com'=>'jjName'));
..
Yii::app()->mail->send($msg);

the usage is same to Swift_message

Leave a comment

Please to leave your comment.

Create extension
Downloads
No downloadable files yet