# Problem with instance vars in explicit multipart email using ActionMailer

**URL:** <https://www.ruby-forum.com/t/problem-with-instance-vars-in-explicit-multipart-email-using-actionmailer/175727>\
**Category:** Rails\
**Created:** [September 18, 2009, 3:36pm UTC](https://www.ruby-forum.com/t/problem-with-instance-vars-in-explicit-multipart-email-using-actionmailer/175727 "2009-09-18T15:36:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacib](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/da6949/32.png) [@jacib](https://www.ruby-forum.com/u/jacib)\
**Post date:** [September 18, 2009, 3:36pm UTC](https://www.ruby-forum.com/t/problem-with-instance-vars-in-explicit-multipart-email-using-actionmailer/175727/1 "2009-09-18T15:36:12Z")

</div>

Hi everyone,

I’m having trouble accessing instance vars passed to my email template  
as part of an explicit multipart message, and I wonder if anyone here  
could help me.

The task at hand is to deliver email with only the text/html content  
type if the message lacks a plaintext (from a record in the db) and to  
send multipart email if it has both plaintext & html available.

Here’s the simplified code from the mailer:

#SampleMailer.rb

```
def email(recipient, content)
    subject content.title
    recipients recipient.email
    from 'removed_email_address@domain.invalid'
    headers 'return-path' => 'removed_email_address@domain.invalid'
    sent_on Time.now

    if context.plaintext.empty? # force HTML email if the module

```

lacks plaintext  
content\_type ‘text/html’  
body :content =\> content, :user =\> recipient  
else # send a multi-part email  
part “text/plain” do |p|  
p.body = render\_message(“email\_plain”, :content =\>  
content, :user =\> recipient)  
p.transfer\_encoding = “base64”  
end  
part “text/html” do |p|  
p.body = render\_message(“email”, :content =\>  
content, :user =\> recipient)  
end  
end  
end

This works fine if content.plaintext is empty – it properly delivers  
the HTML-only email. If however, content.plaintext is not empty (ie,  
it should send a multipart message), the template throws an error.  
For a template like:

# email.erb

\<%= @content.title %\>

The error is:

NoMethodError (You have a nil object when you didn’t expect it! The  
error occurred while evaluating nil.title)

Thank you for your help in looking at this. I’m much obliged for any  
advice you can offer.

Sincerely,

Jacob
