sendgrid / sendgrid-python

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

__init__() got an unexpected keyword argument 'from_email' #932

Closed raghunit closed 4 years ago

raghunit commented 4 years ago

While trying to use SendGrid in google app engine I am facing below error

_init__() got an unexpected keyword argument 'from_email'

But the same code works fine in the other app engine projects.

Below is my code.

from sendgrid import SendGridAPIClient from sendgrid.helpers.mail import Mail

SENDGRID_API_KEY = 'my key' SENDGRID_SENDER = 'myeamilid@app.com'

f_email = 'myeamilid@app.com' u_email = 'raghu.nit@gmail.com' message = Mail(from_email=f_email, to_emails='{},'.format(u_email), subject='Test subject', html_content=' Test body')

try: sg = SendGridAPIClient('my sendgrid key sdfsdj fksdjk') response = sg.send(message)

except Exception as e: print(e.message)

thinkingserious commented 4 years ago

Hello @raghunit

Could you please verify which version of this helper library you have installed on your Google App Engine server? I believe the from_email keyword has been available since version 5 of this helper library.

Thank you!

With best regards,

Elmer

raghunit commented 4 years ago

Hello @raghunit

Could you please verify which version of this helper library you have installed on your Google App Engine server? I believe the from_email keyword has been available since version 5 of this helper library.

Thank you!

With best regards,

Elmer

Hi Elmer,

I am using sendgrid==6.4.5. Is this what you are checking for. Let me know if any further information is required Am I missing any other settings at Google App Engine, which I need to enable to send an email using SendGrid.

Thank you in Advance for your time and help

thinkingserious commented 4 years ago

Hello @raghunit,

Based on this issue, I'm leaning towards this being an import issue, though I was unable to reproduce with the following code:

import sendgrid
import os
from sendgrid.helpers.mail import Mail, Email, To, Content

sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("email@example.com")
to_email = To("email@example.com")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
#mail = Mail(from_email=from_email, subject=subject, to_emails=to_email, plain_text_content=content)
mail = Mail(from_email, to_email, subject, content)
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)

Note that I tested the commented out line also.

Could you please modify your code according the example I provided and let me know the result? Thanks!

raghunit commented 4 years ago

Hello @raghunit,

Based on this issue, I'm leaning towards this being an import issue, though I was unable to reproduce with the following code:

import sendgrid
import os
from sendgrid.helpers.mail import Mail, Email, To, Content

sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("email@example.com")
to_email = To("email@example.com")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
#mail = Mail(from_email=from_email, subject=subject, to_emails=to_email, plain_text_content=content)
mail = Mail(from_email, to_email, subject, content)
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)

Note that I tested the commented out line also.

Could you please modify your code according the example I provided and let me know the result? Thanks!

Tried the given code works fine on my laptop. But when used in google app engine program gives me below error

mail = Mail(from_email, to_email, subject, content) TypeError: init() takes from 1 to 2 positional arguments but 5 were given

thinkingserious commented 4 years ago

Please try sendgrid.helpers.mail.Mail( instead of Mail(. Maybe there is a conflict with another package on Google App Engine that is also named Mail.

raghunit commented 4 years ago

Please try sendgrid.helpers.mail.Mail( instead of Mail(. Maybe there is a conflict with another package on Google App Engine that is also named Mail.

Hi Elmer,

Thank you yeah this worked. Thank you very much for your time.

Thanks Raghu

raghunit commented 4 years ago

Please try sendgrid.helpers.mail.Mail( instead of Mail(. Maybe there is a conflict with another package on Google App Engine that is also named Mail.

Hi Elmer,

Can you please help me how to modify the existing code to use CC option in sending email as well as how to send the same email to multiple recipients

Thanks Raghu

thinkingserious commented 4 years ago

Hello @raghunit,

Thanks for the follow up!

As for your question regarding adding a CC, please take a look at this example. And here is an example for sending the same email to multiple recipients.

Thanks!

With best regards,

Elmer