<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-13327499</id><updated>2011-07-29T02:33:49.709-07:00</updated><title type='text'>Byte me ...</title><subtitle type='html'>Irreverent but hopefully informative thoughts on PHP, Ruby, Perl and development in general from someone who started out a long time ago in a galaxy far away ...</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://phazeforward.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/13327499/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://phazeforward.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>SenorGeek</name><uri>http://www.blogger.com/profile/01797272215878620138</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-13327499.post-1869264112772168763</id><published>2009-03-26T18:38:00.000-07:00</published><updated>2009-03-27T08:14:15.346-07:00</updated><title type='text'>Emailing from CakePHP 1.2.x</title><content type='html'>I've recently started working on a project in CakePHP and I wanted to share something that came out of it ...&lt;br /&gt;&lt;br /&gt;Sending nice emails from any website is always fun, as we all know PHP has some &lt;span style="font-weight: bold; font-style: italic;"&gt;very&lt;/span&gt; basic mail functionality that everyone has extended in their own way. I was trying to add email functionality to a site that I am developing and when I googled for anything to help I found this &lt;a href="http://bakery.cakephp.org/articles/view/94/"&gt;post&lt;/a&gt; from Alex McFayden that had a way to integrate PHPMailer into CakePHP as a component for any controller. Unfortunately it was written in 2006 for a previous version, obviously some changes were needed, so here are the new integration methods.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Get PHPMailer&lt;/h4&gt;  &lt;ol&gt;&lt;li&gt;Get PHPMailer &lt;a href="http://phpmailer.sourceforge.net/"&gt;http://phpmailer.sourceforge.net/&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Unpack it into app/vendors/phpmailer/ , so you'll have /vendors/phpmailer/class.phpmailer.php etc.etc.&lt;/li&gt;&lt;/ol&gt;  &lt;h4&gt;Create views and layouts&lt;/h4&gt; &lt;ol&gt;&lt;li&gt;Create two views, default_html.thtml and default_text.thtml and place them in app/views/your_controller/email/&lt;/li&gt;&lt;li&gt;Create a layout for the HTML part of the email, call it app/views/layouts/email.ctp &lt;/li&gt;&lt;/ol&gt;  &lt;h4&gt;Create component&lt;/h4&gt;  &lt;ol&gt;&lt;li&gt;Create new component email. Paste the following code into app/controllers/components/email.php&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;pre&gt;&amp;lt;?php&lt;br /&gt;/**&lt;br /&gt;* This is a component to send email from CakePHP using PHPMailer&lt;br /&gt;* @link http://bakery.cakephp.org/articles/view/94&lt;br /&gt;* @see http://bakery.cakephp.org/articles/view/94&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;class EmailComponent&lt;br /&gt;{&lt;br /&gt;/**&lt;br /&gt;* Send email using SMTP Auth by default.&lt;br /&gt;*/&lt;br /&gt;var $from = 'some_email@goes.here.com';&lt;br /&gt;var $fromName = "Displayed Name";&lt;br /&gt;var $sitePrefix = '[MySite]';&lt;br /&gt;var $useSMTPAuth = false;&lt;br /&gt;var $smtpUserName = '';&lt;br /&gt;var $smtpPassword = '';&lt;br /&gt;var $smtpHostNames = "localhost:25";&lt;br /&gt;var $text_body = null;&lt;br /&gt;var $html_body = null;&lt;br /&gt;var $to = null;&lt;br /&gt;var $toName = null;&lt;br /&gt;var $subject = null;&lt;br /&gt;var $cc = null;&lt;br /&gt;var $bcc = null;&lt;br /&gt;var $template = 'email/default';&lt;br /&gt;var $attachments = null;&lt;br /&gt;&lt;br /&gt;var $controller;&lt;br /&gt;&lt;br /&gt;function startup( &amp;amp; $controller)&lt;br /&gt;{&lt;br /&gt;   $this-&amp;gt;controller = &amp;amp; $controller;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt;* Helper function to generate the appropriate template location&lt;br /&gt;*&lt;br /&gt;* @return string CakePHP location of the template file&lt;br /&gt;* @param object $template_type&lt;br /&gt;*/&lt;br /&gt;function templateLocation($template_type)&lt;br /&gt;{&lt;br /&gt;   return ('..'.DS.strtolower($this-&amp;gt;controller-&amp;gt;name).DS.$this-&amp;gt;template.$template_type);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt;* Renders the content for either html or text of the email&lt;br /&gt;*&lt;br /&gt;* @return string Rendered content from the associated template&lt;br /&gt;* @param object $type_suffix&lt;br /&gt;*/&lt;br /&gt;function bodyContent($type_suffix)&lt;br /&gt;{&lt;br /&gt;   $temp_layout = $this-&amp;gt;controller-&amp;gt;layout; // store the current controller layout&lt;br /&gt;&lt;br /&gt;   if ($type_suffix == 'html')&lt;br /&gt;       $this-&amp;gt;controller-&amp;gt;layout = '..'.DS.'email';&lt;br /&gt;   else&lt;br /&gt;       $this-&amp;gt;controller-&amp;gt;layout = '';&lt;br /&gt;&lt;br /&gt;   $mail = $this-&amp;gt;controller-&amp;gt;render($this-&amp;gt;templateLocation('_'.strtolower($type_suffix)));&lt;br /&gt;   // render() automatically adds to the controller-&amp;gt;output, we'll remove it&lt;br /&gt;   $this-&amp;gt;controller-&amp;gt;output = str_replace($mail, '', $this-&amp;gt;controller-&amp;gt;output);&lt;br /&gt;&lt;br /&gt;   $this-&amp;gt;controller-&amp;gt;layout = $temp_layout; // restore the controller layout&lt;br /&gt;   return $mail;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function attach($filename, $asfile = '')&lt;br /&gt;{&lt;br /&gt;   if ( empty($this-&amp;gt;attachments))&lt;br /&gt;   {&lt;br /&gt;       $this-&amp;gt;attachments = array ();&lt;br /&gt;       $this-&amp;gt;attachments[0]['filename'] = $filename;&lt;br /&gt;       $this-&amp;gt;attachments[0]['asfile'] = $asfile;&lt;br /&gt;   } else&lt;br /&gt;   {&lt;br /&gt;       $count = count($this-&amp;gt;attachments);&lt;br /&gt;       $this-&amp;gt;attachments[$count+1]['filename'] = $filename;&lt;br /&gt;       $this-&amp;gt;attachments[$count+1]['asfile'] = $asfile;&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function send()&lt;br /&gt;{&lt;br /&gt;   App::import('Vendor', 'PHPMailer', array ('file'=&amp;gt;'phpmailer'.DS.'class.phpmailer.php'));&lt;br /&gt;&lt;br /&gt;   $mail = new PHPMailer();&lt;br /&gt;&lt;br /&gt;   $mail-&amp;gt;IsSMTP();&lt;br /&gt;   $mail-&amp;gt;SMTPAuth = $this-&amp;gt;useSMTPAuth;&lt;br /&gt;   $mail-&amp;gt;Host = $this-&amp;gt;smtpHostNames;&lt;br /&gt;   $mail-&amp;gt;Username = $this-&amp;gt;smtpUserName;&lt;br /&gt;   $mail-&amp;gt;Password = $this-&amp;gt;smtpPassword;&lt;br /&gt;&lt;br /&gt;   $mail-&amp;gt;From = $this-&amp;gt;from;&lt;br /&gt;   $mail-&amp;gt;FromName = $this-&amp;gt;fromName;&lt;br /&gt;   $mail-&amp;gt;AddAddress($this-&amp;gt;to, $this-&amp;gt;toName);&lt;br /&gt;   $mail-&amp;gt;AddReplyTo($this-&amp;gt;from, $this-&amp;gt;fromName);&lt;br /&gt;&lt;br /&gt;   $mail-&amp;gt;CharSet = 'UTF-8';&lt;br /&gt;   $mail-&amp;gt;WordWrap = 80; // set word wrap to 50 characters&lt;br /&gt;&lt;br /&gt;   if (! empty($this-&amp;gt;attachments))&lt;br /&gt;   {&lt;br /&gt;       foreach ($this-&amp;gt;attachments as $attachment)&lt;br /&gt;       {&lt;br /&gt;           if ( empty($attachment['asfile']))&lt;br /&gt;           {&lt;br /&gt;               $mail-&amp;gt;AddAttachment($attachment['filename']);&lt;br /&gt;           } else&lt;br /&gt;           {&lt;br /&gt;               $mail-&amp;gt;AddAttachment($attachment['filename'], $attachment['asfile']);&lt;br /&gt;           }&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   $mail-&amp;gt;IsHTML(true); // set email format to HTML&lt;br /&gt;&lt;br /&gt;   $mail-&amp;gt;Subject = $this-&amp;gt;sitePrefix.' '.$this-&amp;gt;subject;&lt;br /&gt;   $mail-&amp;gt;Body = $this-&amp;gt;bodyContent('html');&lt;br /&gt;   $mail-&amp;gt;AltBody = $this-&amp;gt;bodyContent('text');&lt;br /&gt;&lt;br /&gt;   $result = $mail-&amp;gt;Send();&lt;br /&gt;&lt;br /&gt;   if ($result == false)&lt;br /&gt;       $result = $mail-&amp;gt;ErrorInfo;&lt;br /&gt;&lt;br /&gt;   return $result;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;?&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Then, to use this component you can do the following in your controller ...&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt; var $components = array('Email');&lt;br /&gt;&lt;br /&gt; function send_verification($id)&lt;br /&gt; {&lt;br /&gt;     $this-&amp;gt;Email-&amp;gt;template = 'email/default';&lt;br /&gt;  &lt;br /&gt;     $this-&amp;gt;set('data', $this-&gt;data);&lt;br /&gt;     $toUser = $this-&amp;gt;User-&amp;gt;find(array('User.id'=&gt;$id));&lt;br /&gt;     $this-&amp;gt;Email-&amp;gt;to = $toUser['User']['email'];&lt;br /&gt;     $this-&amp;gt;Email-&amp;gt;subject = 'Your new account';&lt;br /&gt;&lt;br /&gt;     //$this-&gt;Email-&amp;gt;attach($fully_qualified_filename, optionally $new_name_when_attached);&lt;br /&gt;     // You can attach as many files as you like.&lt;br /&gt;&lt;br /&gt;     $result = $this-&amp;gt;Email-&gt;send();&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;I've tested it and it works even when used from another function in the same controller being called as a JSON or XML action extension.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/13327499-1869264112772168763?l=phazeforward.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://phazeforward.blogspot.com/feeds/1869264112772168763/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=13327499&amp;postID=1869264112772168763' title='47 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/13327499/posts/default/1869264112772168763'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/13327499/posts/default/1869264112772168763'/><link rel='alternate' type='text/html' href='http://phazeforward.blogspot.com/2009/03/emailing-from-cakephp-12x.html' title='Emailing from CakePHP 1.2.x'/><author><name>SenorGeek</name><uri>http://www.blogger.com/profile/01797272215878620138</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>47</thr:total></entry><entry><id>tag:blogger.com,1999:blog-13327499.post-8898420641756827849</id><published>2009-03-07T12:28:00.000-08:00</published><updated>2009-03-07T12:31:42.924-08:00</updated><title type='text'>Restarting my thoughts ...</title><content type='html'>Ok, everything old is gone ... I'll be starting up a new topic schedule in this space over the next two weeks as I reorganize my goals and ambitions ... for those less technically oriented it may be a bit on the geeky side and for those who enjoy a bit of geek in their lives I hope to offer a funny/informative take on things ...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/13327499-8898420641756827849?l=phazeforward.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://phazeforward.blogspot.com/feeds/8898420641756827849/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=13327499&amp;postID=8898420641756827849' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/13327499/posts/default/8898420641756827849'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/13327499/posts/default/8898420641756827849'/><link rel='alternate' type='text/html' href='http://phazeforward.blogspot.com/2009/03/restarting-my-thoughts.html' title='Restarting my thoughts ...'/><author><name>SenorGeek</name><uri>http://www.blogger.com/profile/01797272215878620138</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
