Ë
    KµCjr  ã                   ó°   — 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›  € Ü×!Ò!Ü�‰ÐXÐYcÐXdÐeÔfØä×Ò¤X×%;Ò%;Ü�‰ð Ø *˜|ð-ô	
ð ä˜MÓ*€GØ €GˆIÑÜ!×1Ñ1Ð2°"´X×5PÑ5PÐ4QÐQRÐS€GˆF�OØ€GˆD�Mà‡N�N”8˜I wÓ/Ô0ÙØ�‰”x 	¨6Ó2Ô3ðÜ�\‰\œ(×,Ñ,¬h×.@Ñ.@È"ÔMð 	ZÐQWÜ×$Ò$Ø—‘Ô!Ø�L‰Lœ×/Ñ/´×1GÑ1GÔHØ�O‰OœH×7Ñ7¸ÀW×EVÑEVÓEXÔY÷		Zô
 	�‰�n Z L°°7°)Ð<Ô=Ø÷	Zð 	Zûô ò ô 	�‰Ð/°
¨|¸2¸c¸UÐCÈdˆÔ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Ñ:KÈCÐQTÓ:UÐ9VÐVWÐX€Gñ ð ˜×*Ñ*¨3°Ó4Ð5°V¸I×<MÑ<MÈcÐSVÓ<WÐ;XÐXYÑZà% i×&7Ñ&7¸¸SÓ&AÐ%BÀ!ÐDð ð ˜d˜V 4¨¯©Ó(8Ð'9¸¸=¸/ð J=ð 	>ð ð2à26·*±*³,°ð @%Ø%) FÐ*?ÀÇÁÓ@PÐ?Qð RØ€oð ð	€Ið �I˜yÐ(Ð(ó    )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?   