????

Your IP : 3.14.141.163


Current Path : /opt/imunify360/venv/lib/python3.11/site-packages/clcommon/__pycache__/
Upload File :
Current File : //opt/imunify360/venv/lib/python3.11/site-packages/clcommon/__pycache__/mail_helper.cpython-311.pyc

�

I�Qg����ddlZddlZddlZddlZddlZddlmZm	Z	ddl
mZddlm
Z
ddlmZdZGd�de��ZGd	�d
��ZdS)�N)�base�	multipart)�
SysLogHandler)�Optional)�errorzAleksandr Shyshatskyc��eZdZdS)�MailSendFailedN)�__name__�
__module__�__qualname__���t/builddir/build/BUILD/imunify360-venv-2.5.0/opt/imunify360/venv/lib/python3.11/site-packages/clcommon/mail_helper.pyr	r	s�������Drr	c��eZdZdZd�Zddejdeefd�Z	ddeefd�Z
dedejfd	�Z
ddeefd
�ZdS)�
MailHelperz6Common class to send emails via local smtp or sendmailc���tjjs�tj�d��r�t
dtj���}tj�|��tj	d���}|�
|��|�tj��ntj
��tj��|_dS)Nz/dev/log)�address�facilityz3mail_helper: (%(name)s) [%(levelname)s] %(message)s)�fmt)�logging�root�handlers�os�path�existsr�
LOG_DAEMON�
addHandler�	Formatter�setFormatter�setLevel�ERROR�basicConfig�	getLogger�_log)�self�SYS_LOG�
formatter_syss   r�__init__zMailHelper.__init__s����|�$�	&��w�~�~�j�)�)�
&�'�
�]�E]�^�^�^����'�'��0�0�0� '� 1�6k� l� l� l�
��$�$�]�3�3�3�� � ���/�/�/�/��#�%�%�%��%�'�'��	�	�	rN�msg�
encoding_namec�&�	|r(|����|��}n|���}gd�}tj|tjtjtjd���5}|�|��d}|jdkrtj|j||���	ddd��dS#1swxYwYdS#ttjf$r"}tt|����|�d}~wwxYw)z�
        Alternative function that should be called
        when we cannot send email via smtp
        :type msg: email.mime.base.MIMEBase
        :type encoding_name: encoding name to encode message text
        )z/usr/sbin/sendmailz-tz-oiT)�stdin�stdout�stderr�textrN)�	as_string�encode�
subprocess�Popen�PIPE�communicate�
returncode�CalledProcessError�OSErrorr	�str)r%r)r*�	s_message�cmd�procr-�exs        r�_sendmail_consolezMailHelper._sendmail_console*sh��	2��
,��M�M�O�O�2�2�=�A�A�	�	��M�M�O�O�	�5�5�5�C��!�� �o�!��!������	
V���)�)�)�4�4�Q�7���?�a�'�'�$�7����f�U�U�U�(�	
V�	
V�	
V�	
V�	
V�	
V�	
V�	
V�	
V�	
V�	
V�	
V����	
V�	
V�	
V�	
V�	
V�	
V����6�7�	2�	2�	2� ��R���)�)�r�1�����	2���s=�A9C�;AC�>C�C�C�C�C�D�.D�Dc�x�	tjd��}|r(|����|��}n|���}|�|||��|���dS#tjtf$r"}tt|����|�d}~wwxYw)z�
        Send email using smtp protocol;
        :type from_addr: str
        :type to_addrs: List[str]
        :type msg: email.mime.base.MIMEBase
        :type encoding_name: encoding name to encode message text
        �	localhostN)
�smtplib�SMTPr0r1�sendmail�quit�
SMTPException�socketerrorr	r9)r%�	from_addr�to_addrsr)r*�
smtp_instancer:r=s        r�_sendmail_smtpzMailHelper._sendmail_smtpEs���		2�#�L��5�5�M��
,��M�M�O�O�2�2�=�A�A�	�	��M�M�O�O�	��"�"�9�h�	�B�B�B���� � � � � ���%�{�3�	2�	2�	2� ��R���)�)�r�1�����	2���s�A=B�B9�B4�4B9rGc���|ds_tj�|�d��d���|d<|j�d|d��dSdS)z{Add Message-ID to message if it is missing
        :type from_addr: str
        :type msg: multipart.MIMEMultipart
        z
Message-ID�@���)�domainzAdded Message-ID: %sN)�email�utils�
make_msgid�splitr$�debug)r%rGr)s   r�_add_message_idzMailHelper._add_message_idXso��
�<� �	G� %�� 6� 6�i�o�o�c�>R�>R�SU�>V� 6� W� W�C����I�O�O�2�C��4E�F�F�F�F�F�	G�	Grc���|j�d|||��|�||��	|�||||��dS#t$r�}|j�d��	|�|��nQ#t$rD|j�d��|j�t|����Yn
wxYwYd}~dSYd}~dSd}~wwxYw)a0
        Try to send message using local smtp;
        If that can't be done - try to use /usr/sbin/sendmail;
        :type from_addr: str
        :type to_addrs: List[str]
        :type msg: multipart.MIMEMultipart
        :type encoding_name: Optional[str] encoding name to encode message text
        z+Trying to send mail with params: %s, %s, %sz-Smtp send failed, trying to send via sendmailzcCannot send email neither via smtp nor using sendmail; Please, check your mail server configurationN)	r$rSrTrJr	r>�	exceptionrr9)r%rGrHr)r*�smtp_exceptions      rrCzMailHelper.sendmailas4��	
�	���E�y�RZ�\_�`�`�`����Y��,�,�,�		5����	�8�S�-�H�H�H�H�H���	5�	5�	5��I�O�O�K�L�L�L�
5��&�&�s�+�+�+�+��!�
5�
5�
5��	�#�#�%S�T�T�T��	����N� 3� 3�4�4�4�4�4�
5����,�+�+�+�+�+�5�4�4�4�4�4�����	5���s<�A�
C,�C'�4B
�	C'�
AC�C'�C�C'�'C,)N)r
rr�__doc__r(�	mime_base�MIMEBaserr9r>rJr�
MIMEMultipartrTrCr
rrrrs�������@�@�
(�
(�
(�2�2�Y�%7�2��QT�
�2�2�2�2�62�2�h�s�m�2�2�2�2�&G��G�9�3J�G�G�G�G�5�5���
�5�5�5�5�5�5rr)�email.utilsrOrrrAr2�
email.mimerrYr�logging.handlersr�typingr�_socketrrF�
__author__�	Exceptionr	rr
rr�<module>rcs�����������	�	�	�	���������3�3�3�3�3�3�3�3�*�*�*�*�*�*�������(�(�(�(�(�(�
#�
�	�	�	�	�	�Y�	�	�	�Z5�Z5�Z5�Z5�Z5�Z5�Z5�Z5�Z5�Z5r