使用rails将附件添加到SendGrid的电子邮件中

时间:2018-05-07 13:18:48

标签: ruby-on-rails sendgrid sendgrid-ruby

我创建了一个hello_world方法,该方法使用Sendgrid发送电子邮件。我想包括一个附件。我在另一个stackoverflow答案中找到了以下行:mail.attachments['test.txt'] = File.read("#{Rails.root}/public/test.txt")

然而,这会产生以下错误:

Completed 500 Internal Server Error in 17ms (ActiveRecord: 3.4ms)



TypeError - no implicit conversion of String into Integer:
  app/controllers/messages_controller.rb:32:in `hello_world'
  app/controllers/messages_controller.rb:65:in `create'

控制器中的邮件代码:

  def hello_world(company, message)
    from = Email.new(email: "test+#{current_user.auth_token}@example.com")
    to = Email.new(email: 'hello@pim.gg')

    subject = 'TEST from dev'
      content = Content.new(type: 'text/plain', value: "#{company.email} #{current_user} #{current_user.first_name} #{current_user.last_name} #{message.text}")

    mail = SendGrid::Mail.new(from, subject, to, content)
    mail.attachments['test.txt'] = File.read("#{Rails.root}/public/test.txt")

    sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
    response = sg.client.mail._('send').post(request_body: mail.to_json)
    puts response.status_code
    puts response.body
    puts response.headers
  end

1 个答案:

答案 0 :(得分:1)

根据sendgrid-ruby gem adding-attachments的文档应该是这样的:

var input = moment().toISOString();
var output = encodeURIComponent(input);
var endpoint = 'https://example.com/endpoint/date=' + output;
...