Skip to content

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

@utkarsh1996

Description

@utkarsh1996

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" : "[email protected]"} or {"email" : "[email protected]", "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: '[email protected]')
to = Email.new(email: '[email protected]')
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(api_key: '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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions