Below is my code for two modules. I'm trying to work with sending emails using classes.
---------------------------------------------------------------------------------------

TEMPLATE.PY

```
import os

FILE_PATH=os.path.abspath(__file__)
BASE_DIR=os.path.dirname(FILE_PATH)
TEMPLATE_DIR=os.path.join(BASE_DIR,'templates')

class Template:
    template_name=""
    context=None

    def __init__(self,template_name,context,*args,**kwargs):
        self.template_name=template_name
        self.context=context

    def get_template(self):
        template_path=os.path.join(TEMPLATE_DIR,self.template_name)
        if not os.path.exists(template_path):
            raise Exception('Path is non-existent!')
        template_string=''
        with open(template_path,'r') as f:
            template_string=f.read()
        return template_string

    def render(self,context=None):
        render_ctx=context
        if(self.context!=None):
            render_ctx=self.context
        if(not isinstance(render_ctx,dict)):
            render_ctx={}
        template_string=self.get_template()
        return template_string.format(**render_ctx)

```

---

SEND\_MAIL.PY

```
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from templates import Template

username='abc@gmail.com'
password=''

class Emailer():
    subject=''
    context={}
    to_emails=[]
    has_html=False
    test_send=False
    from_email='Some email <ashuradirty@gmaill.com'
    template_html=None
    template_name=None

    def __init__(self,subject='',
                 context={},
                 to_emails=[],
                 test_send=True,
                 template_html=None,
                 template_name=None):
        self.subject=subject
        self.context=context
        assert(isinstance(to_emails,list))
        self.to_emails=to_emails
        self.test_send=test_send
        if template_name==None and template_html==None:
            raise Exception('Template required!')
        if(template_html!=None):
            self.template_html=template_html
            self.has_html=True
        self.template_name=template_name

    def format_msg(self):
        msg=MIMEMultipart('alternative')
        msg['From']=self.from_email
        msg['To']=','.join(self.to_emails)
        msg['Subject']=self.subject

        if(self.template_name!=None):
            tmpl_string=Template(template_name=self.template_name,context=self.context)
            txt_part=MIMEText(tmpl_string.render(),'plain')
            print(txt_part)
            msg.attach(txt_part)
        if(self.template_html!=None):
            tmpl_string=Template(template_name=self.template_name,context=self.context)
            html_part=MIMEText(tmpl_string.render(),'html')
            print(html_part)
            msg.attach(html_part)
        msg_str=msg.as_string()
        return msg_str

    def send(self):
        msg=self.format_msg()
        did_send=False

        if not self.test_send:
            with smtplib.SMTP(host='smtp.gmail.com',port=587) as server:
                server.ehlo()
                server.starttls()
                server.login(username,password)
                try:
                    server.sendmail(from_email,to_emails,msg)
                    did_send=True
                except:
                    did_send=False
        return did_send

emlr=Emailer(subject='something',
             context={'name':'somename'},
             to_emails=['abc@gmail.com'],
             template_name='hello.txt')
print(emlr.send())

```

---

Below is the error:
-------------------

> 
> /Library/Frameworks/Python.framework/Versions/3.10/bin/python3 "/Users/**********/Desktop/Back2Python/Day 11/send\_mail.py"
> Traceback (most recent call last):
> File "/Users/**********/Desktop/Back2Python/Day 11/send\_mail.py", line 76, in 
> print(emlr.send())
> File "/Users/**********/Desktop/Back2Python/Day 11/send\_mail.py", line 57, in send
> msg=self.format\_msg()
> File "/Users/**********/Desktop/Back2Python/Day 11/send\_mail.py", line 45, in format\_msg
> txt\_part=MIMEText(tmpl\_string.render(),'plain')
> File "/Users/\*\*\*\*\*\*\*\*\*\*/Desktop/Back2Python/Day 11/templates.py", line 31, in render
> return template\_string.format(\*\*render\_ctx)
> IndexError: Replacement index 0 out of range for positional args tuple
> 
> 
> 

Process finished with exit code 1

---

The argument 'context' in the constructor isn't unpacking with the format method of the 'template' module. What am I doing wrong? I learning Python and this is my day 11. Sorry if I've made a clumsy mistake.