Current File : //opt/cloudlinux/venv/lib/python3.11/site-packages/ssa/internal/__pycache__/mailer.cpython-311.pyc
�

��e���dZddlZddlZddlZddlZddlmZmZddlm	Z	ddl
mZddlm
Z
ddlmZmZdd	lmZdd
lmZddlmZmZded
efd�ZGd�d��ZdS)z8
This module contains SSA Mailer class and helper tools
�N)�ConfigParser�SectionProxy)�EmailMessage��gethostname)�is_cl_solo_edition)�Environment�FileSystemLoader�)�mail_template_location)�SSAMailerError)�
duration_cast�format_date�report_data�returnc�n�ttt��dd���}t|jd<|�d��}|�|dtd������}|�d��}|�t|d	��|�
��}||fS)zJ
    Render an HTML table for e-mail report and a full e-mail message
    T)�loader�trim_blocks�
lstrip_blocksrzreport_table.html�domains)�skip_jwt_check)r�is_soloz	mail.html�date)r�report_table)	r	r
rr�filters�get_template�renderrr)r�env�table_templater�
mail_template�mails      �J/opt/cloudlinux/venv/lib64/python3.11/site-packages/ssa/internal/mailer.py�render_report_tabler#s����� 6�7�7������C�
$1�C�K�� ��%�%�&9�:�:�N�!�(�(��Y�1G�1C�<@�2B�2B�2B�)�C�C�L��$�$�[�1�1�M����[��V�1D�%E�%E�-9� �;�;�D�����c���eZdZdZd�Zedefd���Zedefd���Z	e
dedefd���Zde
dd	fd
�Zde
dd	fd�Zde
dd	fd
�Z	ddededd	fd�Zdedede
fd�Zd	S)�Mailerz.
    Class contains SSA e-mail send logic
    c�F�tjd��|_d|_dS)N�mailer)�logging�	getLogger�logger�_sender��selfs r"�__init__zMailer.__init__6s���'��1�1�������r$rc��dS)z+
        Local mail server address
        )�	localhost�r-s r"�mail_serverzMailer.mail_server:s	��
�~r$c�$�dt����S)z%
        'From' mail address
        zslow-site-analyzer@rr-s r"�senderz
Mailer.senderAs��
5�[�]�]�4�4�4r$�namec���t�d|�d�}tj�|��r-t	d���}|�|��|dSt
d|�dt�����)zF
        Get preformatted data for e-mail by name of template
        �/z.iniN)�
interpolation�datazFailed to find template z in )r�os�path�existsr�readr
)r6�tmpl�configs   r"�
read_templatezMailer.read_templateHs���
)�5�5�4�5�5�5��
�7�>�>�$���	"�!��5�5�5�F��K�K������&�>�!��I�t�I�I�1G�I�I�K�K�	Kr$�messageNc���|j�d��	tj|j�5}|�|��}|j�d|��ddd��dS#1swxYwYdS#tj$r$}tdt|�������d}~wttf$r$}tdt|�������d}~wwxYw)z=
        Send preformatted e-mail via localhost SMTP
        zTry to send via smtpzSend result: %sNzsmtp mailing failed: zsmtp connection failed: )r+�info�smtplib�SMTPr3�send_message�
SMTPExceptionr
�str�ConnectionError�OSError)r.rB�server�result�es     r"�
_smtp_sendzMailer._smtp_sendUs>��	
����/�0�0�0�	F���t�/�0�
<�F��,�,�W�5�5���� � �!2�F�;�;�;�
<�
<�
<�
<�
<�
<�
<�
<�
<�
<�
<�
<����
<�
<�
<�
<�
<�
<���$�	C�	C�	C� �!A��Q���!A�!A�B�B�B�������)�	F�	F�	F� �!D�C��F�F�!D�!D�E�E�E�����	F���sF�A:�1A-� A:�-A1�1A:�4A1�5A:�:C �	B(�(C �<C�C c� �|j�d��gd�}	tj||���ddd���dS#t
ttjf$r$}tdt|�������d}~wwxYw)z?
        Send preformatted e-mail via sendmail utility
        z Try to send via sendmail utility)z/usr/sbin/sendmailz-tz-oiT)�input�capture_output�text�checkzsendmail utility failed with N)
r+rD�
subprocess�run�	as_stringrK�
ValueError�CalledProcessErrorr
rI)r.rB�cmdrNs    r"�
_console_sendzMailer._console_sendcs���	
����;�<�<�<�1�1�1��	K��N�3�!(�!2�!2�!4�!4�*.� $�D�
2�
2�
2�
2�
2�
2����Z�%B�C�	K�	K�	K� �!I��Q���!I�!I�J�J�J�����	K���s�+A
�
B
�)B�B
r!c��	|�|��dS#t$r�}|j�t	|����	|�|��n_#t$rR}|j�t	|����|j�d|d��Yd}~nd}~wwxYwYd}~dSYd}~dSd}~wwxYw)zm
        Try to send mail via localhost smtp server,
        if fails -- try to use sendmail utility
        z3Both smtp and sendmail failed to send message to %s�ToN)rOr
r+�errorrIr[�critical)r.r!rNs   r"�_sendzMailer._sendqs ��

	 ��O�O�D�!�!�!�!�!���	 �	 �	 ��K���c�!�f�f�%�%�%�
 ��"�"�4�(�(�(�(��!�
 �
 �
 ���!�!�#�a�&�&�)�)�)���$�$�I���J� � � � � � � � �����
 ����)�(�(�(�(�(� � � � � � �����
	 ���s?��
C�'C�A!� C�!
B=�+AB8�3C�8B=�=C�C�
ssa_report�	recipient�templatec�J�|�|j||fi|����dS)z{
        Create a message from given template, including additional kwargs,
        and send it to given recipient
        N)r`�_message)r.rbrc�kwargss    r"�send_simple_mailzMailer.send_simple_mail�s2��	
�
�
�=�4�=��H�?�?��?�?�@�@�@�@�@r$c�\�|�|��}t��}|d|z|d<|j|d<||d<|�|d|z��|�|d|zd���|j�d|�����|S)	z 
        Form a message
        �subject�Subject�Fromr]rS�html)�subtypezGenerated mail --> %s)rArr5�set_content�add_alternativer+rDrW)r.rbrcrfr:�msgs      r"rezMailer._message�s����!�!�(�+�+���n�n���i��6�1��I���k��F����D�	�����V��v�-�.�.�.����D��L�6�1�6��B�B�B�����0�#�-�-�/�/�B�B�B��
r$)ra)�__name__�
__module__�__qualname__�__doc__r/�property�tupler3rIr5�staticmethodrrArrOr[r`rgrer2r$r"r&r&1s��������������U�����X���5��5�5�5��X�5��
K�C�
K�L�
K�
K�
K��\�
K�F�,�F�4�F�F�F�F�K�\�K�d�K�K�K�K� �,� �4� � � � �&*6�A�A�$'�A�#&�A�'+�A�A�A�A������+������r$r&)rtr)r;rErU�configparserrr�
email.messager�socketr�clcommon.lib.cleditionr�jinja2r	r
�	constantsr�
exceptionsr
�utilsrr�dictrvr#r&r2r$r"�<module>r�s8��������	�	�	�	���������3�3�3�3�3�3�3�3�&�&�&�&�&�&�������5�5�5�5�5�5�0�0�0�0�0�0�0�0�-�-�-�-�-�-�&�&�&�&�&�&�-�-�-�-�-�-�-�-��T��e�����,m�m�m�m�m�m�m�m�m�mr$