Sendgrid simple integration on localhost python TinyDB sendgridclient

 Configure mail to send email from a local host without any platform. Sometimes, you need a quick application to test an idea, but don't want to invest in a separate server with DKIM, SPF and other tools installed, but only need to test a business case for 2-3 months. The perfect tool for sendgrid that you can control via your laptop. There is no need to pay for the server and django, and there is no need to use a completely naive integration without a code solution. This API integration is at least 3 times cheaper than any other solution on the market, and it can be used for professional purposes.


Note this integration only works for small lists of about 200k-300k maximum. Otherwise database is too slow and updates are slower than postgres or other databases.


Register your account on the Sendgrid website.


Then, at this point in the API configuration wizard are out of date docs. Ignore it!

We use the code found on github. See the example below.




This is an old version in docs. Not working.





New code from github
import sendgrid
import os
from sendgrid.helpers.mail import *


sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("test@example.com")
to_email = To("test@example.com")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
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)




Trouble shutting



Errors like


Traceback (most recent call last):
File "sendgrid_bedrijven.py", line 67, in handle
self.send_form(contact)
File "sendgrid_bedrijven.py", line 130, in send_form content=body)
File "/Library/Python/2.7/site-packages/sendgrid/helpers/mail/mail.py", line 55, in __init__ personalization.add_to(to_email)
File "/Library/Python/2.7/site-packages/sendgrid/helpers/mail/personalization.py", line 39, in add_to self._tos.append(email.get())
AttributeError: 'str' object has no attribute 'get'


Or


Traceback (most recent call last):
File "test_sendgrid.py", line 13, in <module>
html_content='<strong>and easy to do anywhere, even with Python</strong>')
TypeError: __init__() got an unexpected keyword argument 'html_content'



Traceback (most recent call last):
File "test_sendgrid.py", line 11, in <module>
to_email = To("sergej.dergatsjev@gmail.com")
NameError: name 'To' is not defined






Coming from an old version of the package.
You have to remove it from your environment and install it again.




pip uninstall sendgrid
pip install sendgrid


Then you can get some error messages about authorization.

Traceback (most recent call last):
File "test_sendgrid.py", line 15, in <module>
response = sg.client.mail.send.post(request_body=mail.get())
File "/Library/Python/2.7/site-packages/python_http_client/client.py", line 277, in http_request
self._make_request(opener, request, timeout=timeout)
File "/Library/Python/2.7/site-packages/python_http_client/client.py", line 184, in _make_request
aise exc

python_http_client.exceptions.UnauthorizedError







# using SendGrid's Python Library

# https://github.com/sendgrid/sendgrid-python

import sendgrid
import os
from sendgrid.helpers.mail import *




sg = sendgrid.SendGridAPIClient(api_key="xxxx")
from_email = Email("e-marketing@auto-tweedehands.com")
to_email = To("sergej.dergatsjev@gmail.com")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
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)







When technical setup is done then you have to activate your account.







Dns host without domain




Then you can create script. DNS is installed.



rDNS Sendgrid


Because mails don't arrive well through cheap accounts from Sendgrid, take a pro account and there you can set reverse DNS and other things. Separate IP with your own reputation and rDNS that will help a lot.


Unfortunately, Sendgrid will not recommend it directly, although it is necessary.


Setting up reverse DNS on the IP address allows the mailbox provider to verify the sender when performing a reverse DNS lookup after receiving an email from you. When you update your DNS provider with the DNS records provided by SendGrid, and then send mail through your IP, the recipient's email service provider uses the A record (address record) to perform a reverse DNS lookup (rDNS). The A record maps your domain to your IP address. When the email provider looks up your A record, they will see your SendGrid IP address. When they look at your IP address, they will see rDNS that matches your A record. This round-robin check proves that your SendGrid IP is associated with your domain and your domain is associated with your SendGrid IP.







IP pool

The IP pool allows you to group dedicated SendGrid IP addresses. For example, you can create a separate pool for transactional emails and another pool for marketing emails. When sending marketing emails, please specify that you want to use the marketing IP pool. This allows you to maintain different reputations for different email traffic. A single IP address or range of IP addresses may be dedicated to an account to send email for multiple domains. The reputation of this IP is based on the combined performance of all senders who use it. The IP pool can only be used with IP addresses for which you have set up reverse DNS records. If no IP pool is specified for the email, it will use any available IP, including the pool address. Each user can create up to 10 different IP pools.
Log in to your SendGrid account. Navigate to settings and select the IP address. Find the IP address you want to activate and click Edit. Check Allow my account to use this IP address to send mail. Click Save.

Comments