
    ɧ-i                         d Z ddlmZmZ ddlZddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ  G d
 d          ZdS )zI
Email Service
Handles email sending functionality with async operations
    )DictAnyN)MIMEMultipart)MIMEText)MIMEBase)encoders)Path)settingsc                   D    e Zd Zd Z	 d	dededededeeef         f
dZdS )
EmailServicec                     t           j        | _        t           j        | _        t           j        | _        t           j        | _        d S N)r
   smtp_server	smtp_portsmtp_usernamesmtp_password)selfs    E/var/www/html/web-builder-api.evdpl.com/src/services/email_service.py__init__zEmailService.__init__   s3    #/!+%3%3    Nemailfolder_namewebsite_namehost_urlreturnc                 4  K   	 t          | j        | j        g          st          d          t	          d          |z  }|                                st          d          t	          d          | dz  }|j                            d           |                                sdd	l}|	                    |d
|j
                  5 }|                    d          D ]A}	|	                                r+|	                    |          }
|                    |	|
           B	 d	d	d	           n# 1 swxY w Y   n	 t	          d          }|                                st          d          dd	l}|                    |dd          4 d	{V }|                                 d	{V }d	d	d	          d	{V  n# 1 d	{V swxY w Y   |                    d|          }|st'          j                    }|                    d           d| }|                    d|          }t-                      }| j        |d<   ||d<   d| |d<   |                    t1          |d                     |                    |d          4 d	{V }t3          dd          }|                                 d	{V }|                    |           d	d	d	          d	{V  n# 1 d	{V swxY w Y   t7          j        |           |                    dd| d           |                    |           t=          j        || j         | j!        d| j        | j                   d	{V  dd d!S # t          $ r$}t          d"tE          |                     d	}~ww xY w)#z@
        Send website ZIP via email using HTML template
        zSMTP credentials not configuredztemplates/generatedzWebsite folder not found	downloadsz.zipT)exist_okr   Nw*z#templates/email/email_template.htmlzEmail template not foundrzutf-8)encodingz{{SITE_NAME}}/z/api/download/z{{DOWNLOAD_LINK}}FromTozYour Restaurant Website - Subjecthtmlrbapplicationzoctet-streamzContent-Dispositionzattachment; filename= )hostnameport	start_tlsusernamepasswordzEmail sent successfully)successmessagezEmail sending failed: )#allr   r   	Exceptionr	   existsparentmkdirzipfileZipFileZIP_DEFLATEDrglobis_filerelative_towriteaiofilesopenreadreplacer
   get_current_domainrstripr   attachr   r   set_payloadr   encode_base64
add_header
aiosmtplibsendr   r   str)r   r   r   r   r   website_pathzip_pathr6   zipf	file_patharcnametemplate_pathr=   fhtml_templatehtml_contentdownload_linkmsg
attachmentpartzip_contentes                         r   send_website_emailzEmailService.send_website_email   sT     Q	? *D,>?@@ C ABBB   566DL&&(( < :;;;K((k+?+?+??HO!!4!000 ??$$ 
__XsG4HII ;T%1%7%7%<%< ; ;	$,,.. ;&/&;&;L&I&IG JJy':::;; ; ; ; ; ; ; ; ; ; ; ; ; ; ;  !!FGGM '')) < :;;;OOO}}]C'}JJ / / / / / / /a&'ffhh/ / / / / / / / / / / / / / / / / / / / / / / / / / / )00,OOL  9#688's33PP;PPM'//0C]SSL  //C,CKCIH,HHC	N JJxf55666  }}Xt44 . . . . . . .
~>>$.OO$5$5555555  ---. . . . . . . . . . . . . . . . . . . . . . . . . . .
 "4(((OO%::::   JJt /)^++           $0IJJJ 	? 	? 	?=SVV==>>>	?s   CM) AD, M) ,D00M) 3D04AM) F:(M) :
GM) GCM) 
A K
M) 
K&&M) )K&*A>M) )
N3NNr   )__name__
__module____qualname__r   rI   r   r   rY    r   r   r   r      s        4 4 4 [? [?[? [? 	[?
 [? 
c3h[? [? [? [? [? [?r   r   )__doc__typingr   r   rG   email.mime.multipartr   email.mime.textr   email.mime.baser   r   r   pathlibr	   core.configr
   r   r]   r   r   <module>re      s    
             . . . . . . $ $ $ $ $ $ $ $ $ $ $ $                        b? b? b? b? b? b? b? b? b? b?r   