-
Notifications
You must be signed in to change notification settings - Fork 322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sending email with multiple content types (text+html) fails #81
Comments
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:
code used in rails5
|
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="", 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 Please let me know if you have further questions. Thanks! With Best Regards, Elmer |
Hi Elmer, |
What kind of significance in x-message_id , and we are passing object in json but in response direct object is coming. {"server"=>["nginx"], How we can get success response 200 ok, currently I am getting 202 response and we got message from send-grid. |
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 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:
The text was updated successfully, but these errors were encountered: