[solved] Sending emails
#1
Posted 13 August 2010 - 05:20 AM
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?
#2
Posted 13 August 2010 - 06:06 AM
sermon, on 13 August 2010 - 05:20 AM, said:
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!!!!!!!
#3
Posted 13 August 2010 - 07:14 AM
sermon, on 13 August 2010 - 05:20 AM, said:
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
),
#5
Posted 25 January 2011 - 11:56 PM
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
#6
Posted 04 April 2011 - 04:41 PM
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
Stefanus, on 25 January 2011 - 11:56 PM, said:
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
#7
Posted 30 April 2012 - 05:28 PM
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?
#8
Posted 15 June 2012 - 11:31 AM
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?
#9
Posted 18 June 2012 - 04:33 AM
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!
#10
Posted 12 August 2012 - 02:37 PM
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.
#11
Posted 06 March 2013 - 03:48 AM
Is there a way to do 'this trick' without modify the extension code? ...
Ricordalo quando fai il debug
#12
Posted 06 March 2013 - 04:08 AM
<?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',
Ricordalo quando fai il debug

Help













