2014-05-07 10 views
8

Utilizzo API Mailgun HTTP per inviare e-mail dall'applicazione rails. Ma non sono in grado di visualizzare le visualizzazioni del mailer. Questa è la mia classe mailer.Come visualizzare le viste mailer delle rotaie nell'API HTTP Mailgun

class MailgunMails < ActionMailer::Base 

    def send_complex_message 
     data = Multimap.new 
     data[:from] = "Excited User <[email protected]#{@@domain}>" 
     data[:to] = "[email protected]" 
     data[:subject] = "Hello" 
     data[:html] = File.open("#{Rails.root}/app/views/mailgun_mails/send_complex_message.html.erb", 'r').to_s.html_safe 
     ## also tried 
     data[:html] = render(template: "send_complex_message.html.erb") 
     response = RestClient.post "https://api:#{@@private_key}"\ 
     "@api.mailgun.net/v2/#{@@domain}/messages", data 

     response = JSON.parse(response) 

    end 

end 

Ma che getta:

NoMethodError: undefined method `each_byte' for nil:NilClass

Come faccio a utilizzare viste mailer con Mailgun API.

PS: So che ci sono molte gemme disponibili per usare il mailgun con le rotaie ma nessuno di loro offre la piena funzionalità come fa l'API.

SE esiste un modo alternativo per inviare e-mail dall'api utilizzando le visualizzazioni di binari, sto aspettando quella risposta. Io proprio non so come fare (anche se non usa ActionMailer)

PIENA Traccia di errore

NoMethodError: undefined method `each_byte' for nil:NilClass 
    from /Users/example/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/uri/common.rb:307:in `block in escape' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/core_ext/string/output_safety.rb:177:in `gsub' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/core_ext/string/output_safety.rb:177:in `gsub' 
    from /Users/example/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/uri/common.rb:304:in `escape' 
    from /Users/example/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/uri/common.rb:623:in `escape' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:150:in `handle_key' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:143:in `block in build_stream' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:142:in `collect' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:142:in `build_stream' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:53:in `initialize' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:18:in `new' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:18:in `generate' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/request.rb:45:in `initialize' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `new' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient.rb:72:in `post' 
... 2 levels... 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/abstract_controller/callbacks.rb:18:in `block in process_action' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/callbacks.rb:373:in `_run__1281843520849433682__process_action__callbacks' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/callbacks.rb:80:in `run_callbacks' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/abstract_controller/callbacks.rb:17:in `process_action' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/abstract_controller/base.rb:136:in `process' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/abstract_controller/rendering.rb:44:in `process' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionmailer-4.0.4/lib/action_mailer/base.rb:503:in `process' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionmailer-4.0.4/lib/action_mailer/base.rb:497:in `initialize' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionmailer-4.0.4/lib/action_mailer/base.rb:480:in `new' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionmailer-4.0.4/lib/action_mailer/base.rb:480:in `method_missing' 
    from (irb):1 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.4/lib/rails/commands/console.rb:90:in `start' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.4/lib/rails/commands/console.rb:9:in `start' 
    from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.4/lib/rails/commands.rb:62:in `<top (required)>' 
    from bin/rails:4:in `require' 
+0

Quindi, stai inviando un'e-mail nel tuo Mailer chiamando la loro API al suo interno? È un modello che non ho mai visto prima. http://www.leemunroe.com/send-automated-email-ruby-rails-mailgun/ –

+0

Apparentemente sì. Non riesco a usare le impostazioni smtp. (Non voglio usare) poiché la mia email di invio verrà modificata dinamicamente. Qual è il modo giusto per fare ciò che voglio raggiungere? –

+0

Perché non puoi usare SMTP? Solo curioso. È possibile modificare dinamicamente i modelli di posta elettronica e continuare a utilizzare il comportamento predefinito di ActionMailer. Puoi cambiare le viste usando il metodo render. –

risposta

6

Come sull'utilizzo del metodo render_to_string? Credo che tu voglia generare dinamicamente il contenuto della vista. Leggendo il modello di vista aprendo il file non si eseguirebbe alcun codice Ruby in esso.

class MailgunMails < ActionMailer::Base 

     def send_complex_message 
      data = Multimap.new 
      data[:from] = "Excited User <[email protected]#{@@domain}>" 
      data[:to] = "[email protected]" 
      data[:subject] = "Hello" 
      html_output = render_to_string template: "mailgun_mails/send_complex_message" 
      data[:html] = html_output.to_str 
      response = RestClient.post "https://api:#{@@private_key}"\ 
      "@api.mailgun.net/v2/#{@@domain}/messages", data 

      response = JSON.parse(response) 

     end 

    end 
+0

Già provato. Dà lo stesso errore! –

+1

Prova ad usare il metodo "to_str", come mostrato sopra nel codice aggiornato. to_str per forzarlo a restituire stringhe vere. –

+0

Funziona ma solo come una stringa pura. L'HTML non è reso. (i collegamenti, ecc.) –

Problemi correlati