2015-09-25 15 views
5

Come faccio a legare il mio mailer a Sendgrid con la gemma smtpapi-ruby? Ho seguito il loro limited documentation, ma le mie e-mail non sono passate, ho verificato che la mia implementazione di SendGrid funziona bene solo quando si invia una semplice e-mail, quindi non è così. Questo è quello che ho:Rails Mailer con il modello SendGrid

user_controller.rb

def create 
    @user = User.new(user_params) 

    respond_to do |format| 
     if @user.save 
     format.html { redirect_to @user, notice: 'User was successfully created.' } 
     format.json { render :show, status: :created, location: @user } 


     header = Smtpapi::Header.new 
     header.add_to(@user.email) 
     header.add_substitution('user', [@user.name]) 
     header.add_substitution('body', "You've registered! This is from the controller.") 
     header.add_filter('templates', 'enable', 1) 
     header.add_filter('templates', 'template_id', 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx') 
     header.to_json 

     UserNotifier.welcome(header).deliver 
     else 
     format.html { render :new } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

mailer/user_notifier.rb

class UserNotifier < ApplicationMailer 
    default from: "[email protected]" 

    def welcome(header) 
    headers['X-SMTPAPI'] = hdr.to_json 
    mail(subject: "Welcome to the site!") 
    end 
end 

views/user_notifier/welcome.html.erb

<html> 
<body> 
    Hi -user-<br /> 
    Thanks so much for joining us! 

    <p>-body-</p> 

    Thanks,<br /> 
    The Microblog Team 
</body> 
</html> 

Non vedo nulla sul registro delle attività di SendGrid, quindi non viene nemmeno inviato lì, almeno questa è la mia ipotesi.

Cosa sto sbagliando?

risposta

6

Credo che tu abbia mischiato le variabili. Chiami hdr.to_json e il nome del parametro è header che è già stato convertito in json.

si dovrebbe includere l'intestazione metadati direttamente nel UserNotifier:

headers "X-SMTPAPI" => { 
    "sub": { 
     "%name%" => [user.name] 
    }, 
    "filters": { 
     "templates": { 
     "settings": { 
      "enable": 1, 
      "template_id": 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx' 
     } 
     } 
    } 
    }.to_json 

# the 'to' email can be overridden per action 
mail(
    from: '[email protected]', 
    to: '[email protected]', 
    subject: "Hello World" 
) 

È inoltre possibile passare il contenuto se UserNotifier.welcome è utilizzato in altre parti della vostra applicazione:

UserNotifier.welcome(user: @user, subject: "Welcome!").deliver_now 

# user_notifier.rb 

class UserNotifier < ApplicationMailer 
    default from: "[email protected]" 

    def welcome(user: , subject: , template: "default") 

    # template's default view is "default" 
    headers "X-SMTPAPI" => { 
    "sub": { 
     "%name%" => [user.name] 
    }, 
    "filters": { 
     "templates": { 
     "settings": { 
      "enable": 1, 
      "template_id": 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx' 
     } 
     } 
    } 
    }.to_json 

    mail(
     from: '[email protected]', 
     to: user.email, 
     subject: subject, 
     template_path: 'path/to/view', 
     template_name: template 
    ) 
    # this would try to render the view: `path/to/view/default.erb` 
    end 
end 

Nel modello, puoi includere i tag di sostituzione includendo il nome del tag:

<h1>Hello %name%!</h1> 

More information about substitution tags

Scopri la documentazione SendGrid su using their template system

3

si deve modificare il codice nel vostro programma di posta a qualcosa di simile:

class UserNotifier < ApplicationMailer 
    default from: "[email protected]" 

    def welcome(hdr) 
    headers['X-SMTPAPI'] = hdr.asJSON() 
    mail(subject: "Welcome to the site!") 
    end 
end 

Esempio: https://sendgrid.com/docs/Integrate/Code_Examples/SMTP_API_Header_Examples/ruby.html

+0

Ok, questo ha un senso, ma ora ottengo 'indefinito variabile locale o metodo' header' per # '' – Godzilla74

+0

tuoi inheriths ApplicationMailer' da 'ActionMailer :: Base'? in caso contrario, si può provare ad utilizzare quanto segue: 'class UserNotifier Aguardientico

+0

Doveva cambiare la linea in' intestazioni ['X-SMTPAPI'] = hdr.to_json', quindi sarebbe inviata ... ancora non andando attraverso però. – Godzilla74

Problemi correlati