- Make sure you have installed the "sendgrid" package using pip:
pip install sendgrid
- Import the SendGridAPIClient and Mail classes in your Django file:
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
- Use the SendGridAPIClient to send emails in your Django application:
message = Mail(
from_email='[email protected]',
to_emails='[email protected]',
subject='Subject',
plain_text_content='Plain Text Content',
html_content='<strong>HTML Content</strong>'
)
try:
sg = SendGridAPIClient('SENDGRID_API_KEY')
response = sg.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(e.message)