Yii Framework Forum: [solved] Sending emails - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[solved] Sending emails Rate Topic: -----

#1 User is offline   sermon 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 83
  • Joined: 02-August 10
  • Location:Bielefeld

Posted 13 August 2010 - 05:20 AM

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?
0

#2 User is offline   binkabir 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 193
  • Joined: 25-July 10
  • Location:Abuja,Nigeria

Posted 13 August 2010 - 06:06 AM

View Postsermon, on 13 August 2010 - 05:20 AM, said:

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

<?php
class email{


protected $name;
protected $From;
protected $to;
protected $subject;
protected $body;

public function sendMail($to,$from,$subject,$body){
Yii::import('application.extensions.mail.vendors.swiftMailer.classes.Swift',true);
Yii::registerAutoloader(array('Swift','autoload'));
Yii::import('application.extensions.mail.vendors.swiftMailer.swift_init', true);

try{

$host='mail.yourcompany.com';

$message = Swift_Message::newInstance()
->setTo($to)
->setFrom($from)
->setSubject($subject)
->setBody($body);

$transport = Swift_SmtpTransport::newInstance($host)
->setUsername('email@company.con')
->setPassword('password');

}
catch (Exception $e){
throw new Exception('Server Error');
}

$mailer = Swift_Mailer::newInstance($transport);



($mailer->send($message));



}





} ?>

in your controller call the class
<?


$mail = new email();
$mail->sendMail($name,$From,$to,$subject,$body);
?>
and that is all you need cheers!!!!!!!
1

#3 User is offline   Equ 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 57
  • Joined: 27-November 09
  • Location:Russia, Moscow

Posted 13 August 2010 - 07:14 AM

View Postsermon, on 13 August 2010 - 05:20 AM, said:

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?



This is a standard Yii contact form action:

	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model=new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				$headers="From: {$model->email}\r\nReply-To: {$model->email}";
				mail(Yii::app()->params['adminEmail'],$model->subject,$model->body,$headers);
				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact',array('model'=>$model));
	}


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:

        'params'=>array(
                // this is used in contact page
                'adminEmail'=>'webmaster@example.com', <--- here goes your email
        ),

0

#4 User is offline   sermon 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 83
  • Joined: 02-August 10
  • Location:Bielefeld

Posted 16 August 2010 - 08:39 AM

big thx to all. :)
0

#5 User is offline   Stefanus 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 31-March 10

Posted 25 January 2011 - 11:56 PM

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
0

#6 User is offline   Taarish 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 04-April 11

Posted 04 April 2011 - 04:41 PM

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::registerAutoloader(array('Swift','autoload'));
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,
Taarish

View PostStefanus, on 25 January 2011 - 11:56 PM, said:

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

0

#7 User is offline   ivtdev 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 06-March 12

Posted 30 April 2012 - 05:28 PM

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::registerAutoloader(array('Swift','autoload'));
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?
0

#8 User is offline   atari_icon 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 25-January 12

Posted 15 June 2012 - 11:31 AM

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?
0

#9 User is offline   atari_icon 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 25-January 12

Posted 18 June 2012 - 04:33 AM

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

i found a clue here:
stackoverflow.com/questions/8435411/import-models-in-subdirectories-yii

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

#10 User is offline   spinicrus 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 24-July 12
  • Location:up :)

Posted 12 August 2012 - 02:37 PM

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

public function init() {
    spl_autoload_unregister(array('YiiBase','autoload'));
    require_once(dirname(__FILE__).'/lib/swift_required.php');
    spl_autoload_register(array('YiiBase','autoload'));
}


php 5.2.*, Yii 1.1.11, SwiftMailer 4.2.1

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

#11 User is offline   realtebo 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 03-October 12
  • Location:Ferrara

Posted 06 March 2013 - 03:48 AM

This works !

Is there a way to do 'this trick' without modify the extension code? ...
Il computer non ragiona, ma ha sempre ragione.
Ricordalo quando fai il debug
0

#12 User is offline   realtebo 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 03-October 12
  • Location:Ferrara

Posted 06 March 2013 - 04:08 AM

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

<?php

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()
    {
        spl_autoload_unregister(array('YiiBase','autoload'));
        require_once( Yii::getPathOfAlias('ext.swiftMailer.lib') . "/swift_required.php");
        spl_autoload_register(array('YiiBase','autoload'));
    }

}


So I change in main.php

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


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

Il computer non ragiona, ma ha sempre ragione.
Ricordalo quando fai il debug
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users