o
     #ia                     @   s   d dl Z d dlmZmZmZmZ d dlmZ d dlm	Z	 d dl
mZmZ d dlmZ d dlmZ d dlmZ e eZG d	d
 d
ZdS )    N)ListOptionalUnionTuple)settings)cache)EmailMultiAlternativesget_connection)render_to_string)SMTPException)GlobalSettingsc                   @   s   e Zd ZdZ					ddedeeee f dee deee  deee  d	eeeee	ef   d
eee  fddZ
dd Zdd Zddee defddZdededefddZdS )EmailWrappera:  
    Wrapper for sending HTML or templated emails using Django's email backend.

    Example:
        email = EmailWrapper(subject="Welcome!", to=["user@example.com"])
        context = {"name": "John"}
        success = email.send_with_template("emails/welcome.html", context)
        # Returns True or False
     Nsubjecttobodyccbccattachmentsreply_toc                 C   sZ   || _ t|tr|gn|| _|pd| _|pg | _|pg | _|p g | _|p%g | _| 	  d S )Nz&Please view this email in HTML format.)
r   
isinstancestrr   r   r   r   r   r   _load_email_config)selfr   r   r   r   r   r   r    r   G/var/www/GraceOrthoHospital/grace_ortho_care/utilities/email_wrapper.py__init__   s   





zEmailWrapper.__init__c                 C   s   t d}|stj }t d|d t|ddptj| _	t|ddp(ttdd| _
t|ddp5ttdd	| _t|d
dpBttdd| _|rLt|ddnttdd| _|r^t|dd| _dS ttdd| _dS )z;Load SMTP configuration from cache, DB, or Django settings.global_email_settingsi,  
smtp_emailNsmtp_passwordEMAIL_HOST_PASSWORD	smtp_host
EMAIL_HOSTzsmtp.gmail.com	smtp_port
EMAIL_PORTiK  smtp_use_tlsEMAIL_USE_TLSTsmtp_use_sslEMAIL_USE_SSLF)r   getr   objectsfirstsetgetattrr   DEFAULT_FROM_EMAIL
from_emailpasswordr!   r#   use_tlsuse_ssl)r   gsr   r   r   r   1   s   

(zEmailWrapper._load_email_configc              
   C   sP   zt | j| j| j| j| j| jddW S  ty' } z	t	d|   d}~ww )z4Return a live email connection using current config.F)hostportusernamer0   r1   r2   fail_silentlyz%Failed to establish SMTP connection: N)
r	   r!   r#   r/   r0   r1   r2   	Exceptionlogger	exception)r   er   r   r   r	   D   s   	zEmailWrapper.get_connectionhtml_messagereturnc              
   C   s  zJ|   }t| j| j| j| j| j| j| j|d}|r |	|d | j
D ]}|j|  q#| }|dkr?td| j  W dS td| j  W dS  tyi } ztd| j d	|  W Y d
}~dS d
}~w ty } ztd| j d	|  W Y d
}~dS d
}~ww )z6
        Send a plain + HTML multipart email.
        )r   r   r/   r   r   r   r   
connectionz	text/htmlr   zEmail sent successfully to Tz'Email send() returned 0 for recipients Fz"SMTP error while sending email to z: Nz(Unexpected error while sending email to )r	   r   r   r   r/   r   r   r   r   attach_alternativer   attachsendr9   infowarningr   errorr8   r:   )r   r<   r>   email
attachment
sent_countr;   r   r   r   rA   W   s>   
zEmailWrapper.sendtemplate_pathcontextc              
   C   sV   zt ||}| j|dW S  ty* } ztd| d|  W Y d}~dS d}~ww )z
        Render an HTML template and send the email.

        :param template_path: e.g. 'emails/welcome.html'
        :param context: Template context
        )r<   z#Failed to render or send template 'z': NF)r
   rA   r8   r9   r:   )r   rH   rI   r<   r;   r   r   r   send_with_template   s   
zEmailWrapper.send_with_template)r   NNNN)N)__name__
__module____qualname____doc__r   r   r   r   r   bytesr   r   r	   boolrA   dictrJ   r   r   r   r   r      s4    



+r   )loggingtypingr   r   r   r   django.confr   django.core.cacher   django.core.mailr   r	   django.template.loaderr
   smtplibr   utilities.modelsr   	getLoggerrK   r9   r   r   r   r   r   <module>   s    
