well, I need it working today, so here is what I'm just doing:

1) extension of the email component to be suited for multipart
alternative

2) master layout for a multipart alternative email, with one
"renderElement" for each of the templates, the html and the txt
version.

3) the html and the txt version as templates in cakephp style created
as elements in views/elements (I think they fit in there, but do they?)

4) "set"ting the vars needed in the controller and calling the Email
component to render and send, following (but extending) the wiki
sample.

As you see below, I'm still working on part 3 and 4, so this is no
finished code to drop in somewhere, not yet.

If you see me doing something stupid, or un-cakePHPish, please alert
me.

Also any ideas, hints or comments are welcome!

----------------------------------------------

Here's the part one, the email component to be suited for multipart
alternative:

*************** BEGIN **************
<?php

class MultipartAlternativeEmailComponent
{
    var $tpl;
    var $to             = null;      // has to be set before sending
    var $from           = null;      // has to be set before sending
    var $subject        = "no subject";
    var $cc             = null;      // optional
    var $bcc            = null;      // optional
    var $fifthparameter = "-f";   // sometimes needed to force
mailserver to use $from as sender
    var $xmailer        = "X-Mailer: cakePHP multipart Email component
v20060630\n";
    var $controller;


    function init($params)
    {
        if (!empty($params['tpl']))      $this->tpl     =
$params['tpl'];
        if (!empty($params['to']))       $this->to      =
$params['to'];
        if (!empty($params['from ']))    $this->from    = $params['from
'];
        if (!empty($params['cc']))       $this->cc      =
$params['cc'];
        if (!empty($params['bcc']))      $this->bcc     =
$params['bcc'];
        if (!empty($params['subject']))  $this->subject =
$params['subject'];

        // create a boundary
        $this->boundary = "UNIVERSALX-BOUNDARY-" . md5(uniqid (rand()))
;
        $this->boundary = str_replace("=", "-", $this->boundary);

        $this->controller->set('boundary', $this->boundary );

        if ( empty($this->to) || empty($this->from) ||
empty($this->tpl))
        {
            return false;
        }
        else
        {
            return true;
        }

    }

    function message()
    {
        ob_start();

$this->controller->render($this->tpl,'multipart_alternative_email');
        $mail = ob_get_clean();
        return $mail;
    }


    function send()
    {
        // make sure we have all what we need
        if ( empty($this->from) ||empty($this->to)  )
        {
            return false;   // missing sender and / or receipient
        }

        // create a boundary
        $boundary = "UNIVERSALX-BOUNDARY-" . md5(uniqid (rand())) ;
        $boundary = str_replace("=", "-", $boundary);


        // TBD: check for valid email format

        $headers  = "From: $this->from\n" . "Return-Path:
$this->from\n" . $this->xmailer ;
        $headers .= "MIME-Version: 1.0\nContent-Type:
multipart/alternative; boundary=$boundary\n\n";

        if (!empty($this->cc)  )  $headers  .= "CC:$this->cc\n" ;
        if (!empty($this->bcc) )  $headers  .= "BCC:$this->bcc\n"  ;

        $fifthparameter .= $this->from ;

        $success = mail($this->to, $this->subject, $this->message(),
$headers, $fifthparameter);
        return $success;
    }

}
?>
*************** END **************

Here's the part 2, the master layout for a multipart alternative email:

*************** BEGIN **************
This is an Email in the modern MIME format. If you are reading this
text, your email reader does not understand how to properly display
MIME multipart messages. You might want to consider upgrading your
email reader.

--<?php echo $boundary; ?>
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: 8bit

<?php echo $this->renderElement( 'emailTxt' ); ?>

--<?php echo $boundary; ?>
Content-Type: text/html; charset="utf-8"
Content-Disposition: inline
Content-Transfer-Encoding: 8bit

<?php echo $this->renderElement( 'emailHtml' ); ?>

--<?php echo $boundary; ?>

*************** END **************

Part 3, the 2 templates itself, are whatever you need in cakePHP
format, and

Part 4, the code in the controller, is something like this:

*************** BEGIN **************
    function sendBookingEmail($some_data_for_email)
    {
        $this->set('var_to_email',$some_data_for_email);

        $this->MultipartAlternativeEmail->controller = $this;

        $emailparams = array(
                            'tpl'     => 'email_to_send',
                            'to'      => '[EMAIL PROTECTED]',
                            'from'    => '[EMAIL PROTECTED]',
                            'cc'      => '[EMAIL PROTECTED]',
                            'bcc'     => '[EMAIL PROTECTED]',
                            'subject' => 'the subject',
                            );


        $this->MultipartAlternativeEmail->init( $emailparams );

        // render the text part into a variable
        // set text part as $title_for_layout

        // render the html part into a variable
        // set html part as $content_for_layout

        /***********************************************/

        if($this->MultipartAlternativeEmail->send())
        {
            $this->set('message','email was sent');
        }
        else
        {
            $this->set('message','email could not be sent');
        }
    
    }
*************** END **************


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---

Reply via email to