padrino / padrino-framework

Padrino is a full-stack ruby framework built upon Sinatra.
http://www.padrinorb.com
MIT License
3.37k stars 509 forks source link

custom path to mailer template #109

Closed marcboeker closed 14 years ago

marcboeker commented 14 years ago

hi guys,

is there a way to add the possibility to specify the path to the mail template directly in the mailer class.

for example to use the mail template from views/customers/xyz instead of views/account_mailer

class AccountMailer < Padrino::Mailer::Base

  def account_registered(email, data)
    template_path 'views/customers/xyz' # <--- an extra option like this
    from 'foo@bar.org'
    to email
    subject 'Subject'
    body data
    content_type 'text/html'
    via :smtp
  end

end

thanks and cheers, marc

DAddYE commented 14 years ago

No at the moment is not possible, but that's is a good addition.

nesquena commented 14 years ago

I agree, I will try to get this in soon.

nesquena commented 14 years ago

I believe this commit 798dc56ab711b9f25e6098f4c9fa31bee0ccc8dd should add support for this feature ala the new 'template' option

template 'views/customers/xyz' # <--- an extra option like this

Let me know if theres any issues.

marcboeker commented 14 years ago

hi nesquena,

thanks for beeing so fast in implementing this :) using the mailer with the template option in the development console works without any issues. but with the padrino server in development/production mode, the template cannot be found. padrino doesn't throw any errors, but the mail body is empty and the views_path attribute results in an array instead of a file path.

do you encounter the same issue?

i'm really impressed how fast and active you are. it's great to see that you are really pushing padrino to become the framework of choice for sinatra lovers :)

cheers marc

DAddYE commented 14 years ago

views_path attribute results in an array instead of a file path.

Yes that's is normal because there is views_path of each mounted app.

marcboeker commented 14 years ago

ah okay :) are there different path settings for views in the development console compared to the server mode?

DAddYE commented 14 years ago

No are the same. It's not for you?

marcboeker commented 14 years ago

hm, this is what i've encountered while testing.

using the console:

>> AccountMailer.deliver(:activate_account, 'foo', {})
"/srv/example/account/views/clients/foo/mails/activate_account.erb"
DEBUG - [10/Apr/2010 10:49:43] "Sending email via smtp:
From: foo@bar.org
To: foo@foobar.org
Subject: New activation request
Content-Type: text/html; charset=UTF-8

test"
=> true

and now via padrino's server:

DEBUG - [10/Apr/2010 10:50:57] "  MONGODB example_development['users'].find({:_id=>ObjectID('4bbf0383a917ba6d74000001')}, {}).limit(-1)"
"/srv/example/account/views/clients/foo/mails/activate_account.erb"
DEBUG - [10/Apr/2010 10:50:57] "Sending email via smtp:
From: foo@bar.org
To: foo@foobar.org
Subject: New activation request
Content-Type: text/html; charset=UTF-8

"

in the console, the mail has a non empty body and in the server, the mail body is empty. that is confusing me :)

marcboeker commented 14 years ago

ps: i've added

p final_template

to padrino-mailer's base.rb to see the template path.

DAddYE commented 14 years ago

are you using latest feature of Nathan?

DAddYE commented 14 years ago

can you paste me your mailer?

marcboeker commented 14 years ago

yep, i've done a fresh git pull and rake install before testing it:

class AccountMailer < Padrino::Mailer::Base

  def activate_account(client, data)
    template "clients/#{client}/mails/activate_account"
    from 'foo@bar.org'
    to 'foobar@bar.org'
    subject "New activation request"
    body data
    content_type 'text/html'
    via :smtp
  end

end
DAddYE commented 14 years ago

can you add p @mail_attributes[:body] before line 51 ?

marcboeker commented 14 years ago

in the console, i can see the body value but using the server i only see nil.

def deliver
  p @mail_attributes[:body]
  @mail_attributes.reverse_merge!(:via => self.delivery_method.to_sym)
  @mail_attributes.reverse_merge!(:smtp => @smtp_settings) if using_smtp?
  self.send_mail(@mail_attributes)
end
DAddYE commented 14 years ago

try this:

      def body(body_value)
        final_template = template_path
        raise "Template for '#{@mail_name}' could not be located in views path!" unless final_template
        @mail_attributes[:body] = Tilt.new(final_template).render(self, body_value.symbolize_keys) if body_value.is_a?(Hash)
        p body_value.inspect
        p @mail_attributes[:body] 
        @mail_attributes[:body] = body_value if body_value.is_a?(String)
      end
marcboeker commented 14 years ago

ah sorry, i've looked at the wrong file :)

it's exactly the same result. body_value gives me the right value but code>@mail_attributes[:body]</code is nil within the server. in the console i see in code>@mail_attributes[:body]</code the correct mail body.

very strange behaviour.

DAddYE commented 14 years ago

Yep super strange, Im using it on production and I don't have this problem.

Btw can you tell the result of p body_value.inspect ?

marcboeker commented 14 years ago

ahh i've found the problem. i'm passing an object from mongomapper to the body, not a hash. in the dev console, i haven't got that object so i've used an empty hash instead.

the if body_value.is_a?(Hash) after the render() method call prevents passing an object. so it was my fault, sorry for steeling your time :)

thanks and cheers

DAddYE commented 14 years ago

Heheh, no problem!

nesquena commented 14 years ago

Ah I am glad we have this working then :) Thanks for the suggestion marc!

i'm really impressed how fast and active you are. it's great to see that you are really pushing padrino to become the framework of choice for sinatra lovers :)

Yes, we are committed to having Padrino become a standard for use with Sinatra to build web applications. And yes, a hash (containing the body variables) should be passed to body in order for the template to render right now.