Your IP : 216.73.217.79


Current Path : /var/www/cesa.co.za/python/app/services/__pycache__/
Upload File :
Current File : /var/www/cesa.co.za/python/app/services/__pycache__/mail.cpython-311.pyc

�

<Ri�'��Z�dZddlZddlZddlZddlmZddlmZddlm	Z	ddl
mZddlm
Z
mZmZmZddlmZddlZej�deee��jjj����dd	lmZGd
�d��Zded
ededefd�Zedkr�	e��Ze� ��r@e!d��e�"dddd���Z#e#r
e!d��dSe!d��dSe!d��dS#e$$rZ%e!de%����YdZ%[%dSdZ%[%wwxYwdS)z�
Mail Service for IGA Core Python Applications

This module provides a mail service class that uses Python-specific configuration
and sends emails using the configured SMTP server.
�N)�MIMEText)�
MIMEMultipart)�MIMEBase)�encoders)�List�Optional�Dict�Any)�Path)�get_mail_configc�2�eZdZdZddeeeeffd�Zd�Z						ddededed	eed
ee
edee
edee
ed
eedefd�Zde
defd�Zde
de
edefd�Zdefd�Zdeeeffd�ZdS)�SymfonyMailServicezm
    Mail service class that uses Python-specific configuration
    and provides methods to send emails.
    N�configc�X�|p
t��|_|���dS)z�
        Initialize the mail service.
        
        Args:
            config: Optional custom mail configuration. If None, uses default config.
        N)rr�_validate_config��selfrs  �//var/www/cesa.co.za/python/app/services/mail.py�__init__zSymfonyMailService.__init__s.���1�� 1� 1�����������c�4�|j�d��std���|j�d��std���|j�d��r)|j�d��std���dSdS)	z'Validate the loaded SMTP configuration.�	smtp_hostzSMTP host not configured�	smtp_portzSMTP port not configured�
auth_required�
smtp_usernamez6SMTP authentication required but username not providedN)r�get�
ValueError)rs rrz#SymfonyMailService._validate_config)s����{���{�+�+�	9��7�8�8�8��{���{�+�+�	9��7�8�8�8��;�?�?�?�+�+�	W�D�K�O�O�O�4T�4T�	W��U�V�V�V�	W�	W�	W�	Wr�to_email�subject�body�
from_email�cc�bcc�attachments�	html_body�returnc		��	td��}	||	d<||	d<|r||	d<n=|j�dd���d|j�dd	���d
�|	d<|rd�|��|	d<t	|d
d��}
|	�|
��|r&t	|dd��}|	�|��|r|D]}|�|	|���|g}
|r|
�|��|r|
�|��td|����|�	|	|
��}td|����|S#t$rf}tdt|��j�dt|������ddl}td��|���Yd}~dSd}~wwxYw)aM
        Send an email using the configured SMTP server.
        
        Args:
            to_email: Recipient email address
            subject: Email subject
            body: Plain text email body
            from_email: Sender email address (optional, will use default if not provided)
            cc: List of CC recipients
            bcc: List of BCC recipients
            attachments: List of file paths to attach
            html_body: HTML version of the email body (optional)
        
        Returns:
            bool: True if email sent successfully, False otherwise
        �alternative�Subject�To�From�	from_namezPython Mail Servicez <r!znoreply@yourdomain.com�>z, �Cc�plainzutf-8�htmlz*[DEBUG MAIL] Attempting to send email to: z&[DEBUG MAIL] _send_via_smtp returned: z![ERROR MAIL] Error sending mail: �: rNz[ERROR MAIL] Traceback:F)rrr�joinr�attach�_add_attachment�extend�print�_send_via_smtp�	Exception�type�__name__�str�	traceback�	print_exc)rrrr r!r"r#r$r%�msg�	text_part�	html_part�attachment_path�
