<?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>SPARQ.BLOG &#187; backgroundrb</title>
	<atom:link href="http://blog.sparqcode.com/tag/backgroundrb/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sparqcode.com</link>
	<description>The insider&#039;s guide to everything sparqcode</description>
	<lastBuildDate>Sat, 04 Feb 2012 18:41:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Actionmailer with BackgroundRb</title>
		<link>http://blog.sparqcode.com/2009/11/25/actionmailer-with-backgroundrb/</link>
		<comments>http://blog.sparqcode.com/2009/11/25/actionmailer-with-backgroundrb/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 20:57:46 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[Technical Development]]></category>
		<category><![CDATA[actionmailer]]></category>
		<category><![CDATA[backgroundrb]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.mskynet.com/msky-blog/?p=166</guid>
		<description><![CDATA[We have been working on a mailing service for a while now and we really like what we can do with html emails. Unfortunately, with the plethora of smart phones these days, HTML support is inconsistent. We figured that a good stop-gap solution was to offer both HTML and text versions of an email. Preferably &#8230; <a href="http://blog.sparqcode.com/2009/11/25/actionmailer-with-backgroundrb/">Read more <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><img class="alignright" title="Ruby on Rails" src="http://msky-blog.s3.amazonaws.com/rails/ror_logo.png" alt="" width="104" height="124" />We have been working on a mailing service for a while now and we really like what we can do with html emails. Unfortunately, with the plethora of smart phones these days, HTML support is inconsistent. We figured that a good stop-gap solution was to offer both HTML and text versions of an email. Preferably in one package.</p>
<p>And that&#8217;s where multipart emails come in.</p>
<p>Suprisingly (or not), this was rather easy to implement in Rails 2.3.</p>
<p>There are two ways of doing this. Implicit and explicit multipart emails. I decided to go with the former.</p>
<p>All I had to do was to make sure the content type is set correctly:</p>
<pre name="code" class="ruby:nocontrols">
 content_type    "multipart/alternative"
</pre>
<p>Then, I just had to make sure my template file ends with the right signature &#8220;foo.text.plain.erb&#8221; and &#8220;foo.text.html.erb&#8221; for plain text and html emails respectively.</p>
<p>See the multipart email section in the official doc here: <a title="http://api.rubyonrails.org/classes/ActionMailer/Base.html" href="http://api.rubyonrails.org/classes/ActionMailer/Base.html">http://api.rubyonrails.org/classes/ActionMailer/Base.html</a></p>
<p>I used a controller to trigger the email and it all worked just like a charm. I had my email with both text and html components. I checked in the code, fired up my mailer service which runs in backgroundrb and called it a day.</p>
<p>That&#8217;s when I saw this error popping up:</p>
<div id="_mcePaste" style="overflow: hidden; position: absolute; width: 1px; height: 1px; top: 220px; left: -10000px;">Message:can&#8217;t convert nil into String</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; width: 1px; height: 1px; top: 220px; left: -10000px;">/usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.2/lib/action_mailer/vendor/tmail-1.2.3/tmail/mail.rb:551:in `quote&#8217;</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; width: 1px; height: 1px; top: 220px; left: -10000px;">/usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.2/lib/action_mailer/vendor/tmail-1.2.3/tmail/mail.rb:551:in `read_multipart&#8217;</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; width: 1px; height: 1px; top: 220px; left: -10000px;">/usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.2/lib/action_mailer/vendor/tmail-1.2.3/tmail/mail.rb:540:in `parse_body_0&#8242;</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; width: 1px; height: 1px; top: 220px; left: -10000px;">/usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.2/lib/action_mailer/vendor/tmail-1.2.3/tmail/mail.rb:522:in `parse_body&#8217;</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; width: 1px; height: 1px; top: 220px; left: -10000px;">/usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.2/lib/action_mailer/vendor/tmail-1.2.3/tmail/mail.rb:476:in `body=&#8217;</div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; width: 1px; height: 1px; top: 220px; left: -10000px;">/usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.2/lib/action_mailer/base.rb:650:in `create_mail&#8217;</div>
<pre>can't convert nil into String

/usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.2/lib/action_mailer/vendor/tmail-1.2.3/tmail/mail.rb:551:in `quote'

/usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.2/lib/action_mailer/vendor/tmail-1.2.3/tmail/mail.rb:551:in `read_multipart'

/usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.2/lib/action_mailer/vendor/tmail-1.2.3/tmail/mail.rb:540:in `parse_body_0'

/usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.2/lib/action_mailer/vendor/tmail-1.2.3/tmail/mail.rb:522:in `parse_body'

/usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.2/lib/action_mailer/vendor/tmail-1.2.3/tmail/mail.rb:476:in `body='

/usr/lib/ruby/gems/1.8/gems/actionmailer-2.3.2/lib/action_mailer/base.rb:650:in `create_mail'

...</pre>
<p>After some investigation, I figure that this is one of those things where backgroundrb is incompatible with a certain feature of rails. I considered fudging around with the tmail code but gave up on that endeavor quickly. Instead, I decided to work around with an explicit version of the multipart email. To my delight, that worked perfectly.</p>
<pre name="code" class="ruby:nocontrols"content_type "multipart/alternative">

part :content_type =&gt; “text/plain”,
  :body =&gt; render_message(“foo.text.plain”, :variable1=&gt;variable1, :variable2 =&gt; variable2)
part :content_type =&gt; “text/html”,
  :body =&gt; render_message(“foo.text.html”, :variable1=&gt;variable1, :variable2 =&gt; variable2)
</pre>
<p>Be sure to declare text/plain and text/html in that order. Email clients like Gmail may not offer the text alternative or show the HTML version automatically if you change that order.</p>
<p>The template files work as-is without any modifications. Well, crisis averted. Back to making some barcodes. :)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sparqcode.com/2009/11/25/actionmailer-with-backgroundrb/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

