sendgrid-ruby icon indicating copy to clipboard operation
sendgrid-ruby copied to clipboard

NoMethodError undefined method `personalizations='

Open treycaliva opened this issue 10 years ago • 5 comments

Issue Summary

Issues with personalizations and transactional templates

Steps to Reproduce

When using transactional template example I am receiving the following error message NoMethodError: undefined methodpersonalizations=' for #Mail::Message:0x007fa0e2bef750`

Technical details:

  • sendgrid-ruby Version: master
  • Ruby Version: 2.3.1

treycaliva avatar Sep 28 '16 16:09 treycaliva

Hi @treycaliva,

Please take a look at these examples: https://github.com/sendgrid/sendgrid-ruby/blob/master/USE_CASES.md#transactional_templates

If you are still having issues, please shoot over your source code so I can try to reproduce. Thanks!

thinkingserious avatar Sep 28 '16 17:09 thinkingserious

Hello, I am also having this same issue. I have written a small ruby class to help me with building the sendgrid mail objects:

require 'sendgrid-ruby'
include SendGrid

class MySendgridMailer::Base
  attr_accessor :mail

  def initialize
    @mail = Mail.new
  end

  def deliver!
    mail.from = Email.new(email: '[email protected]')
    mail.subject = 'I\'m replacing the subject tag'
    personalization = Personalization.new
    personalization.to = Email.new(email: '[email protected]')
    mail.personalizations = personalization
    mail.contents = Content.new(type: 'text/html', value: 'I\'m replacing the <strong>body tag</strong>')
    mail.template_id = 'TEMPLATE_ID'

    sg = SendGrid::API.new(api_key: 'API_KEY')
    begin
        response = sg.client.mail._("send").post(request_body: mail.to_json)
    rescue Exception => e
        puts e.message
    end
    puts response.status_code
    puts response.body
    puts response.headers
    
  end

end

I get undefined method errors for mail.personalizations along with mail.template_id

sendgrid-ruby (4.0.6) ruby 2.3.1

I have followed this example: https://github.com/sendgrid/sendgrid-ruby/blob/master/USE_CASES.md#transactional_templates

but still am getting the same issues. Am i doing something wrong creating the Mail object? is it possibly not what the sendgrid-ruby gem provides?

Thanks!

pwgustafson avatar Nov 23 '16 00:11 pwgustafson

Hello @pwgustafson,

I tried to reproduce your error with the following code:

require 'sendgrid-ruby'
include SendGrid

class MySendgridMailer
  attr_accessor :mail

  def initialize
    @mail = Mail.new
  end

  def deliver!
      mail.from = Email.new(email: '[email protected]')
      mail.subject = 'I\'m replacing the subject tag'
      personalization = Personalization.new
      personalization.to = Email.new(email: '[email protected]')
      mail.personalizations = personalization
      mail.contents = Content.new(type: 'text/html', value: 'I\'m replacing the <strong>body tag</strong>')
      mail.template_id = 'TEMPLATE_ID'

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

end

sg = MySendgridMailer.new
sg.deliver!

and the email sent without error.

My ruby version is ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin15] sendgrid-ruby 4.0.6

Do you have any suggestions on how I can try to verify your issue?

thinkingserious avatar Nov 29 '16 04:11 thinkingserious

Hi @thinkingserious,

I actually figured out what was going on, and I forgot to update this ticket. It was actually because the @mail = Mail.new was not a sendgrid mail object, probably an ActionMailer object.

Thanks for getting back to me.

pwgustafson avatar Nov 29 '16 04:11 pwgustafson

Thanks for the follow up!

thinkingserious avatar Nov 29 '16 04:11 thinkingserious