sendgrid / sendgrid-ruby

The Official Twilio SendGrid Led, Community Driven Ruby API Library
https://sendgrid.com
MIT License
620 stars 324 forks source link

uninitialized constant SendgridService::Personalization #484

Closed FlandersBurger closed 2 years ago

FlandersBurger commented 2 years ago

Issue Summary

My code is practically identical to the example that includes personalization. However, I get the following error:

NameError (uninitialized constant SendgridService::Personalization

image

Sending an email without personalization works fine.

Code Snippet

def send_email(communication)
    # Send email
    from = SendGrid::Email.new(email: communication.sender.email)
    to = SendGrid::Email.new(email: communication.recipient.email)
    content = SendGrid::Content.new(type: 'text/plain', value: communication.message)
    if communication.template_id.present?
      mail = SendGrid::Mail.new(from, communication.subject, to)
      mail.template_id = communication.template_id
      personalization = Personalization.new
      personalization.add_dynamic_template_data(communication.message.to_json)
      mail.add_personalization(personalization)
    else
      mail = SendGrid::Mail.new(from, communication.subject, to, content)
    end

    # Attach media
    if communication.links.present?
      media = SendGrid::Media.new(type: 'image/jpeg', value: communication.links)
      mail.add_media(media)
    end

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

Exception/Log

14:58:24 app.1     | Completed 500 Internal Server Error in 241ms (ActiveRecord: 55.7ms | Allocations: 119061)
14:58:24 app.1     | 
14:58:24 app.1     | 
14:58:24 app.1     |   
14:58:24 app.1     | NameError (uninitialized constant SendgridService::Personalization):
14:58:24 app.1     |   
14:58:24 app.1     | app/services/sendgrid_service.rb:18:in `send_email'
14:58:24 app.1     | app/models/communications/email.rb:14:in `send_email'
14:58:24 app.1     | app/models/listing.rb:96:in `send_reject_email'
14:58:24 app.1     | app/controllers/api/listings_controller.rb:25:in `update'

Technical details:

FlandersBurger commented 2 years ago

Just had to add SendGrid::