11from __future__ import annotations
22
33import smtplib
4+ import textwrap
5+ from datetime import datetime
46from email .message import EmailMessage
57
68from patchwork .common .utils .utils import mustache_render
79from patchwork .step import Step
10+ from patchwork .steps import ReadEmail
811from patchwork .steps .SendEmail .typed import SendEmailInputs , SendEmailOutputs
912
1013
1114class SendEmail (Step , input_class = SendEmailInputs , output_class = SendEmailOutputs ):
1215 def __init__ (self , inputs ):
1316 super ().__init__ (inputs )
14- self .email_template_value = inputs .get ("email_template_value" , dict ())
15- self .subject = inputs .get ("subject" , "Patchwork Execution Email" )
16- self .body = inputs .get ("body" , "Patchwork Execution Email" )
17- self .sender_email = inputs ["sender_email" ]
18- self .recipient_email = inputs ["recipient_email" ]
1917 self .smtp_host = inputs .get ("smtp_host" , "smtp.gmail.com" )
2018 self .smtp_username = inputs ["smtp_username" ]
2119 self .smtp_password = inputs ["smtp_password" ]
2220 self .smtp_port = int (inputs .get ("smtp_port" , 25 ))
23- self .reply_message_id = inputs .get ("reply_message_id" )
2421 self .is_ssl = bool (inputs .get ("is_smtp_ssl" , False ))
2522
23+ self .sender_email = inputs ["sender_email" ]
24+ self .recipient_email = inputs ["recipient_email" ]
25+ email_template_value = inputs .get ("email_template_value" , dict ())
26+ self .subject = mustache_render (inputs .get ("subject" , "Patchwork Execution Email" ), email_template_value )
27+ self .body = mustache_render (inputs .get ("body" , "Patchwork Execution Email" ), email_template_value )
28+ self .reply_message_id = inputs .get ("reply_message_id" )
29+ self .__handle_eml_file (inputs .get ("reply_eml_file_path" ))
30+
31+ def __handle_eml_file (self , eml_file : str ):
32+ if eml_file is None :
33+ return
34+
35+ original_email_data = ReadEmail (dict (eml_file_path = eml_file )).run ()
36+ timestamp : datetime = original_email_data .get ("datetime" )
37+ date_str = timestamp .date ().strftime ('%-d %b %Y' )
38+ time_str = timestamp .time ().strftime ('%H:%M' )
39+ from_ = original_email_data .get ("from" )
40+ self .subject = original_email_data .get ("subject" )
41+ self .body += f"\n \n On { date_str } at { time_str } , { from_ } wrote:\n \n " + textwrap .indent (original_email_data .get ("body" ), "> " )
42+ self .reply_message_id = original_email_data .get ("message_id" )
43+
2644 def run (self ) -> dict :
27- msg = EmailMessage ()
28- msg .set_content (mustache_render (self .body , self .email_template_value ))
29- msg ["Subject" ] = mustache_render (self .subject , self .email_template_value )
30- msg ["From" ] = self .sender_email
31- msg ["To" ] = self .recipient_email
32- if self .reply_message_id is not None :
33- msg .add_header ("References" , self .reply_message_id )
34- msg .add_header ("In-Reply-To" , self .reply_message_id )
45+ msg = self .__create_email_message ()
3546
3647 smtp_clazz = smtplib .SMTP
3748 if self .is_ssl :
@@ -41,3 +52,14 @@ def run(self) -> dict:
4152 mailserver .login (self .smtp_username , self .smtp_password )
4253 mailserver .send_message (msg )
4354 return dict ()
55+
56+ def __create_email_message (self ):
57+ msg = EmailMessage ()
58+ msg .set_content (self .body )
59+ msg ["Subject" ] = self .subject
60+ msg ["From" ] = self .sender_email
61+ msg ["To" ] = self .recipient_email
62+ if self .reply_message_id is not None :
63+ msg .add_header ("References" , self .reply_message_id )
64+ msg .add_header ("In-Reply-To" , self .reply_message_id )
65+ return msg
0 commit comments