Thursday, June 2, 2011

Trouble using email templates

Hey everybody, I'm setting up a contact form. I want to send 2 email
at once. One to contact person as confirmation and one to the person
handling the contact request.

I setup up 2 templates in the /app/view/layout/text folder
internal_message.ctp and default.ctp.

I'm receiving the mails but the template us the same in both cases.
And I'm sure I specified the right template.
This is the code I'm using:

function index(){
if(isset($this->data)) {
$contact = $this->Contact->create($this->data);

if($this->Contact->validates()){

$smtpOptions = array(
'port'=> Configure::read('Settings.mail_port'),
'timeout'=>
Configure::read('Settings.mail_timeout'),
'host' =>
Configure::read('Settings.mail_host'),
'username'=>
Configure::read('Settings.mail_username'),
'password'=>
Configure::read('Settings.mail_password'),
);

$this->_internalResponse($contact,$smtpOptions);

$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Please Correct Errors');
//$this->redirect('/contacts');
}
}
}

function _autoResponse($data,$smtpOptions)
{
$this->Email->reset();

$this->Email->smtpOptions = $smtpOptions;
$this->Email->delivery = 'smtp';
//$this->Email->delivery = 'debug';

$this->Email->to = $this->data['Contact']['name'].' <'.
$this->data['Contact']['email'].'>';
$this->Email->from = 'no-reply@prevaes.be';
$this->Email->subject = 'Prevaes contact betreft: '.
$this->data['Contact']['subject'];

$this->Email->template = 'default';
$this->Email->sendAs = 'text';

$this->set('Contact',$data);

if ($this->Email->send()) {
$this->Session->setFlash('Bedankt om contact met
ons op te nemen, </br> een kopie zal naar uw inbox gestuurd worden.');

//$this->redirect('/');
} else {
$this->Session->setFlash('Mail Not Sent');
//$this->set('smtp_errors', $this->Email-
>smtpError);
}
}

function _internalResponse($data,$smtpOptions)
{
$this->Email->reset();

$this->Email->smtpOptions = $smtpOptions;
$this->Email->delivery = 'smtp';

$this->Email->to = Configure::read('Settings.mail_username');
$this->Email->from = $this->data['Contact']['name'].'
<'.$this->data['Contact']['email'].'>';
$this->Email->subject = 'Prevaes internal contact: '.
$this->data['Contact']['subject'];

$this->Email->template = 'internal_message';
$this->Email->sendAs = 'text';


$this->set('Contact',$data);

if ($this->Email->send()) {
$this->_autoResponse($data, $smtpOptions);
} else {
$this->Session->setFlash('Mail Not Sent');
//$this->set('smtp_errors', $this->Email-
>smtpError);
}
}

I'm using 2 private method to create end send the 2 different mails.
As you can see in both cases I specified
$this->Email->template = 'default';
$this->Email->sendAs = 'text';
I know that sendAs isn't strictly necessary because it's default. But
I thought that could be the reason.
And I perform a $this->Email->reset(); to make sure the properties of
the email are reset.

Does any one know how to fix this or see an error in my code ?

Thanks a lot in advance.

Regards,

Niels

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: