Your IP : 216.73.217.79


Current Path : /home/otto/python/app/services/__pycache__/
Upload File :
Current File : //home/otto/python/app/services/__pycache__/mail.cpython-310.pyc

o

~ױh/"�
@s`dZddlZddlZddlZddlmZddlmZddlm	Z	ddl
mZddlm
Z
mZmZmZddlmZddlZej�deee�jjj��dd	lmZGd
d�d�Zded
ededefdd�Zedkr�z+e�Ze� �r�e!d�ej"ddddd�Z#e#r�e!d�WdSe!d�WdSe!d�WdSe$y�Z%ze!de%���WYdZ%[%dSdZ%[%wwdS)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@s�eZdZdZddeeeeffdd�Zdd�Z						dded	ed
edeedee
ed
ee
edee
edeedefdd�Zde
defdd�Zde
de
edefdd�Zdefdd�Zdeeeffdd�ZdS) �SymfonyMailServicezm
    Mail service class that uses Python-specific configuration
    and provides methods to send emails.
    N�configcCs|pt�|_|��dS)z�
        Initialize the mail service.
        
        Args:
            config: Optional custom mail configuration. If None, uses default config.
        N)rr
�_validate_config��selfr
�r�U/home/otto/wwwroots/BitBucket/iga-core-symfony/iga-core-5/python/app/services/mail.py�__init__szSymfonyMailService.__init__cCsP|j�d�s
td��|j�d�std��|j�d�r$|j�d�s&td��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)rrrrr)s�z#SymfonyMailService._validate_config�to_email�subject�body�
from_email�cc�bcc�attachments�	html_body�returnc	
Cszmtd�}	||	d<||	d<|r||	d<n|j�dd��d|j�dd	��d
�|	d<|r1d�|�|	d<t|d
d�}
|	�|
�|rIt|dd�}|	�|�|rV|D]}|�|	|�qM|g}
|r`|
�|�|rg|
�|�|�|	|
�WSt	y�}zt
dt|��WYd}~dSd}~ww)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 <rznoreply@yourdomain.com�>z, �Cc�plainzutf-8�html�Error sending mail: NF)rr
r�joinr�attach�_add_attachment�extend�_send_via_smtp�	Exception�print�str)rrrrrrrr r!�msg�	text_part�	html_part�attachment_path�
recipients�errr�
send_email4s8
(



��zSymfonyMailService.send_emailr5�	file_pathcCs�z;t|d��}tdd�}|�|���Wd�n1swYt�|�|�ddtj�	|����|�
|�WdStyEYdSw)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�basenamer.r2)rr5r<�
attachment�partrrrr/xs
�
��z"SymfonyMailService._add_attachmentr9cCs�zK|j�d�rt�|jd|jd�}nt�|jd|jd�}|j�d�r.|jt��d�|j�d�r@|�|jd|jd�|�	|�|�
�Wd	Styatd
t
t��d��YdSw)
z Send the email message via SMTP.�use_sslrr�use_tls��contextrr�
smtp_passwordTr,�F)r
r�smtplib�SMTP_SSL�SMTP�starttls�ssl�create_default_context�login�send_message�quitr2r3r4�sys�exc_info)rr5r9�serverrrrr1�s0���
�z!SymfonyMailService._send_via_smtpcCs�zF|j�d�rt�|jd|jd�}nt�|jd|jd�}|j�d�r.|jt��d�|j�d�r@|�|jd|jd�|�	�Wd	St
yPYd
Sw)z2Test the SMTP connection without sending an email.rIrrrJrKrrrMTF)r
rrOrPrQrRrSrTrUrWr2)rrZrrr�test_connection�s,����z"SymfonyMailService.test_connectioncCs*|j��}d|vr|drdnd|d<|S)z<Get the current SMTP configuration (without sensitive data).rMz***N)r
�copyrrrr�
get_config�s
zSymfonyMailService.get_config)N)NNNNN)�__name__�
__module__�__qualname__�__doc__rrr4r	rrr�boolr;rr/r1r[r]rrrrrs@
�����
�
�
��
�D%rrrrr"cKst�}|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
    )rr;)rrr�kwargs�mail_servicerrr�send_quick_email�s
re�__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>)rrrr!zTest email sent successfully!zFailed to send test email.zSMTP connection failed!zError: )&rarDrOrS�email.mime.textr�email.mime.multipartr�email.mime.baser�emailr�typingrrrr	�pathlibr
rXrE�insertr4�__file__�parentr
rrrbrer^rdr[r3r;�successr2r:rrrr�<module>sH;����