DEV Community

Mohamed Ajmal P
Mohamed Ajmal P

Posted on

To create an email template for a time off approval request in Django

To create an email template for a time off approval request in Django, you can use the django.core.mail module to define the email's subject, message, and recipient. Here is an example of a time off approval email template in Django:

from django.core.mail import EmailMessage

def send_time_off_approval_email(employee, start_date, end_date):
  subject = 'Time off request approved'
  message = (
    f'Dear {employee.name},\n\n'
    f'Your time off request from {start_date} to {end_date} has been approved. '
    'Please let your manager know if you have any questions.\n\n'
    'Best regards,\n'
    'HR team'
  )
  to = [employee.email]
  email = EmailMessage(subject, message, to=to)
  email.send()

Enter fullscreen mode Exit fullscreen mode

In this example, the send_time_off_approval_email function takes the employee's name, start date, and end date as arguments and uses them to create the email's subject and message. The function then sends the email using Django's EmailMessage class and the send method.

To use this template in your Django application, you would call the send_time_off_approval_email function whenever an employee's time off request is approved, passing in the employee's information and the start and end dates of the time off. For example:

send_time_off_approval_email(employee, '2022-12-01', '2022-12-05')

Enter fullscreen mode Exit fullscreen mode

This would send an email to the employee with the subject "Time off request approved" and a message thanking them for their time off request and letting them know that it has been approved.

Top comments (0)