[solved] Sending emails

Hej Folks.

I have steh standart contact us example from yii. now i want, that it should work propably.

but i cant find out, where i had to pass an emailadress so that the contact us form send really an email to me.

could someone plz help me?

Hello, i had the same problem but i decided to use an extension that made it very easy to implement.

i used swiftmailer.

in brief download the swiftmailer extension then creat in the component folder a new class and use YII::import to import all the class required by the swiftmailer then i define methods to send my mails.

here is the code


class email{

protected &#036;name; 

protected &#036;From; 

protected &#036;to; 

protected &#036;subject;

protected &#036;body;

public function sendMail(&#036;to,&#036;from,&#036;subject,&#036;body){



	Yii::import('application.extensions.mail.vendors.swiftMailer.swift_init', true);






	&#036;message = Swift_Message::newInstance()






	&#036;transport = Swift_SmtpTransport::newInstance(&#036;host)




catch (Exception &#036;e){

		throw new Exception('Server Error'); 



	&#036;mailer = Swift_Mailer::newInstance(&#036;transport); 








} ?>

in your controller call the class


$mail = new email();



and that is all you need cheers!!!!!!!

This is a standard Yii contact form action:


	 * Displays the contact page


	public function actionContact()


		$model=new ContactForm;






				$headers="From: {$model->email}\r\nReply-To: {$model->email}";


				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');






In the code above we can see that it sends the email to Yii::app()->params[‘adminEmail’]. So you have to specify “adminEmail” param in your config like this:


                // this is used in contact page

                'adminEmail'=>'webmaster@example.com', <--- here goes your email


big thx to all. :)

Hi… I still got this error when i was trying this solution

include(Swift_DependencyContainer.php) [<a href=‘function.include’>function.include</a>]: failed to open stream: No such file or directory

Could anyone help me what is going on ?

Thanks a lot

Hi ! As far I understood it : In Swift 4.0.6, they use spl_autoload_register in a way that puts Swift’s autoloader at the end of the auto-loader chain…

Without changing Swift’s files, I came up with this (hacky?) solution :

Yii::import('ext.Mailer.vendors.Swift.lib.classes.Swift', true);


Yii::import('ext.Mailer.vendors.Swift.lib.swift_init', true);

or if you don’t mind editing Swift’s file you can edit lib/classes/Swift.php, and change the registration of the autoloader :

  public static function registerAutoload()


    spl_autoload_register(array('Swift', 'autoload'), false, true);


Hope this help,

See ya,


I am using swiftMailer version 4.1.7 with extension wkd-swiftmailer as a wrapper which is loaded in the components section of protected/config/main.php. Supposedly this version has made some changes to prevent conflicts with other autoloaders but I still see the warning:

include(Swift_DependencyContainer.php) [<a href=‘function.include’>function.include</a>]: failed to open stream: No such file or directory

I tried the following imports:

Yii::import('ext.swiftMailer.lib.classes.Swift', true);


Yii::import('ext.swiftMailer.lib.swift_init', true);

but got an error for duplicating the require statement found somewhere else.

Anyone have a suggestion for me?

i too am getting the following error:

include(Swift_DependencyContainer.php) [<a href=‘function.include’>function.include</a>]: failed to open stream: No such file or directory

but only on my production server (running php 5.2.5) and not on my development server (running php 5.3) leading me to suspect that it could be something to do with the version of php (namespaces being introduced in v 5.3).

can anyone confirm that this is the case - or am i way off the mark?

yes, it looks like this is an issue in php 5.2.5 (or anything pre 5.3)

i found a clue here:

but i didn’t want to get into the complications of mixing revisions, so i just manually imported every directory in the swiftMailer extension.

to save someone else the (minor) effort of copy-pasting, here’s the code for importing everything (for v. Swift-4.0.4 - other versions may have different directory structure)

Yii::import('ext.swiftMailer.lib.classes.Swift.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.ByteStream.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.CharacterReader.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.CharacterReaderFactory.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.CharacterStream.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.Encoder.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.Events.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.KeyCache.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.Mailer.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.Mime.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.Mime.ContentEncoder.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.Mime.HeaderEncoder.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.Mime.Headers.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.Plugins.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.Plugins.Decorator.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.Plugins.Loggers.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.Plugins.Pop.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.Plugins.Reporters.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.StreamFilters.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.Transport.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.Transport.Esmtp.*', true);

Yii::import('ext.swiftMailer.lib.classes.Swift.Transport.Esmtp.Auth.*', true);

a bit of a hack - but hope it helps!

maybe a little too late, but try this (protected/extensions/swiftMailer/SwiftMailer.php):

public function init() {





php 5.2.*, Yii 1.1.11, SwiftMailer 4.2.1

don’t know if this is the safest approach, perhaps someone could comment. :)

This works !

Is there a way to do ‘this trick’ without modify the extension code? …

I actually got it working creating an extension of extension ( :) )


Yii::import( "ext.swiftMailer.SwiftMailer" );

class Yii_SwiftMailer extends SwiftMailer {

    // Sovrascrittura dell'init per far funzionare l'ultima versione di Swift_Mailer con Yii

    // Può darsi che da PHP 5.3 in avanti questo problema non si presenti più

    public function init()



        require_once( Yii::getPathOfAlias('ext.swiftMailer.lib') . "/swift_required.php");




So I change in main.php


'class'     => 'ext.swiftMailer.SwiftMailer',

to this

'class'     => 'ext.<my_company_name>.Yii_SwiftMailer',