Contact Form- Send Mail To Email Address Stored In Db Table

Hi Guys!

First I wanna say hello to everyone, 'cause this is my first post here :)

I’ve already created a model for DB table. Now I created a custom form, with who I want to get some data for a custom query (get email address for particular ‘addid’ which is primary key stored in email column) . I wanted to send mail by using contact form to the related email address for the ‘addid’ in the table.

for this, I created contactpublisher.php in the view of model.

my model is Add controller is AddController view is add/contactpublisher.php

view code contactpublisher.php




<?php

/* @var $this SiteController */

/* @var $model ContactForm */

/* @var $form TbActiveForm */


$this->pageTitle=Yii::app()->name . ' - Contact Add Publisher';

$this->breadcrumbs=array(

	'Contact',

);

?>


<h2>Contact Add Publisher</h2>


<?php if(Yii::app()->user->hasFlash('contact')): ?>


    <?php $this->widget('bootstrap.widgets.TbAlert', array(

        'alerts'=>array('contact'),

    )); ?>


<?php else: ?>


<p>

If you have business inquiries, please fill out the following form to contact the publisher of this add.

</p>


<div class="form">


<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(

	'id'=>'contact-form',

    'type'=>'horizontal',

	'enableClientValidation'=>true,

	'clientOptions'=>array(

		'validateOnSubmit'=>true,

	),

)); ?>


<?php echo "<br>"; ?>


	<?php echo $form->errorSummary($model); ?>


    <?php echo $form->textFieldRow($model,'name'); ?>


    

<?php echo $form->textFieldRow($model,'email'); ?>

    <?php echo $form->textFieldRow($model,'subject',array('size'=>60,'maxlength'=>128)); ?>


    <?php echo $form->textAreaRow($model,'body',array('rows'=>4, 'class'=>'span5')); ?>


	


	<div class="form-actions">

		<?php $this->widget('bootstrap.widgets.TbButton',array(

            'buttonType'=>'submit',

            'type'=>'primary',

			'size'=>'large',

            'label'=>'Submit',

        )); ?>

	</div>


<?php $this->endWidget(); ?>


</div><!-- form -->


<?php endif; ?>

I put the link for contactpublisher form in the view of add:- view.php as




<?php

$this->menu=array(

array('label'=>'Contact Add Owner', 'url'=>array('contactpublisher', 'id'=>$model->addid)),

);

?>



my question is how to find the email address saved in table for the particular addid, I already tried this by using


$mailid=Add::model()->findByPk($addid);

but this is not working at all.

maybe someone can help me out with a small snippet :)

cheers and thanks to everyone in advance!!

Can you show your code from controller action?

Do you get some error?

controller code-




       class AddController extends Controller

       {

          public function actionContactpublisher()

         {

          $model=new ContactForm;

	

	  $mailid=Add::model()->findByPk(addid);

	

		if(isset($_POST['ContactForm']))

		{

			$model->attributes=$_POST['ContactForm'];

			

			if($model->validate())

			{

				$name='=?UTF-8?B?'.base64_encode($model->name).'?=';

				$subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';

				$headers="From: $name <{$model->email}>\r\n".

					"Reply-To: {$model->email}\r\n".

					"MIME-Version: 1.0\r\n".

					"Content-type: text/plain; charset=UTF-8";


				mail($mailid->addname,$subject,$model->body,$headers);

				Yii::app()->user->setFlash('contact','Mail is sent to the add publisher, Please wait publisher will reply soon... ');

				$this->refresh();

			}

		}

		$this->render('contactpublisher',array('model'=>$model));

}

 


}




no any error… i think problem is that addid value is not passed to the contactpublisher form from the view page of add model

add/view.php code-


<?php

/* @var $this AddController */

/* @var $model Add */


$this->breadcrumbs=array(

	'Adds'=>array('index'),

	$model->addid,

);


$this->menu=array(

	array('label'=>'Contact Add Owner', 'url'=>array('contactpublisher', 'id'=>$model->addid)),

);

?>


<h1>View Add #<?php echo $model->addid; ?></h1>


<?php $this->widget('bootstrap.widgets.TbDetailView', array(

	'data'=>$model,

	'type'=>'condensed',

	

	'attributes'=>array(

		'username',

		'addname',

		'description',

		'address',

		'mobile',

		'email',

		'price',

	

		

	),

)); 


?>

How to pass addid value to the contactpublisher form??

Add it to render();


render($this->render('contactpublisher',array('model'=>$model, 'addid'=>$wahtever)));

like this??

yes