
    KCjr                         d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	  e       Z
 e	e      Zddeded	ed
edz  def
dZdedededz  dedeeeef   f
dZy)u  
Email sending service for notification alerts (module 1.1.11).

Uses plain SMTP via Python's standard library rather than a third-party
email API/SDK — this keeps the dependency footprint minimal and works
with any SMTP provider the client chooses (SendGrid, AWS SES, Gmail
SMTP relay, their own mail server, etc.) by just changing the .env
settings, with no code change.

Deliberately fails safe: if EMAIL_ENABLED is false or SMTP isn't
configured, send_email() logs and returns False rather than raising —
a notification email failing to send must never break the underlying
notification (which still exists and is visible in-app) or the
analysis flow that triggered it.
    N)MIMEMultipart)MIMEText)get_settings)
get_logger
to_addresssubject	body_text	body_htmlreturnc                    t         j                  st        j                  d|         yt         j                  rt         j
                  st        j                  d|         yt        d      }||d<   t         j                   dt         j                   d|d<   | |d	<   |j                  t        |d
             |r|j                  t        |d             	 t        j                  t         j                  t         j                  d      5 }t         j                  r|j!                          |j#                  t         j
                  t         j$                         |j'                  t         j                  | |j)                                d d d        t        j                  d|  d|        y# 1 sw Y   %xY w# t*        $ r'}t        j-                  d|  d| d       Y d }~yd }~ww xY w)NuB   Email sending disabled (EMAIL_ENABLED=false) — skipped email to FuZ   EMAIL_ENABLED is true but SMTP_HOST/SMTP_USERNAME are not configured — skipped email to alternativeSubjectz <>FromToplainhtml
   )timeoutzSent email to z: TzFailed to send email to )exc_info)settingsEMAIL_ENABLEDloggerinfo	SMTP_HOSTSMTP_USERNAMEwarningr   EMAIL_FROM_NAMEEMAIL_FROM_ADDRESSattachr   smtplibSMTP	SMTP_PORTSMTP_USE_TLSstarttlsloginSMTP_PASSWORDsendmail	as_string	Exceptionerror)r   r   r	   r
   messageserverexcs          GC:\Crop_Prediction\Backend\crop-ai-system\app\services\email_service.py
send_emailr0      s   !!XYcXdefX%;%;  *|-	
 M*G GI!112"X5P5P4QQRSGFOGDMNN8Iw/0x	623\\(,,h.@.@"M 	ZQW$$!LL//1G1GHOOH77WEVEVEXY		Z
 	nZL7)<=	Z 	Z   	/
|2cUCdSs1   4F: A>F.
#F: .F73F: :	G*G%%G*croplocation	old_label	new_labelc           	      f   d| j                          d|j                  dd       d}|r)d|j                  dd       d|j                  dd       dnd|j                  dd       d}d	|  d
|j                          d| d}d| j                          d|  d|j                          d| d	}|||fS )zMReturns (subject, plain_text_body, html_body) for a feasibility-change alert.zUpdate on your z plan: now '_ 'zchanged from 'z' to 'zis now classified as 'zYour crop plan for z in zz.

Log in to AgriField to see the full updated analysis, including yield forecast, market outlook, and alternative crops.
zm
    <div style="font-family: sans-serif; color: #1A1A1A;">
      <h2 style="color: #2D5016;">Update on your z/ plan</h2>
      <p>Your crop plan for <strong>z</strong> in <strong>z</strong>
      zX.</p>
      <p>Log in to AgriField to see the full updated analysis.</p>
    </div>
    )titlereplace)r1   r2   r3   r4   r   change_phrase	text_body	html_bodys           r/   build_feasibility_change_emailr>   A   s     

~\):K:KCQT:U9VVWXG  **345VI<M<McSV<W;XXYZ%i&7&7S&A%B!D  dV4(8'9=/ J= 	> 226**, @%%)F*?@P?Q Ro 	I Iy((    )N)__doc__r!   email.mime.multipartr   email.mime.textr   app.core.configr   app.core.logging_configr   r   __name__r   strboolr0   tupler>    r?   r/   <module>rJ      s      . $ ( .>	H	"3 " " "t "_c "J)
))),t)@C)
3S=)r?   