Skip to content
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

Closed
34code opened this issue Jul 25, 2016 · 14 comments
Closed

sending email with multiple content types (text+html) fails #81

34code opened this issue Jul 25, 2016 · 14 comments
Labels
status: help wanted requesting help from the community type: question question directed at the library

Comments

@34code
Copy link

34code commented Jul 25, 2016

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

    mail = Mail.new()
    mail.from = Email.new(email: "test@example.com")
    mail.subject = "Hello World from the SendGrid Ruby Library"
    personalization = Personalization.new()
    personalization.to = Email.new(email: "test@example.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>")

Technical details:

  • sendgrid-ruby Version: master
  • Ruby Version: 2.2
@thinkingserious thinkingserious added status: help wanted requesting help from the community type: question question directed at the library labels Jul 25, 2016
@thinkingserious
Copy link
Contributor

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?

@34code
Copy link
Author

34code commented Jul 25, 2016

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.

@thinkingserious
Copy link
Contributor

Awesome, thanks!

@34code
Copy link
Author

34code commented Jul 26, 2016

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

@thinkingserious
Copy link
Contributor

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

@34code
Copy link
Author

34code commented Jul 27, 2016

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

@34code
Copy link
Author

34code commented Jul 27, 2016

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..

@thinkingserious
Copy link
Contributor

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.

@34code
Copy link
Author

34code commented Jul 28, 2016

awesome, thanks!

@ravirakesh
Copy link

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?

@thinkingserious
Copy link
Contributor

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

@ravirakesh
Copy link

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

@ravirakesh
Copy link

ravirakesh commented Apr 11, 2017

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?

@thinkingserious
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted requesting help from the community type: question question directed at the library
Projects
None yet
Development

No branches or pull requests

3 participants