how tie in rails mailer sendgrid using smtpapi-ruby gem? i've followed limited documentation, emails aren't going through, i've verified sendgrid implementation works fine when sending plain email, that's not it. have:
user_controller.rb
def create @user = user.new(user_params) respond_to |format| if @user.save format.html { redirect_to @user, notice: 'user 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! 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
mailers/user_notifier.rb
class usernotifier < applicationmailer default from: "test@test.com" def welcome(header) headers['x-smtpapi'] = hdr.to_json mail(subject: "welcome site!") end end
views/user_notifier/welcome.html.erb
<html> <body> hi -user-<br /> joining us! <p>-body-</p> thanks,<br /> microblog team </body> </html>
i'm not seeing come through on sendgrid activity log, it's not getting sent there, @ least that's guess.
what doing wrong?
i think you've mixed variables. call hdr.to_json
, param name header
converted json.
you should include header meta data directly usernotifier
:
headers "x-smtpapi" => { "sub": { "%name%" => [user.name] }, "filters": { "templates": { "settings": { "enable": 1, "template_id": 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx' } } } }.to_json # 'to' email can overridden per action mail( from: 'test@test.com', to: 'recipient@test.com', subject: "hello world" )
you can pass content if usernotifier.welcome
used in other parts of app:
usernotifier.welcome(user: @user, subject: "welcome!").deliver_now # user_notifier.rb class usernotifier < applicationmailer default from: "test@test.com" def welcome(user: , subject: , template: "default" ) # template's default view "default" headers "x-smtpapi" => { "sub": { "%name%" => [user.name] }, "filters": { "templates": { "settings": { "enable": 1, "template_id": 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx' } } } }.to_json mail( from: 'test@test.com', to: user.email, subject: subject, template_path: 'path/to/view', template_name: template ) # try render view: `path/to/view/default.erb` end end
in template, can include substitution tags including name of tag:
<h1>hello %name%!</h1>
more information substitution tags
check out sendgrid docs on using template system
Comments
Post a Comment