
    Kh                         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                   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          t          |dd          5 }|                                }d	d	d	           n# 1 swxY w Y   |                    d|          }|st%          j                    }|                    d           d| }|                    d|          }t+                      }| j        |d<   ||d<   d| |d<   |                    t/          |d                     t          |d          5 }t1          dd          }|                    |                                           d	d	d	           n# 1 swxY w Y   t5          j        |           |                    dd| d           |                    |           t;          j        || j        | j         d| j        | j                   d	{V  dd d!S # t          $ r$}t          d"tC          |                     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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_pathfhtml_templatehtml_contentdownload_linkmsg
attachmentpartes                       r   send_website_emailzEmailService.send_website_email   sm     O	? *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 '')) < :;;;mS7;;; )q !) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )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 h%% 4~>>  !2!23334 4 4 4 4 4 4 4 4 4 4 4 4 4 4 "4(((OO%::::   JJt /)^++           $0IJJJ 	? 	? 	?=SVV==>>>	?s   CL' AD, L' ,D00L' 3D04A	L' =FL' F""L' %F"&B6L' 8J L'  J$$L' 'J$(A>L' '
M1MMr   )__name__
__module____qualname__r   rH   r   r   rW    r   r   r   r      s        4 4 4 Y? Y?Y? Y? 	Y?
 Y? 
c3hY? Y? Y? Y? Y? Y?r   r   )__doc__typingr   r   rF   email.mime.multipartr   email.mime.textr   email.mime.baser   r   r   pathlibr	   core.configr
   r   r[   r   r   <module>rc      s    
             . . . . . . $ $ $ $ $ $ $ $ $ $ $ $                        `? `? `? `? `? `? `? `? `? `?r   