@@ -26,9 +26,13 @@ def webhook():
2626 raw_message = base64 .b64decode (message_base64 ).decode ('utf-8' )
2727 email_message = Parser ().parsestr (raw_message )
2828
29- # Extract the original sender
30- original_sender = email_message .get ('From' , 'Unknown Sender' )
31- original_subject = email_message .get ('Subject' , 'No Subject' )
29+ # Extract original email information
30+ original_infos = {
31+ 'sender' : email_message .get ('From' , 'Unknown Sender' ),
32+ 'subject' : email_message .get ('Subject' , 'No Subject' ),
33+ 'to' : email_message .get ('To' , '' ),
34+ 'cc' : email_message .get ('Cc' , '' )
35+ }
3236
3337 payload = None
3438 if email_message .is_multipart ():
@@ -46,21 +50,54 @@ def webhook():
4650 if len (message_content ) < 10 :
4751 raise ValueError ("The email body must have at least 10 characters" )
4852
49- send_email (message_content , original_sender , original_subject )
53+ send_email (message_content , original_infos )
5054 return "OK" , 200
5155 except (KeyError , ValueError , base64 .binascii .Error ) as e :
5256 return jsonify ({"error" : str (e )}), 400
5357 except Exception as e :
5458 return jsonify ({"error" : str (e )}), 500
5559
56- def send_email (content , original_sender , original_subject ):
60+ def format_forwarded_email_header (original_infos ):
61+ """
62+ Format the header information for forwarded emails.
63+ This function can be easily extended to include more information in the future.
64+
65+ Args:
66+ original_infos (dict): Dictionary containing original email information
67+ Expected keys: 'sender', 'to', 'cc', 'subject'
68+ """
69+ header_lines = [
70+ "--- Forwarded Message Information ---" ,
71+ f"Original Sender: { original_infos .get ('sender' , 'Unknown' )} "
72+ ]
73+
74+ if original_infos .get ('to' ):
75+ header_lines .append (f"Original Recipients: { original_infos ['to' ]} " )
76+
77+ if original_infos .get ('cc' ):
78+ header_lines .append (f"Original CC: { original_infos ['cc' ]} " )
79+
80+ header_lines .extend ([
81+ "--- End of Forwarded Message Information ---" ,
82+ "" # Empty line to separate from message content
83+ ])
84+
85+ return "\n " .join (header_lines )
86+
87+ def send_email (content , original_infos ):
5788 msg = EmailMessage ()
58- forward_infos = f"\n \n Note: this message has been forwarded via hook2mail from { original_sender } "
59- msg .set_content (content + forward_infos )
60- msg ["Subject" ] = original_subject
89+
90+ # Create header with original email information
91+ forwarded_header = format_forwarded_email_header (original_infos )
92+
93+ # Combine header and original content
94+ full_content = forwarded_header + "\n " + content
95+
96+ msg .set_content (full_content )
97+ msg ["Subject" ] = original_infos .get ('subject' , 'No Subject' )
6198 msg ["From" ] = EMAIL_FROM
6299 msg ["To" ] = EMAIL_TO
63- msg .add_header ('Reply-To' , f"{ original_sender } , { EMAIL_FROM } " )
100+ msg .add_header ('Reply-To' , f"{ original_infos . get ( 'sender' , EMAIL_FROM ) } , { EMAIL_FROM } " )
64101
65102 with smtplib .SMTP (SMTP_HOST , SMTP_PORT ) as server :
66103 if USE_STARTTLS :
0 commit comments