Closed 34code closed 8 years ago
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: "dx@sendgrid.com")
mail.subject = "Hello World from the SendGrid Ruby Library"
personalization = Personalization.new()
personalization.to = Email.new(email: "elmer@sendgrid.com")
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?
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.
Awesome, thanks!
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: 'team@email.com')
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
Please try this:
email = SendGrid::Mail.new
email.from = SendGrid::Email.new(email: 'team@email.com')
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
ok that worked but I had to switch the lines and put the line with 'text/plain' before the line with 'text/html'...
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..
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.
awesome, thanks!
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?
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
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
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?
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.
Issue Summary
sending email with multiple content types (text+html) fails. Error received is "expected object but received string for content.0" .. not sure how to properly specify the email as there is no documentation anywhere to do it properly with v3 api.
Steps to Reproduce
Technical details: