2015-09-25 48 views
5

如何使用smtpapi-ruby gem将我的rails邮件程序绑定到Sendgrid?我按照他们的limited documentation,但我的电子邮件没有通过,我已经验证,发送一封普通电子邮件时,我的SendGrid实施工作正常,所以不是这样。这是我有:使用SendGrid模板的Rails邮件程序

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 

邮寄/ 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 

的意见/ 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> 

我在SendGrid活动日志中没有看到任何东西,所以它甚至没有发送到那里,至少这是我的猜测。

我在做什么错?

回答

6

我想你混淆了你的变量。您可以拨打hdr.to_json,参数名称为header,该名称也已转换为json。

您应该包含头的元数据直接导入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" 
) 

您也可以通过内容如果UserNotifier.welcome在你的应用程序的其他部分使用:

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 

在模板中,您可以通过包含标签名称来包含替代标签:

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

More information about substitution tags

检查出using their template system

3

你必须在你的邮件代码更改为类似:

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

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

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

+0

的Sendgrid文档好吧,这是有道理的,但现在我得到'未定义的局部变量或方法'头”为#' – Godzilla74

+0

你'ApplicationMailer' inheriths从“ActionMailer :: Base”?如果没有,你可以尝试使用下面的代码:'class UserNotifier Aguardientico

+0

不得不将行改为'headers ['X-SMTPAPI'] = hdr.to_json'然后它会提交...仍然不会尽管如此。 – Godzilla74

相关问题