U
    1i                     @  s   d Z ddlmZ ddlZddlZddlmZ ddlmZ ddl	m
Z
 edZddd	d	d
ddddZddd	d	d
ddddZddd	d	d
ddddZdS )z)Email sending via Gmail SMTP or SendGrid.    )annotationsN)MIMEMultipart)MIMEText)EmailConfigseo_optimizerr   strzlist[str] | Nonebool)configsubject	html_bodyto_addressesreturnc              
   C  s   |p| j }|std dS | jr(| js6td dS td}||d< | jpP| j|d< d||d< |t	|d	d
 znt
| j| jD}|  |  |  || j| j ||d ||  W 5 Q R X td|| W dS  tk
r } ztd| W Y dS d}~X Y nX dS )a&  Send email via SMTP (Gmail TLS).

    Args:
        config: Email configuration.
        subject: Email subject.
        html_body: HTML body content.
        to_addresses: Override recipients (defaults to config.email_to).

    Returns:
        True if sent successfully, False otherwise.
    0   No email recipients configured — skipping sendFu1   SMTP credentials not configured — skipping sendalternativeSubjectZFromz, ZTohtmlzutf-8zEmail sent via SMTP to %s: %sTzSMTP send failed: %sN)email_tologgerwarning	smtp_usersmtp_passwordr   
email_fromjoinattachr   smtplibSMTP	smtp_host	smtp_portZehloZstarttlsZloginZsendmail	as_stringinfo	Exceptionerror)r	   r
   r   r   
recipientsmsgservere r&   6/opt/web_system/seo_auto/src/reporting/email_sender.py	send_smtp   s0    


 r(   c              
   C  s   |p| j }|std dS | js0td dS zbddlm} ddlm} || jpV| j	|||d}|| j}|
|}	td|	j|| |	jd	kW S  tk
r }
 ztd
|
 W Y dS d}
~
X Y nX dS )a'  Send email via SendGrid HTTP API.

    Args:
        config: Email configuration.
        subject: Email subject.
        html_body: HTML body content.
        to_addresses: Override recipients (defaults to config.email_to).

    Returns:
        True if sent successfully, False otherwise.
    r   Fu1   SendGrid API key not configured — skipping sendr   )SendGridAPIClient)Mail)Z
from_emailZ	to_emailsr
   Zhtml_contentz-Email sent via SendGrid (status %d) to %s: %s)         zSendGrid send failed: %sN)r   r   r   sendgrid_api_keyZsendgridr)   Zsendgrid.helpers.mailr*   sendgrid_fromr   sendr   status_coder    r!   )r	   r
   r   r   r"   r)   r*   messageZsgresponser%   r&   r&   r'   send_sendgrid>   s8    





  r4   c                 C  s"   | j rt| |||S t| |||S )a2  Send email using the configured method (SendGrid if available, else SMTP).

    Args:
        config: Email configuration.
        subject: Email subject.
        html_body: HTML body content.
        to_addresses: Override recipients.

    Returns:
        True if sent successfully, False otherwise.
    )use_sendgridr4   r(   )r	   r
   r   r   r&   r&   r'   
send_emailp   s    r6   )N)N)N)__doc__
__future__r   loggingr   email.mime.multipartr   Zemail.mime.textr   src.config.settingsr   	getLoggerr   r(   r4   r6   r&   r&   r&   r'   <module>   s   
 3 6 