recipients�result�er<s                 r�
send_emailzSymfonyMailService.send_email4sI��2/	��
�.�.�C�$�C�	�N� �C��I��
S�(��F���!%�����>S�!T�!T�S�S�X\�Xc�Xg�Xg�ht�wO�YP�YP�S�S�S��F���
*� �I�I�b�M�M��D�	�!��w��8�8�I��J�J�y�!�!�!��
&�$�Y���@�@�	��
�
�9�%�%�%��
?�'2�?�?�O��(�(��o�>�>�>�>�#��J��
&��!�!�"�%�%�%��
'��!�!�#�&�&�&�
�I�x�I�I�J�J�J��(�(��j�9�9�F��C�6�C�C�D�D�D��M���	�	�	��R�d�1�g�g�6F�R�R�#�a�&�&�R�R�S�S�S������,�-�-�-����!�!�!��5�5�5�5�5�����	���s�EE�
G�AF=�=Gr>�	file_pathc��	t|d��5}tdd��}|�|�����ddd��n#1swxYwYt	j|��|�ddtj�	|������|�
|��dS#t$rYdSwxYw)z+Add a file attachment to the email message.�rb�applicationzoctet-streamNzContent-Dispositionzattachment; filename= )�openr�set_payload�readr�
encode_base64�
add_header�os�path�basenamer3r8)rr>rF�
attachment�parts     rr4z"SymfonyMailService._add_attachment~s��	��i��&�&�
4�*��
�~�>�>��� � ����!2�!2�3�3�3�
4�
4�
4�
4�
4�
4�
4�
4�
4�
4�
4����
4�
4�
4�
4�
�"�4�(�(�(��O�O�%�F���)9�)9�)�)D�)D�F�F�
�
�
�
�J�J�t��������	�	�	��D�D�	���s5�C�8A�
C�A�C�A�A"C�
C�CrBc	��	td|jd�d|jd����td|j�d���d|j�d������|j�d��rJtd	��tj|jd|jd��}td
��n�td��tj|jd|jd��}td��|j�d��rEtd
��|�tj�����td��|j�d��rZtd|jd�d���|�	|jd|jd��td��ntd��td|����|�
|��td��|���td��dS#t$rf}tdt|��j�dt|������ddl}td��|���Yd}~d Sd}~wwxYw)!z Send the email message via SMTP.zEBUG SMTP] Connecting to r�:rz[DEBUG SMTP] Use SSL: �use_sslz, Use TLS: �use_tlsz,[DEBUG SMTP] Creating SMTP_SSL connection...z,[DEBUG SMTP] SMTP_SSL connection establishedz([DEBUG SMTP] Creating SMTP connection...z([DEBUG SMTP] SMTP connection establishedz[DEBUG SMTP] Starting TLS...��contextz[DEBUG SMTP] TLS startedrz[DEBUG SMTP] Authenticating as rz...�
smtp_passwordz&[DEBUG SMTP] Authentication successfulz'[DEBUG SMTP] No authentication requiredz,[DEBUG SMTP] Sending message to recipients: z&[DEBUG SMTP] Message sent successfullyz[DEBUG SMTP] Connection closedTz![ERROR SMTP] Error sending mail: r1rNz[ERROR SMTP] Traceback:F)r6rr�smtplib�SMTP_SSL�SMTP�starttls�ssl�create_default_context�login�send_message�quitr8r9r:r;r<r=)rr>rB�serverrDr<s      rr7z!SymfonyMailService._send_via_smtp�s���4	��e���K�0H�e�e�4�;�Wb�Kc�e�e�f�f�f��n�4�;�?�?�9�+E�+E�n�n�RV�R]�Ra�Ra�bk�Rl�Rl�n�n�o�o�o��{���y�)�)�
6��D�E�E�E� �)��K��,��K��,�����D�E�E�E�E��@�A�A�A� ���K��,��K��,�����@�A�A�A��;�?�?�9�-�-�6��8�9�9�9��O�O�C�,F�,H�,H�O�I�I�I��4�5�5�5��{����/�/�
A��Y���O�8T�Y�Y�Y�Z�Z�Z�����K��0��K��0�����>�?�?�?�?��?�@�@�@�
�M��M�M�N�N�N�����$�$�$��:�;�;�;��K�K�M�M�M��2�3�3�3��4���	�	�	��R�d�1�g�g�6F�R�R�#�a�&�&�R�R�S�S�S������,�-�-�-����!�!�!��5�5�5�5�5�����	���s�II�
K�&AK�Kc�B�	|j�d��r,tj|jd|jd��}nltj|jd|jd��}|j�d��r'|�t
j�����|j�d��r,|�|jd|jd��|�	��d	S#t$rYd
SwxYw)z2Test the SMTP connection without sending an email.rVrrrWrXrrrZTF)rrr[r\r]r^r_r`rarcr8)rrds  r�test_connectionz"SymfonyMailService.test_connection�s��	��{���y�)�)�
J� �)��K��,��K��,�����
!���K��,��K��,����
�;�?�?�9�-�-�J��O�O�C�,F�,H�,H�O�I�I�I��{����/�/�
�����K��0��K��0����

�K�K�M�M�M��4���	�	�	��5�5�	���s�DD�
D�Dc�^�|j���}d|vr|drdnd|d<|S)z<Get the current SMTP configuration (without sensitive data).rZz***N)r�copyrs  r�
get_configzSymfonyMailService.get_config�s=����!�!�#�#���f�$�$�/5�o�/F�&P�e�e�D�F�?�#��
r)N)NNNNN)r:�
__module__�__qualname__�__doc__rr	r;r
rrr�boolrErr4r7rfri�rrrrs���������
 � �x��S�#�X��7� � � � �	W�	W�	W�04�-1�.2�6:�.2�H�H� �H��H��H� (��}�	H�
 ��S�	�*�H�!��c��+�
H�!)��c�� 3�H�'�s�m�H�8<�H�H�H�H�T�=��S����� 6�-�6�T�#�Y�6�4�6�6�6�6�p������:�D��c��N������rrrrr r&c�>�t��}|j|||fi|��S)aY
    Quick function to send an email using default configuration.
    
    Args:
        to_email: Recipient email address
        subject: Email subject
        body: Email body
        **kwargs: Additional arguments passed to SymfonyMailService.send_email()
    
    Returns:
        bool: True if email sent successfully, False otherwise
    )rrE)rrr �kwargs�mail_services     r�send_quick_emailrr�s.��&�'�'�L�"�<�"�8�W�d�E�E�f�E�E�Er�__main__zSMTP connection successful!ztest@example.comzTest Email from IGA Corez@This is a test email sent from the IGA Core Symfony application.zk<h1>Test Email</h1><p>This is a test email sent from the <strong>IGA Core Symfony</strong> application.</p>)rrr r%zTest email sent successfully!zFailed to send test email.zSMTP connection failed!zError: )&rlrOr[r_�email.mime.textr�email.mime.multipartr�email.mime.baser�emailr�typingrrr	r
�pathlibr�sysrP�insertr;�__file__�parentrrrrmrrr:rqrfr6rE�successr8rDrnrr�<module>rsq����
�	�	�	�����
�
�
�
�$�$�$�$�$�$�.�.�.�.�.�.�$�$�$�$�$�$�������,�,�,�,�,�,�,�,�,�,�,�,��������
�
�
������3�3�t�t�H�~�~�,�3�:�;�;�<�<�<�"�"�"�"�"�"�O�O�O�O�O�O�O�O�fF�s�F�S�F��F�$�F�F�F�F�$�z����)�)�+�+���'�'�)�)�	-��E�/�0�0�0�#�-�-�+�2�W�H�	.���G��
4���5�6�6�6�6�6���2�3�3�3�3�3��E�+�,�,�,�,�,������
��m��m�m������������������3�s%�AD
�0D
�=D
�
D(�D#�#D(