sendgrid / sendgrid-python

The Official Twilio SendGrid Python API Library
https://sendgrid.com
MIT License
1.55k stars 714 forks source link

Send a Single Email to a Single Recipient #224

Closed thinkingserious closed 7 years ago

thinkingserious commented 8 years ago

Acceptance Criteria:

Reference:

dibyadas commented 7 years ago

@thinkingserious I would like to work on this issue. Can you give me some pointers , on how to proceed?

thinkingserious commented 7 years ago

@dibyadas,

Awesome!

The first step will be to create a proposal similar to this so that the community can review and provide feedback.

Thanks!

Elmer

dibyadas commented 7 years ago

@thinkingserious So, I just went through links for reference given in the issue description. But I still have some doubts. Can you help me with, what issue is this ticket addressing? And , whose response payload we are referring to and the kind of data validation we are talking about?

Thanks!

Dibya

thinkingserious commented 7 years ago

Hi @dibyadas,

Thank you for the follow up!

  1. For the Response object, I'm referring to something like this. It's a wrapper around HTTPResponse. The idea is that we can then customize the return object for specific SendGrid functionality. For now, it should have a status_code, body and headers. Right now, we return the original object directly.

  2. The data validation portion does not have to be included in this iteration. But we should plan for it. The idea is that the helper should surface API errors where possible before the message is sent.

  3. For error handling please see: https://github.com/sendgrid/sendgrid-python/issues/193

At a high level, we want a request object builder that we can pass to a function that will send the email.

For the use case described in the example, I'm thinking of something along the lines of this (both cases should be possible):

import sendgrid

sg = sendgrid.SendGridClient('SENDGRID_API_KEY')

from_email = Email('Test User', 'test@example.com')
to_email = Email('Test User', 'test@example.com')
subject = 'Hello World from the SendGrid Python Library!'
plain_text_content = 'Hello, Email!'
html_content = '<strong>Hello, Email!</strong>'
msg = create_single_email(from_email, to_email, subject, plain_text_content, html_content)

response = sg.send(msg)

or

import sendgrid
from sendgrid import SendGridMessage

sg = sendgrid.SendGridClient('SENDGRID_API_KEY')

msg = SendGridMessage()
msg.from_email = Email('Test User', 'test@example.com')
msg.to_email = Email('Test User', 'test@example.com')
msg.subject = 'Hello World from the SendGrid Python Library!'
msg.plain_text_content = 'Hello, Email!'
msg.html_content = '<strong>Hello, Email!</strong>'

response = sg.send(msg)

The first case demonstrates the use of the SendGridMessage builder create_single_email which this issue addresses.

The second case demonstrates the custom building of the SendGridMessage builder to allow for more advanced usage.

dibyadas commented 7 years ago

@thinkingserious Thanks a lot for your informative explanation! So what I have gathered from this is the following :- We can define a class which will act as a wrapper around the HTTPResponse Object, as you mentioned. So instead of returning the object directly, We can have :-

......
    except timeout as e:
            raise SendGridClientError(408, 'Request timeout')
    response_obj = SendGridResponse(response.read() response.getcode(), response.getheaders())
    return response_obj
......

Here the SendGridResponse is the class that we going to add.

And for the exception handling, we can also define SendGrid custom exceptions which can more be informative than standard python exceptions.

Does this look good?

Thanks!

Regards, Dibya

thinkingserious commented 7 years ago

Looks good :)

Thanks @dibyadas!

dibyadas commented 7 years ago

@thinkingserious Once I am ready with proposed modifications, I will make a PR. :smile:

Thanks!

thinkingserious commented 7 years ago

Awesome, thanks!

dibyadas commented 7 years ago

@thinkingserious I was having some problems registering an account in SendGrid. It said that my profile has been flagged as high risk. :confused: Am I doing something wrong there? Can you help me with this?

Thanks,

Regards, Dibya

thinkingserious commented 7 years ago

What's your SendGrid username?

dibyadas commented 7 years ago

@thinkingserious dibyadas

dibyadas commented 7 years ago

@thinkingserious Thanks! I got a response from SendGrid support to activate my account. :smile:

dibyadas commented 7 years ago

@thinkingserious Unfortunately, I did not get any reply from Bogdan from SendGrid support. But then I remembered, I have Github Student Pack. So I registered a free account in it and now its working. :smile:

I cloned this repository and went through the code base. I saw that the response object returned here, for ex. , response = sg.client.mail.send.post(request_body=mail.get()) is a object of the Response class of python_http_client module. This class already has the status_code , body and headers of the response.

I also checked out this tree of the repo. It has a bit different code base. Am I missing something? Where should I make the changes? I installed the sendgrid module through pip. python version :- 3.5.2 pip version :- 9.0.1 sendgrid version :- 4.1.0

Sorry for the trouble!

Thanks, Regards, Dibya

thinkingserious commented 7 years ago

Please branch off the current master (v4.1.0). That tree you are pointed to is no longer relevant.

dibyadas commented 7 years ago

@thinkingserious I guess then the Response object of the email is sorted. I just need to add the single email sending feature like you mentioned and the exception handling.

Thanks, Regards, Dibya

thinkingserious commented 7 years ago

Correct, thanks!

thinkingserious commented 7 years ago

This has been moved here.

rukmanigupta commented 3 years ago

What's your SendGrid username?

Hi @thinkingserious I am also facing the same issue, I just opened my new website as a Job portal & when I am registering it is not showing dashboard as it is saying that my profile is flagged as high risk, I don't why.

Plz help.

my registered email is admin@myjobpocket.com