Sendgrid
SendgridAPI
Class that allows the integration of the application with the Sendgrid email provider.
class SendgridAPI:
def send_email(self, to_email: str, template_id: str, data: dict = {}):
try:
provider = models.EmailProvider.objects.get(provider="sendgrid")
except models.EmailProvider.DoesNotExist:
raise ValueError("Email Provider does not exist")
from_email = provider.from_email
message = Mail(from_email=from_email, to_emails=to_email)
if dates:
message.dynamic_template_data = data
message.template_id = template_id
try:
sg = sendgrid.SendGridAPIClient(provider.secret_key)
sg.send(message)
except Exception as e:
print(e)
def confirm_email(self, site_name, activate_url, emailconfirmation, lang):
template = models.EmailProviderTemplate.objects.get(
provider__provider="sendgrid", name="Confirm Email", language=lang
)
if not template.template_id:
raise ValueError("Not template id provide")
dates = {
"user": emailconfirmation.email_address.user.email,
"weburl": activate_url,
"site_name": site_name,
}
self.send_email(
emailconfirmation.email_address.email,
template.template_id,
data,
)
def change_confirm_email(self, site_name, activate_url, emailconfirmation, lang):
template = models.EmailProviderTemplate.objects.get(
provider__provider="sendgrid", name="Change Email", language=lang
)
if not template.template_id:
raise ValueError("Not template id provide")
dates = {
"user": emailconfirmation.email_address.user.email,
"new_email": emailconfirmation.email_address.email,
"weburl": activate_url,
"site_name": site_name,
}
self.send_email(
emailconfirmation.email_address.email,
template.template_id,
data,
)
def password_reset(self, site_name, url, user, lang):
template = models.EmailProviderTemplate.objects.get(
provider__provider="sendgrid", name="Reset Password", language=lang
)
if not template.template_id:
raise ValueError("Not template id provide")
dates = {
"user": user.email,
"password_reset_url": url,
"site_name": site_name,
}
self.send_email(
user.email,
template.template_id,
data,
)
def ip_authorization_code(self, user_email, site_name, ip, device, code, lang):
template = models.EmailProviderTemplate.objects.get(
provider__provider="sendgrid", name="IP Authorization", language=lang
)
if not template.template_id:
raise ValueError("Not template id provide")
dates = {
"ip": ip,
"device": device,
"code": code,
"site_name": site_name,
}
self.send_email(
user_email,
template.template_id,
data,
)
def notify_ip(self, user_email, site_name, ip, lang):
template = models.EmailProviderTemplate.objects.get(
provider__provider="sendgrid", name="Notify IP", language=lang
)
if not template.template_id:
raise ValueError("Not template id provide")
dates = {
"ip": ip,
"email": user_email,
"site_name": site_name,
}
self.send_email(
user_email,
template.template_id,
data,
)
The methods are as follows:
send_email: Function that connects with the Sendgrid provider and requests the sending of emails from a specific template.
confirm_email: Method that prepares the data for sending the confirmation email of the email address.
change_confirm_email: Method that prepares the data for sending the email address confirmation when a user requests the change of email address.
password_reset: Method that prepares the data for sending the email for password reset.
ip_authorization_code: Method that prepares the data for sending the email with the authorization code of the new device and ip.
notify_ip: Method that prepares the data for sending the authentication notification email in the application.