sendgrid / sendgrid-ruby

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

Error in using the Mail Helper Class in sending mail #438

Closed utkarsh1996 closed 3 years ago

utkarsh1996 commented 3 years ago

With the example of Mail Helper Class, I implemented in code and got this particular error

400 {"errors":[{"message":"The from object must be provided for every email send. It is an object that requires the email parameter, but may also contain a name parameter. e.g. {\"email\" : \"example@example.com\"} or {\"email\" : \"example@example.com\", \"name\" : \"Example Recipient\"}.","field":"from.email","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.from"},{"message":"The personalizations field is required and must have at least one personalization.","field":"personalizations","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#-Personalizations-Errors"},{"message":"Unless a valid template_id is provided, the content parameter is required. There must be at least one defined content block. We typically suggest both text/plain and text/html blocks are included, but only one block is required.","field":"content","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.content"}]} {"server"=>["nginx"], "date"=>["Thu, 24 Sep 2020 05:12:50 GMT"], "content-type"=>["application/json"], "content-length"=>["980"], "connection"=>["close"], "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"]}

Even though I gave all the required info like from, to, content.

My code: from = Email.new(email: 'testmail@gmail.com') to = Email.new(email: 'totestmail@gmail.com') subject = 'Sending with SendGrid is Fun' content = Content.new(type: 'text/plain', value: 'and easy to do anywhere, even with Ruby') mail = Mail.new(from, subject, to, content) sg = SendGrid::API.new(apikey: 'my API key') response = sg.client.mail.('send').post(request_body: mail.to_json) //giving error on this line puts response.status_code puts response.body puts response.headers

futura commented 3 years ago

I got the same result. Was this ever resolved?