<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nerdnotes.org &#187; phpmailer</title>
	<atom:link href="http://nerdnotes.org/tag/phpmailer/feed/" rel="self" type="application/rss+xml" />
	<link>http://nerdnotes.org</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Fri, 17 Dec 2010 22:55:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>CakePHP Email component</title>
		<link>http://nerdnotes.org/2009/10/cakephp-email-component/</link>
		<comments>http://nerdnotes.org/2009/10/cakephp-email-component/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 22:11:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[cakephp]]></category>
		<category><![CDATA[nerdnotes]]></category>
		<category><![CDATA[component]]></category>
		<category><![CDATA[phpmailer]]></category>

		<guid isPermaLink="false">http://nerdnotes.org/?p=120</guid>
		<description><![CDATA[This is quite a simple component that integrates PHPMailer with CakePHP. Save the following code to /app/controllers/components/email.php In addition to that, make sure phpmailer is installed in /app/vendors/phpmailer. I just extracted the phpmailer archive in /app/vendors/ and created a symlink: $ tar xzf PHPMailer_v5.0.2.tar.gz $ ln -s PHPMailer_v5.0.2 phpmailer Usage In your controller code, make [...]]]></description>
			<content:encoded><![CDATA[<p>This is quite a simple component that integrates <a href="http://phpmailer.worxware.com/index.php?pg=phpmailer">PHPMailer</a> with <a href="http://cakephp.org">CakePHP</a>. Save the following code to /app/controllers/components/email.php</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php 

App::import('Vendor', 'PhpMailer', array('file' =&gt; 'phpmailer' . DS . 'class.phpmailer.php'));

/*
    Depends on &quot;unhtml&quot;
*/

class EmailComponent extends PHPMailer
{
	// phpmailer
	var $Mailer = 'sendmail'; // choose 'sendmail', 'mail', 'smtp'
	var $unhtml_bin = '/usr/bin/unhtml';

	// component
	var $controller;

	function startup( &amp;$controller )
	{
		$this-&gt;controller = &amp;$controller;
	}

	function renderBody($view)
	{
		// render the view and use its output to set the body text of the email
		$this-&gt;Body = $this-&gt;controller-&gt;render('emails/' . $view, 'email');

		// reset the output of the controller
		$this-&gt;controller-&gt;output = '';

		// create plain text version of the email
		//
		// create temporary files
		$htmlfile = tempnam(TMP, 'htmlfile');
		$textfile = tempnam(TMP, 'textfile');

		// write html to temporary file
		file_put_contents($htmlfile, $this-&gt;Body);

		// convert the html file to plain text
		$cmd = &quot;cat $htmlfile | $this-&gt;unhtml_bin &gt; $textfile&quot;;
		system($cmd);

		// set the plain text body of the email
		$this-&gt;AltBody = file_get_contents($textfile);

		// remove temporary files
		unlink($htmlfile);
		unlink($textfile);
	}
}

?&gt;
</pre>
<p>In addition to that, make sure phpmailer is installed  in /app/vendors/phpmailer. I just extracted the phpmailer archive in /app/vendors/ and created a symlink:<br />
<code><br />
$ tar xzf PHPMailer_v5.0.2.tar.gz<br />
$ ln -s PHPMailer_v5.0.2 phpmailer<br />
</code><br />
<strong>Usage</strong><br />
In your controller code, make sure the component is included by adding it to the components array:</p>
<pre class="brush: php; title: ; notranslate">
var $components = array('Email');
</pre>
<p>In app/views/layouts, create a new layout called email.ctp. It should contain something like:</p>
<pre class="brush: php; title: ; notranslate">
&lt;html&gt;
&lt;body&gt;
&lt;?php echo $content_for_layout; ?&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p><strong>Let&#8217;s send some emails!</strong><br />
In controller snippet, the renderBody(&#8216;test&#8217;) call will create a html email from the view in app/views/users/emails/test.ctp. Additionally, a plain text version will be generated using &#8216;unhtml&#8217;. Unhtml has a package in the Ubuntu / Debian repository.<br />
The action works just like rendering a normal view. The $this->Email object is just an instance of PHPMailer and all PHPMailers&#8217; methods and properties apply.</p>
<pre class="brush: php; title: ; notranslate">
function testmail()
{
	// *Some notes*
	// Email methods:    http://phpmailer.worxware.com/index.php?pg=methods
	// Email properties: http://phpmailer.worxware.com/index.php?pg=properties
	//
	// Email configuration is done in /app/controllers/components/email.php

	$this-&gt;set(compact('some', 'vars'));

	// the email content is just a (html) view in app/views/{controller}/emails/testmail.ctp
	$this-&gt;Email-&gt;renderBody('test');

	// subject
	$this-&gt;Email-&gt;Subject = 'Test from example.com';

	// sender
	$this-&gt;Email-&gt;SetFrom('test@example.com', 'Test');

	// recipients
	$this-&gt;Email-&gt;AddAddress('joe@example.com', 'Joe');
	$this-&gt;Email-&gt;AddAddress('jane@example.com', 'Jane');

	// send!
	$this-&gt;Email-&gt;Send();
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://nerdnotes.org/2009/10/cakephp-email-component/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

