o
    }¾8Z+  ã                   @   sø   d dl mZ ddlZddlZddlmZ zddlmZ W n ey+   ddl	mZ Y nw zddl
Z
W n eyA   ddlmZ
 Y nw ddlmZ ddlmZ ddlmZ dd	lmZ ddlZdd
lmZmZmZmZmZmZmZmZ G dd„ deƒZdS )é   )Ú__version__é    N)ÚMIMEBase)Ú	HTTPError)Úsettings)ÚImproperlyConfigured)ÚEmailMultiAlternatives)ÚBaseEmailBackend)Ú
AttachmentÚCategoryÚContentÚ	CustomArgÚEmailÚMailÚPersonalizationÚSubstitutionc                       s2   e Zd ZdZd	‡ fdd„	Zdd„ Zdd„ Z‡  ZS )
ÚSendGridBackendz"
    SendGrid Web API Backend
    Fc                    sz   t t| ƒjdd|i|¤Ž d|v r|d | _nttdd ƒ| _| js%tdƒ‚tj| jd�| _	d 
t¡| _| j| j	jjd< d S )	NÚfail_silentlyÚapi_keyÚSENDGRID_API_KEYzA
                SENDGRID_API_KEY must be declared in settings.py)Úapikeyzsendgrid/{0};djangoz
User-agent© )Úsuperr   Ú__init__r   Úgetattrr   r   ÚsendgridÚSendGridAPIClientÚsgÚformatr   ÚversionÚclientÚrequest_headers)Úselfr   Úkwargs©Ú	__class__r   ú@/var/www/html/env/lib/python3.10/site-packages/sgbackend/mail.pyr   '   s   ÿÿzSendGridBackend.__init__c                 C   sp   |sdS d}|D ]-}|   |¡}z| jjjjj|d� |d7 }W q ty5 } z
| js+‚ W Y d}~qd}~ww |S )z
        Comments
        Nr   )Úrequest_bodyr   )Ú_build_sg_mailr   r    ÚmailÚsendÚpostr   r   )r"   ÚemailsÚcountÚemailr)   Úer   r   r&   Úsend_messages7   s   
ÿ€ÿzSendGridBackend.send_messagesc                 C   s$  t ƒ }t |j¡\}}|sd }| t||ƒ¡ | |j¡ tƒ }|j	D ]	}| 
t|ƒ¡ q#|jD ]	}| t|ƒ¡ q0|jD ]	}| t|ƒ¡ q=| |j¡ | td|jƒ¡ t|tƒrt|jD ]}|d dkrr| t|d |d ƒ¡ q^n|jdkr�g |_| tddƒ¡ | td|jƒ¡ t|dƒrŸ|jD ]	}| t|ƒ¡ q•t|dƒr¶|j ¡ D ]\}	}
| t|	|
ƒ¡ q©t|d	ƒrØ| |j ¡ t|d
ƒrØ|j! ¡ D ]\}}| "t#||ƒ¡ qËd}|j$ ¡ D ]\}}| %¡ dkrì|}qß| &||i¡ qß|j'�st|dƒ�r|j'�r|j'd }|�r*t |¡\}}|�r |�r | (t||ƒ¡ n
|�r*| (t|ƒ¡ |j)D ][}t|t*ƒ�rPt+ƒ }| ,| -¡ ¡ | .t/ 0| 1¡ ¡¡ | 2|¡ �q-t|t3ƒ�r‡t+ƒ }| ,|d ¡ t/ 0|d ¡}t4j5dk�rv| .t6|dƒ¡ n| .|¡ | 7|d ¡ | 2|¡ �q-| 8|¡ | 9¡ S )Nz
text/plainr   z	text/htmlr   Úhtmlú Ú
categoriesÚcustom_argsÚtemplate_idÚsubstitutionsÚ zreply-toÚreply_to)é   zutf-8é   ):r   Úrfc822Ú	parseaddrÚ
from_emailÚset_fromr   Úset_subjectÚsubjectr   ÚtoÚadd_toÚccÚadd_ccÚbccÚadd_bccÚadd_contentr   ÚbodyÚ
isinstancer   ÚalternativesÚcontent_subtypeÚcontentsÚhasattrr3   Úadd_categoryr   r4   ÚitemsÚadd_custom_argr   Úset_template_idr5   r6   Úadd_substitutionr   Úextra_headersÚlowerÚ
add_headerr8   Úset_reply_toÚattachmentsr   r
   Úset_filenameÚget_filenameÚset_contentÚbase64Ú	b64encodeÚget_payloadÚadd_attachmentÚtupleÚsysÚversion_infoÚstrÚset_typeÚadd_personalizationÚget)r"   r.   r)   Ú	from_namer=   Úpersonalizationr/   ÚaltÚcÚkÚvÚkeyÚvalueÚreply_to_stringÚreply_to_nameÚreply_to_emailÚ
attachmentÚattachÚbase64_attachmentr   r   r&   r(   I   s†   




€þ









€
zSendGridBackend._build_sg_mail)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r0   r(   Ú__classcell__r   r   r$   r&   r   #   s
    r   ) r   r   r[   r`   Úemail.mime.baser   Úurllib.errorr   ÚImportErrorÚurllib2r;   Úemail.utilsÚutilsÚdjango.confr   Údjango.core.exceptionsr   Údjango.core.mailr   Údjango.core.mail.backends.baser	   r   Úsendgrid.helpers.mailr
   r   r   r   r   r   r   r   r   r   r   r   r&   Ú<module>   s*    ÿÿ(