Coder Social home page Coder Social logo

Comments (14)

34code avatar 34code commented on August 17, 2024 1

I think I was using 3.0.7 .. but let me try 4.0.0 and confirm if this is a definite issue. I'll also provide with the exact error message alongside code shortly.

from sendgrid-ruby.

thinkingserious avatar thinkingserious commented on August 17, 2024 1

Awesome, thanks!

from sendgrid-ruby.

34code avatar 34code commented on August 17, 2024 1

ok that worked but I had to switch the lines and put the line with 'text/plain' before the line with 'text/html'...

from sendgrid-ruby.

34code avatar 34code commented on August 17, 2024 1

awesome, thanks!

from sendgrid-ruby.

thinkingserious avatar thinkingserious commented on August 17, 2024

Hello @34code,

I could not reproduce that error with this code:

require 'sendgrid-ruby'
include SendGrid
require 'json'

def hello_world
    mail = Mail.new()
    mail.from = Email.new(email: "[email protected]")
    mail.subject = "Hello World from the SendGrid Ruby Library"
    personalization = Personalization.new()
    personalization.to = Email.new(email: "[email protected]")
    mail.personalizations = personalization
    mail.contents = Content.new(type: "text/plain", value: "some text here")
    mail.contents = Content.new(type: "text/html", value: "<html><body>some text here</body></html>")
    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

hello_world

What version of the library are you using?

from sendgrid-ruby.

34code avatar 34code commented on August 17, 2024

still broken. tested in both 4.0.1 and 4.0.0

error:

App 88244 stdout: 400
App 88244 stdout: {"errors":[{"message":"Invalid type. Expected: object, given: string.","field":"content.0","help":null},{"message":"Invalid type. Expected: object, given: string.","field":"personalizations.0.to.0","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.personalizations.to"}]}

code used in rails5

template_id = "6ede18bb-2eba-4958-8a57-43a58a559a0a"

from = SendGrid::Email.new(email: '[email protected]')
subject = "App - Reset Password"

per = SendGrid::Personalization.new
per.to = SendGrid::Email.new(email: user.email, name: user.name)
per.substitutions = SendGrid::Substitution.new(key: "user_name", value: user.name.split(" ")[0].capitalize)
per.substitutions = SendGrid::Substitution.new(key: "reset_link", value: some_func(token, email: user.email))

content = Content.new(type: 'text/html', value: 'test')

email = SendGrid::Mail.new(from, subject)
email.personalizations = per

email.contents = SendGrid::Content.new(type: 'text/plain', value: "Hi #{user.name}.. Click the following link to reset your password.. #{function_reset(token, email: user.email)}... This link will expire in two hours.. If you did not request your password to be reset, please ignore this email and your password will stay as it is.")

email.template_id = "6ede18bb-2eba-4958-8a57-43a58a559a0a"


response = @@sg.client.mail._('send').post(request_body: email.to_json)

puts response.status_code
puts response.body
puts response.headers

from sendgrid-ruby.

thinkingserious avatar thinkingserious commented on August 17, 2024

Please try this:

email = SendGrid::Mail.new
email.from = SendGrid::Email.new(email: '[email protected]')
email.subject = "App - Reset Password"

per = SendGrid::Personalization.new
per.to = SendGrid::Email.new(email: user.email, name: user.name)
per.substitutions = SendGrid::Substitution.new(key: "user_name", value: user.name.split(" ")[0].capitalize)
per.substitutions = SendGrid::Substitution.new(key: "reset_link", value: some_func(token, email: user.email))

email.personalizations = per

email.contents = Content.new(type: 'text/html', value: 'test')
email.contents = SendGrid::Content.new(type: 'text/plain', value: "Hi #{user.name}.. Click the following link to reset your password.. #{function_reset(token, email: user.email)}... This link will expire in two hours.. If you did not request your password to be reset, please ignore this email and your password will stay as it is.")

email.template_id = "6ede18bb-2eba-4958-8a57-43a58a559a0a"

response = @@sg.client.mail._('send').post(request_body: email.to_json)

puts response.status_code
puts response.body
puts response.headers

from sendgrid-ruby.

34code avatar 34code commented on August 17, 2024

issue can be closed -- but I hope this helps someone else searching on google as documentation is pretty sparse right now around the new API..

from sendgrid-ruby.

thinkingserious avatar thinkingserious commented on August 17, 2024

Thanks @34code,

I've added a short term baby step of providing a simple working template example to the docs in the repo to our backlog. I should be able to get that implemented within a few sprints.

Ideally, you should not have to worry about the ordering the content objects. I will address than in the next iteration of the mail helper.

from sendgrid-ruby.

ravirakesh avatar ravirakesh commented on August 17, 2024

I am getting response like :--

@Body="",
@headers=
{"server"=>["nginx"],
"date"=>["Mon, 10 Apr 2017 13:06:27 GMT"],
"content-type"=>["text/plain; charset=utf-8"],
"content-length"=>["0"],
"connection"=>["close"],
"x-message-id"=>["8mw9nktNSBSGQnrHkXny8Q"],
"x-frame-options"=>["DENY"],
"access-control-allow-origin"=>["https://sendgrid.api-docs.io"],
"access-control-allow-methods"=>["POST"],
"access-control-allow-headers"=>["Authorization, Content-Type, On-behalf-of, x-sg-elas-acl"],
"access-control-max-age"=>["600"],
"x-no-cors-reason"=>["https://sendgrid.com/docs/Classroom/Basics/API/cors.html"]},
@status_code="202">

I didn't get why?

from sendgrid-ruby.

thinkingserious avatar thinkingserious commented on August 17, 2024

Hello @ravirakesh,

That looks like a successful response. Here is a complete list of our status codes.

In this particular case, the 202 status code means that your message is both valid, and queued to be delivered.

The response body should be empty.

The x-message-id helps you to track this particular message when used in conjunction with our Event Webhook.

Please let me know if you have further questions.

Thanks!

With Best Regards,

Elmer

from sendgrid-ruby.

ravirakesh avatar ravirakesh commented on August 17, 2024

Hi Elmer,
I have verified that when I am sending mail on send-grid got response but I want to know how to manage object.
example if I send @user.email and using in template then how can i get actual response
Thanks,
Ravishankar

from sendgrid-ruby.

ravirakesh avatar ravirakesh commented on August 17, 2024

What kind of significance in x-message_id , and we are passing object in json but in response direct object is coming.

{"server"=>["nginx"],
"date"=>["Tue, 11 Apr 2017 13:44:44 GMT"],
"content-type"=>["text/plain; charset=utf-8"],
"content-length"=>["0"],
"connection"=>["close"],
"x-message-id"=>["-ybjzAnqRVSTa9HG-bW9Fw"],
"x-frame-options"=>["DENY"],
"access-control-allow-origin"=>["https://sendgrid.api-docs.io"],
"access-control-allow-methods"=>["POST"],
"access-control-allow-headers"=>["Authorization, Content-Type, On-behalf-of, x-sg-elas-acl"],
"access-control-max-age"=>["600"],
"x-no-cors-reason"=>["https://sendgrid.com/docs/Classroom/Basics/API/cors.html"]}

How we can get success response 200 ok, currently I am getting 202 response and we got message from send-grid.
WHY?

from sendgrid-ruby.

thinkingserious avatar thinkingserious commented on August 17, 2024

Hello @ravirakesh,

You don't really need to do anything with the return object if your email is successfully sent. You will want to save the x-message-id if you are doing email tracking, but otherwise you can ignore it.

In the case of an error, you will want to do this.

I hope that helps.

  • Elmer

from sendgrid-ruby.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.