Why would a variable work in the Create action but not the Update? Why would a working variable that hadn’t been changed just stop working? I have been over and over the code, I have searched for answers … I really could use some help, Please!
I was Creating and Updating, all of the emails were working, perfectly! Then I changed from registration required to using the captcha feature. I find it odd that the problem is with a mailer variable which didn’t get changed and even odder … it is only an error for the Update action, even though the same variable still works for Create.
The only thing I had to change for the mailer was where the email address for the visitor came from, ($model->email rather than $user->email).
PHP Error
Undefined variable: message
Controller.php (218)
$mailer->Body = $message; // line in actionUpdate
First; Controller Update code causing error - the working Create code is posted below for comparison. Any insight into the problem would be greatly appreciated.
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Referral']))
{
$model->attributes=$_POST['Referral'];
if($model->save())
$message = strtr ('Updated Referral Submitted
Changes have been made to the referral database.
A confirmation has been sent to the visitor email address.
ID#: {id}
Date/Time: {date}
Updated by: {byFName} {byLName}
Referral Name: {leadFName} {leadLName}
Referral Phone: {call}
Comments: {message}', array(
'{id}' => $model->id,
'{date}' => $model->date,
'{byFName}' => $model->customerFirstName,
'{byLName}' => $model->customerLastName,
'{leadFName}' => $model->firstName,
'{leadLName}'=> $model->lastName,
'{call}' => $model->phone,
'{message}' => $model->comments));
$title = strtr ('Updated Referral #{id} by {byFName} {byLName}',array(
'{id}' => $model->id,
'{byFName}' => $model->customerFirstName,
'{byLName}' => $model->customerLastName,
));
$mailer = Yii::createComponent('application.extensions.mailer.EMailer');
$mailer->Host = 'localhost';
$mailer->IsSMTP();
$mailer->From = Yii::app()->params['adminEmail'];
$mailer->AddReplyTo('No-Reply@mysite.net');
$mailer->AddAddress(Yii::app()->params['adminEmail']);
$mailer->AddCC(Yii::app()->params['adminOtherEmail']);
$mailer->FromName = 'Update Referral';
$mailer->CharSet = 'UTF-8';
$mailer->Subject = $title;
$mailer->Body = $message; // <------------------THIS IS THE UNDEFINED VARIABLE ERROR
$mailer->Send();
//send thank-you/copy of update to visitor
$message = strtr ('Thank-you {byFName} {byLName}, your updated referral submission at {siteAddress} has been received.
ID#: {id}
Submitted: {date}
Referral Name: {leadFName} {leadLName}
Referral Phone: {call}
Comments: {message}
Kindest Regards,
{siteAdmin}', array(
'{byFName}' => $model->customerFirstName,
'{byLName}' => $model->customerLastName,
'{siteAddress}' => 'http://mysite.net',
'{siteAdmin}' => Yii::app()->params['siteAdmin'],
'{id}' => $model->id,
'{date}' => $model->date,
'{leadFName}' => $model->firstName,
'{leadLName}'=> $model->lastName,
'{call}' => $model->phone,
'{message}' => $model->comments));
$mailer = Yii::createComponent('application.extensions.mailer.EMailer');
$mailer->Host = 'localhost';
$mailer->IsSMTP();
$mailer->From = Yii::app()->params['adminEmail'];
$mailer->AddReplyTo('No-Reply@mysite.net');
$mailer->AddAddress($model->email); // <-------------------THIS IS WHAT GOT CHANGED
$mailer->FromName = 'Site Admin';
$mailer->CharSet = 'UTF-8';
$mailer->Subject = 'Updated Referral Submission Received';
$mailer->Body = $message;
$mailer->Send();
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
Controller Create Works
public function actionCreate()
{
$model=new Referral;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Referral']))
{
$model->attributes=$_POST['Referral'];
if($model->save())
$message = strtr ('New Referral Submitted
A thank-you email has been sent for supplying the following information:
ID#: {id}
Submitted by: {byFName} {byLName}
{date}
Referral Name: {leadFName} {leadLName}
Referral Phone: {call}
Comments: {message}', array(
'{id}' => $model->id,
'{date}' => $model->date,
'{byFName}' => $model->customerFirstName,
'{byLName}' => $model->customerLastName,
'{leadFName}' => $model->firstName,
'{leadLName}'=> $model->lastName,
'{call}' => $model->phone,
'{message}' => $model->comments));
$title = strtr ('New Referral #{id} by {byFName} {byLName}',array(
'{id}' => $model->id,
'{byFName}' => $model->customerFirstName,
'{byLName}' => $model->customerLastName,
));
$mailer = Yii::createComponent('application.extensions.mailer.EMailer');
$mailer->Host = 'localhost';
$mailer->IsSMTP();
$mailer->From = Yii::app()->params['adminEmail'];
$mailer->AddReplyTo('No-Reply@mysite.net');
$mailer->AddAddress(Yii::app()->params['adminEmail']);
$mailer->AddCC(Yii::app()->params['adminOtherEmail']);
$mailer->FromName = 'New Referral';
$mailer->CharSet = 'UTF-8';
$mailer->Subject = $title;
$mailer->Body = $message; // <------------------- THIS WORKS and is set the same as Update
$mailer->Send();
//send thank you to visitor
$message = strtr ('Thank-you {byFName} {byLName}, your referral submission has been received at {siteAddress}.
ID#: {id}
Submitted: {date}
Referral Name: {leadFName} {leadLName}
Referral Phone: {call}
Comments: {message}
Kindest Regards,
{siteAdmin}', array(
'{byFName}' => $model->customerFirstName,
'{byLName}' => $model->customerLastName,
'{siteAddress}' => 'http://mysite.net',
'{siteAdmin}' => Yii::app()->params['siteAdmin'],
'{id}' => $model->id,
'{date}' => $model->date,
'{leadFName}' => $model->firstName,
'{leadLName}'=> $model->lastName,
'{call}' => $model->phone,
'{message}' => $model->comments));
$mailer = Yii::createComponent('application.extensions.mailer.EMailer');
$mailer->Host = 'localhost';
$mailer->IsSMTP();
$mailer->From = Yii::app()->params['adminEmail'];
$mailer->AddReplyTo('No-Reply@mysit.net');
$mailer->AddAddress($model->email);
$mailer->FromName = 'Site Admin';
$mailer->CharSet = 'UTF-8';
$mailer->Subject = 'New Referral Submission Received';
$mailer->Body = $message; // <------------------------ THIS WORKS and is set the same for Update
$mailer->Send();
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
Sorry to post so much code, …