Hi, I am just start learn how to use Yii Framework for my current project. I am facing a problem to send an email to admin after I fill out all the details on the ‘Contact Form’. I have already change the ‘adminEmail’ to my email address from the protected/config/main.php.
public $name;
public $email;
public $subject;
public $body;
public $verifyCode;
public function rules()
{
return array(
// name, email, subject and body are required
array('name, email, subject, body', 'required'),
// email has to be a valid email address
array('email', 'email'),
// verifyCode needs to be entered correctly
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
);
}
public function attributeLabels()
{
return array(
'verifyCode'=>'Verification Code',
);
}
well I wanna help you. But I would not know unless there is an error everything seems to be okay in terms of syntax I don’t see anything wrong with logic either
// does it display the message in your view
Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
$this->refresh();
Hi, alirz23. Thank you so much for ur help. Ya, it does display message in my view. But im wondering why it does not send any email to my gmail mailbox since there is no error and everything seems okay. Did I miss out anything? or did I use any wrong port number??
<?php
return array(
'viewPath' => 'application.views.mail',
'layoutPath' => 'application.views.layouts',
'baseDirPath' => 'webroot.images.mail',
'savePath' => 'webroot.assets.mail',
'testMode' => false,
'layout' => 'mail',
'CharSet' => 'UTF-8',
'AltBody' => Yii::t('YiiMailer','You need an HTML capable viewer to read this message.'),
'language' => array(
'authenticate' => Yii::t('YiiMailer','SMTP Error: Could not authenticate.'),
'connect_host' => Yii::t('YiiMailer','SMTP Error: Could not connect to SMTP host.'),
'data_not_accepted' => Yii::t('YiiMailer','SMTP Error: Data not accepted.'),
'empty_message' => Yii::t('YiiMailer','Message body empty'),
'encoding' => Yii::t('YiiMailer','Unknown encoding: '),
'execute' => Yii::t('YiiMailer','Could not execute: '),
'file_access' => Yii::t('YiiMailer','Could not access file: '),
'file_open' => Yii::t('YiiMailer','File Error: Could not open file: '),
'from_failed' => Yii::t('YiiMailer','The following From address failed: '),
'instantiate' => Yii::t('YiiMailer','Could not instantiate mail function.'),
'invalid_address' => Yii::t('YiiMailer','Invalid address'),
'mailer_not_supported' => Yii::t('YiiMailer',' mailer is not supported.'),
'provide_address' => Yii::t('YiiMailer','You must provide at least one recipient email address.'),
'recipients_failed' => Yii::t('YiiMailer','SMTP Error: The following recipients failed: '),
'signing' => Yii::t('YiiMailer','Signing Error: '),
'smtp_connect_failed' => Yii::t('YiiMailer','SMTP Connect() failed.'),
'smtp_error' => Yii::t('YiiMailer','SMTP server error: '),
'variable_set' => Yii::t('YiiMailer','Cannot set or reset variable: ')
),
);
...
public function actionContact()
{
$model=new ContactForm;
if(isset($_POST['ContactForm']))
{
$model->attributes=$_POST['ContactForm'];
if($model->validate())
{
//use 'contact' view from views/mail
$mail = new YiiMailer('contact', array('message' => $model->body, 'name' => $model->name, 'description' => 'Contact form'));
//set properties
$mail->setFrom($model->email, $model->name);
$mail->setSubject($model->subject);
$mail->setTo(Yii::app()->params['adminEmail']);
//send
if ($mail->send()) {
Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
} else {
Yii::app()->user->setFlash('error','Error while sending email: '.$mail->getError());
}
$this->refresh();
}
}
$this->render('contact',array('model'=>$model));
}
class ContactForm extends CFormModel
{
public $name;
public $email;
public $subject;
public $body;
public $verifyCode;
/**
* Declares the validation rules.
*/
public function rules()
{
return array(
// name, email, subject and body are required
array('name, email, subject, body', 'required'),
// email has to be a valid email address
array('email', 'email'),
// verifyCode needs to be entered correctly
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
);
}
/**
* Declares customized attribute labels.
* If not declared here, an attribute would have a label that is
* the same as its name with the first letter in upper case.
*/
public function attributeLabels()
{
return array(
'verifyCode'=>'Verification Code',
);
}
}
However, it returns the error: ‘Undefined variable: model’. Please help me. Thanks in advance.
Ya, you are right! I just realized that a few minutes ago. Thanks for correcting me. I changed the ‘YiiMailer’ into ‘ContactForm’ but it returns another error which is 'ContactForm and its behaviors do not have a method or closure named “setFrom”. ’ And I do not understand the error.
public function __call($name,$parameters)
{
if($this->_m!==null)
{
foreach($this->_m as $object)
{
if($object->getEnabled() && method_exists($object,$name))
return call_user_func_array(array($object,$name),$parameters);
}
}
if(class_exists('Closure', false) && $this->canGetProperty($name) && $this->$name instanceof Closure)
return call_user_func_array($this->$name, $parameters);
throw new CException(Yii::t('yii','{class} and its behaviors do not have a method or closure named "{name}".',
array('{class}'=>get_class($this), '{name}'=>$name)));
}
Your mail sending code (Put in your view or controller as per the use) -
$to = 'demo@example.com'; //Email id, which you want to send an email
$from= 'your_email_id@gmail.com'; //Email id, provide your email id
$mail=Yii::app()->Smtpmail;
$mail->SetFrom($from, 'Yii Demo');
$mail->Subject = 'your email subject';
$mail->MsgHTML('Testing...');
$mail->AddAddress($to, "");
$mail->IsHTML(true);
$mail->Send();