Here's how I have deployed email using the objects that CakePHP
provides.

In one application I have a screen where people can register their
interest on a site. When the new registration is added, I send them a
confirmation email. This controller is called 'registrations'. In the
registrations controller, I add the email component:

var $components = array('Email');

At the appropriate point (after the add is complete) I call a custom
function:

$this->emailConfirm($this->data['Registration']['first_name'], $this-
>data['Registration']['last_name'], $this->data['Registration']
['email']);

The emailConfirm function looks like this:

        function emailConfirm($first_name, $last_name, $email) {
                $to = $first_name . ' '. $last_name . ' <' . $email . '>';
                $this->Email->reset();
                $this->Email->from = 'Yadda Yadda <[email protected]>';
                $this->Email->to = $to;
                $this->Email->cc = array('Yadda Yadda 
<[email protected]>');
                $this->Email->bcc = array('Yadda Yadda 
<[email protected]>');
                $this->Email->sendAs = 'html';
                $this->Email->template = 'new_registration';
                $this->Email->subject = 'Thanks for registering';
                $this->set('first_name', $first_name);
                return $this->Email->send();
        }

I then have a folder called 'email' in my views/elements folder. This
contains two sub folders, 'html' and 'text'. Within the html folder I
have a template called 'new_registration.ctp'. This relates to the
template I am calling in my emailConfirm function. $this->Email-
>sendAs = 'html'; specifies I am sending an HTML email, so Cake looks
in the elements/email/html folder for a template that matches the
value set by $this->Email->template = 'xxx'; - in my case xxx =
'new_registration'.

The new_registration.ctp file can be as complex or simple as you
choose. In my case I have this:

<h3>Thanks for registering with xxxx.</h3>
<p>Hi there <?php echo $first_name;?></p>
<p>Thanks for taking the time to register your interest in our new
product, xxxx.</p>
<p>We'll be back in touch occasionally to bring you up to date with
how we are getting on.</p>
<h4>Privacy</h4>
<p>Please be assured that your details will not be used for any
purpose other than contacting you and acting as a log in to the
registration system.</p>

<p>Thanks again.</p>

You'll see that it calls a $first_name variable, which is set by the
line $this->set('first_name', $first_name); in my emailConfirm
function. You can set and call as many variable as you need in this
way to make the email truly customisable. I guess you could also set
and loop through array variables if you wanted to produce a simple
table - say an order confirmation.

The template is essentially an element that is presented inside a
layout. You therefore need to create a folder called 'email' in your
views/layouts folder, and within the email folder two sub folders
called 'html' and 'text' - just as you did for the templates. Within
each of the subfolders, create a default.ctp file, which is the layout
that will receive and wrap the email element. My default.ctp file in
the views/layouts/email/html folder is very simple:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">

<html>
        <head>
                <title><?php echo $title_for_layout;?></title>
        </head>

        <body>
                <?php echo $content_for_layout;?>
        </body>
</html>

The element, as you'd expect, is placed into the $content_for_layout
placeholder.

I have only used html emails thus far. The text folders I mention
above are for sending plain text emails. They are more basic versions
of the html files. For example, the default.ctp file in views/layouts/
email/text folder is simply this:

<?php echo $content_for_layout;?>

That's about it. You now have an email function that populates an
element with variables that is wrapped inside a layout. You can call
this same function sequentially passing in different variable values
if you want to loop through a list of emails to be sent. You'll notice
the line $this->Email->reset(); which makes sure that any previous
values sent to earlier emails are cleared before the next one is sent.

Hope this helps.

On Dec 1, 11:18 am, Vidya N <[email protected]> wrote:
> Hi All,
>
> Hi, I am cakePHP learner.. I want to integrate Email functionality through
> cakePHP.
>
> If anybody knows.. pleaes help me out...
>
> Thanks,
> Vidya

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 [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to