Monday, January 4, 2010

How do I use the email component?

Hello,

I am new to CakePHP and trying desperately to learn!

My most recent struggle is with the Email Component.

I have a contract. When I create the contract, I add a user. When I
save the new contract...for the first time, I want to send an email to
the user in that contract that allows them to click on a link back to
the contract, and then accept or reject the contract.

How do I send this email?

The more details I can get, answer-wise, the better. Everything I have
read out there is surprisingly confusing. Do I need to configure smtp
settings? How do I grab the user in the contract after it has been
saved and pass it and the link to the contract on to the email? How do
I know if the email has been sent, without going and checking my email
every single time?

Here is the code I have in my contracts_controller.php for the email
sending function: (A Contract belongsTo a User, and a User hasMany
Contracts.)

function _sendContractEmail($id) {
$this->Email->smtpOptions = array(
'port'=>'465',
'timeout'=>'30',
'host'=>'ssl://smtp.gmail.com',
'username'=>'username',
'password'=>'password'
);
$this->Email->delivery = 'smtp';
$User = $this->Contract->User->read(null,$id);
$this->Email->to = 'jeremiah@jeremiahotis.com';
$this->Email->subject = '';
$this->Email->replyTo = 'no-reply@goodvaluation.com';
$this->Email->from = 'Jeremiah Oits
<jeremiah@jeremiaotis.com>';
$this->Email->template = 'simple_message';
$this->Email->sendAs = 'html';
$this->set('User', $User);
$this->Email->_debug = true;
$this->Email->send('Test Email');
$this->redirect(array('controller'=>'contracts',
'action'=>'index'));
}

Here is the code I have in my contracts_controller.php add() function:

function add() {
if (!empty($this->data)) {
$this->Contract->create();
if ($this->Contract->save($this->data)) {
$this->Session->setFlash(__('The Contract has been
saved', true));
$this->_sendContractEmail($this->Contract->User->id);
} else {
$this->Session->setFlash(__('The Contract could not be
saved. Please, try again.', true));
}

And, this is at the top of my contracts_controller.php file after the
$name and $helpers:

var $components = array('Email');

I guess I should point out that I was having trouble with the
template, so to test it, I included the body of the email directly in
the send(), and I specified the to email address rather than using a
variable. Still...nothing! No error, no email.

Any help would be greatly appreciated! I really have no idea what I am
doing wrong!

Thanks,

Jeremiah

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
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?hl=en

No comments: