Skip to main content

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.

Comments

Popular posts from this blog

Pgpool PgBouncer Postgresql streaming replication, load balancing and administration

The term scalability refers to the ability of a software system to grow as the business that uses it grows. PostgreSQL provides some features to help you build scalable solutions, but strictly speaking, PostgreSQL itself is not scalable. It can effectively use the following resources from one computer. Now, we will show you some configurations that may be useful for your use case. However, this can be problematic when distributing the database solution to multiple computers, because the standard PostgreSQL server can only run on a single computer. In this article, we will study different extension schemes and their implementation in PostgreSQL. Replication can be used in many expansion scenarios. Its main purpose is to create and maintain a backup database when the system fails. This is especially true for physical replication. However, replication can also be used to improve the performance of PostgreSQL-based solutions. Sometimes third-party tools can be used to implement complex exp

Tekstverwerking python Text processing python SpaCy, TensorFlow, NLTK, Allen-NLP, Stanford-NLP

 Dit post maakt gebruik van spaCy, een populaire Python-bibliotheek die de taalgegevens en algoritmen bevat die je nodig hebt om teksten in natuurlijke taal te verwerken. Zoals u in dit post zult leren, is spaCy gemakkelijk te gebruiken omdat het containerobjecten biedt die elementen van natuurlijke taalteksten vertegenwoordigen, zoals zinnen en woorden. Deze objecten hebben op hun beurt attributen die taalkenmerken vertegenwoordigen, zoals delen van spraak. Op het moment van schrijven bood spaCy voorgetrainde modellen aan voor Engels, Duits, Grieks, Spaans, Frans, Italiaans, Litouws, Noors Bokmål, Nederlands, Portugees en meerdere talen gecombineerd. Bovendien biedt spaCy ingebouwde visualizers die u programmatisch kunt aanroepen om een grafische weergave van de syntactische structuur van een zin of benoemde entiteiten in een document te genereren.   De spaCy-bibliotheek ondersteunt ook native geavanceerde NLP-functies die andere populaire NLP-bibliotheken voor Python niet hebben. Spa

Google Closure

   Closure Library De Closure-bibliotheek is een JavaScript-bibliotheek, vergelijkbaar met andere moderne producten zoals jQuery, Angular, Vue.js, Dojo en MooTools. De coderingsstijl en het gebruik van opmerkingen in de Closure-bibliotheek zijn op maat gemaakt voor Closure Compiler. In vergelijking met andere JavaScript-bibliotheken is het de belangrijkste onderscheidende factor van Closure Compiler. Een eenvoudig compressie-experiment ontdekte dat wanneer Closure Compiler wordt gebruikt in plaats van YUI Compressor, de Closure Lib-code met 85% kan worden verminderd, wat een enorme impact kan hebben op de codecompressiecapaciteit van de compiler.    De implementatie van de  closure bibliotheek richt zich ook op leesbaarheid en prestaties. Wees zuinig bij het maken van objecten, maar wees genereus bij het benoemen en opnemen van objecten. Het heeft ook een prachtig gebeurtenissysteem, ondersteuning voor klassen en overerving en verschillende UI-componenten, waaronder bijvoorbeeld een ri