
    i                         d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	  ej                  e      Zde	e   defdZde	e   defdZdefd	Zdee   fd
ZdededdfdZdededdfdZy)z
Optional SMTP summary email after scan_recent completes.
Configure via environment variables (see .env.example); disabled if unset or SMTP_REPORT_ENABLED=false.
    N)MIMEText)ListOptionalvalreturnc                 H    | y| j                         j                         dv S )NF)1trueyesonstriplowerr   s    9/home/batmin/projects/sonar_scan/src/scan_report_email.py_truthyr      s%    
{99;"<<<    c                 n    | | j                         dk(  ry| j                         j                         dv S )N F)0falsenooffr   r   s    r   _falsyr      s2    
{ciikR'99;"===r   c                  r   t        t        j                  d            ryt        j                  dd      j                         t        j                  dd      j                         t        j                  dd      j                         t        j                  dd      j                         f} t	        |       S )zSTrue when email should be sent (all required vars set and not explicitly disabled).SMTP_REPORT_ENABLEDFSMTP_REPORT_HOSTr   SMTP_REPORT_TOSMTP_REPORT_USERSMTP_REPORT_PASSWORD)r   osgetenvr   all)requireds    r   smtp_summary_enabledr%      s    bii-./
		$b)//1
		"B'--/
		$b)//1
		("-335	H x=r   c                      t        j                  dd      } | j                  d      D cg c]#  }|j                         s|j                         % c}S c c}w )Nr   r   ,)r!   r"   splitr   )rawxs     r   _recipientsr+   (   s=    
))$b
)C"yy~;!AGGI;;;s   A Asubjectbodyc                    t         j                  d   j                         }t        t         j                  j	                  dd      j                         xs d      }t         j                  d   j                         }t         j                  d   }t        j
                  d      xs |j                         }t               }|st        d      t        |dd	      }| |d
<   ||d<   dj                  |      |d<   t        t        j
                  d            xs |dk(  }	|	 xr t        t        j
                  d             }
t        j                         }|	rVt        j                  |||d      5 }|j!                  ||       |j#                  |||j%                                ddd       nt        j&                  ||d      5 }|j)                          |
r"|j+                  |       |j)                          |j!                  ||       |j#                  |||j%                                ddd       t,        j/                  ddj                  |             y# 1 sw Y   /xY w# 1 sw Y   ;xY w)zP
    Send plain-text email. Raises on failure; caller should catch and log.
    r   SMTP_REPORT_PORT587r   r    SMTP_REPORT_FROMz SMTP_REPORT_TO has no recipientsplainzutf-8SubjectFromz, ToSMTP_REPORT_USE_SSLi  SMTP_REPORT_STARTTLS<   )contexttimeoutN)r:   )r9   zSent scan summary email to %s)r!   environr   intgetr"   r+   
ValueErrorr   joinr   r   sslcreate_default_contextsmtplibSMTP_SSLloginsendmail	as_stringSMTPehlostarttlsloggerinfo)r,   r-   hostportuserpassword	from_addrto_addrsmsguse_sslrI   r9   servers                r   send_scan_summary_emailrU   -   s    ::()//1Drzz~~0%8>>@IEJD::()//1Dzz01H-.6$==?I}H;<<
4'
*CC	NCK		(#CIbii 567F43;G{L6"))4J*K#LLH((*GdD'2F 	B&LLx(OOIxA	B 	B \\$b1 	BVKKM0LLx(OOIxA	B KK/81DE	B 	B	B 	Bs   24IA(I)I&)I2c                     t               st        j                  d       y	 t        | |       y# t        $ r }t        j                  d|       Y d}~yd}~ww xY w)zASend summary email if SMTP is configured; log and swallow errors.z7SMTP summary email skipped (not configured or disabled)Nz%Could not send scan summary email: %s)r%   rJ   debugrU   	Exceptionwarning)r,   r-   es      r   maybe_send_scan_summary_emailr[   S   sL    !NOC. C>BBCs   / 	AAA)__doc__loggingr!   rB   r@   email.mime.textr   typingr   r   	getLogger__name__rJ   strboolr   r   r%   r+   rU   r[    r   r   <module>re      s     	  
 $ !			8	$=# =4 => >$ >
d 
<T#Y <
#FS #F #F #FLC3 Cc Cd Cr   