forked from awalon/MailToTelegramForwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailToTelegramForwarder.py
816 lines (685 loc) · 34 KB
/
mailToTelegramForwarder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
#!/usr/bin/env python3
"""
Fetch mails from IMAP server and forward them to Telegram Chat.
Copyright (C) 2021 Awalon (https://github.com/awalon)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
try:
import warnings
import sys
import re
import unicodedata
import html
import socket
import time
import logging
import configparser
import argparse
import imaplib2
import email
from email.header import Header, decode_header, make_header
except ImportError as import_error:
logging.critical(import_error.__class__.__name__ + ": " + import_error.args[0])
sys.exit(2)
"""
Mail2TelegramForwarder:
A python script that fetches mails from remote IMAP mail server
and forward body and/or attachments to Telegram chat/user.
Based on great work done by:
https://github.com/florianpaulhoberg/IMAP2Telegram
https://github.com/angelos-se/IMAPBot
"""
__appname__ = "Mail to Telegram Forwarder"
__version__ = "0.1.2"
__author__ = "Awalon (https://github.com/awalon)"
with warnings.catch_warnings(record=True) as w:
# Cause all warnings to always be triggered.
warnings.simplefilter("always")
from telegram.utils import helpers
import telegram
# Ignore not supported warnings
if len(w) > 0:
if 'This is allowed but not supported by python-telegram-bot maintainers' in str(w[-1].message):
w.remove(w[-1])
class Tool:
def __init__(self,):
pass
@staticmethod
def binary_to_string(value, **kwargs) -> str:
encoding = kwargs.get('encoding')
if not encoding:
encoding = 'utf-8'
if type(value) is bytes:
try:
return str(bytes.decode(value, encoding=encoding, errors='replace'))
except UnicodeDecodeError as decode_error:
logging.error("Can not decode value: '", value, "' reason: ", decode_error.reason)
return ' ###decoder-error:%s### ' % decode_error.reason
else:
return str(value)
class Config:
config = None
imap_user = None
imap_password = None
imap_server = None
imap_port = 993
imap_timeout = 60
imap_refresh = 10
imap_push_mode = False
imap_disconnect = False
imap_folder = 'INBOX'
imap_search = '(UID ${lastUID}:* UNSEEN)'
imap_mark_as_read = False
imap_max_length = 2000
tg_bot_token = None
tg_forward_to_chat_id = None
tg_prefer_html = True
tg_markdown_version = 2
tg_forward_mail_content = True
tg_forward_attachment = True
def __init__(self, cmd_args):
"""
Parse config file to obtain login credentials, address of remote mail server,
telegram config and configuration which controls behaviour of this script .
"""
try:
self.config = configparser.ConfigParser()
files = self.config.read(cmd_args.config)
if len(files) == 0:
logging.critical("Error parsing config file: File '%s' not found!" % cmd_args.config)
sys.exit(2)
self.imap_user = self.get_config('Mail', 'user', self.imap_user)
self.imap_password = self.get_config('Mail', 'password', self.imap_password)
self.imap_server = self.get_config('Mail', 'server', self.imap_server)
self.imap_port = self.get_config('Mail', 'port', self.imap_port, int)
self.imap_timeout = self.get_config('Mail', 'timeout', self.imap_timeout, int)
self.imap_refresh = self.get_config('Mail', 'refresh', self.imap_refresh, int)
self.imap_push_mode = self.get_config('Mail', 'push_mode', self.imap_push_mode, bool)
self.imap_disconnect = self.get_config('Mail', 'disconnect', self.imap_disconnect, bool)
self.imap_folder = self.get_config('Mail', 'folder', self.imap_folder)
self.imap_search = self.get_config('Mail', 'search', self.imap_search)
self.imap_mark_as_read = self.get_config('Mail', 'mark_as_read', self.imap_mark_as_read, bool)
self.imap_max_length = self.get_config('Mail', 'max_length', self.imap_max_length, int)
self.tg_bot_token = self.get_config('Telegram', 'bot_token', self.tg_bot_token)
self.tg_forward_to_chat_id = self.get_config('Telegram', 'forward_to_chat_id',
self.tg_forward_to_chat_id, int)
self.tg_forward_mail_content = self.get_config('Telegram', 'forward_mail_content',
self.tg_forward_mail_content, bool)
self.tg_prefer_html = self.get_config('Telegram', 'prefer_html', self.tg_prefer_html, bool)
self.tg_markdown_version = self.get_config('Telegram', 'markdown_version', self.tg_markdown_version, int)
self.tg_forward_attachment = self.get_config('Telegram', 'forward_attachment',
self.tg_forward_attachment, bool)
except configparser.ParsingError as parse_error:
logging.critical("Error parsing config file: Impossible to parse file %s. Message: %s"
% (parse_error.source, parse_error.message))
sys.exit(2)
except configparser.Error as config_error:
logging.critical("Error parsing config file: %s." % config_error.message)
sys.exit(2)
def get_config(self, section, key, default=None, value_type=None):
value = default
try:
if self.config.has_section(section):
if self.config.has_option(section, key):
if value_type is not None:
# get value based on type of default value
if value_type is int:
value = self.config.getint(section, key)
elif value_type is float:
value = self.config.getfloat(section, key)
elif value_type is bool:
value = self.config.getboolean(section, key)
else:
value = self.config.get(section, key)
else:
# use string as default
value = self.config.get(section, key)
else:
# raise exception as both sections are mandatory sections (Mail + Telegram)
logging.warning("Get config value error for '%s'.'%s' (default: '%s'): Missing section '%s'."
% (section, key, default, section))
raise configparser.NoSectionError(section)
except configparser.Error as config_error:
logging.critical("Error parsing config file: %s." % config_error.message)
raise config_error
except Exception as get_val_error:
logging.critical("Get config value error for '%s'.'%s' (default: '%s'): %s."
% (section, key, default, get_val_error))
raise get_val_error
return value
class TelegramBot:
config: Config = None
def __init__(self, config):
self.config = config
@staticmethod
def cleanup_html(message):
"""
Parse HTML message and remove HTML elements not supported by Telegram
"""
# supported tags
# https://core.telegram.org/bots/api#sendmessage
# <b>bold</b>, <strong>bold</strong>
# <i>italic</i>, <em>italic</em>
# <u>underline</u>, <ins>underline</ins>
# <s>strikethrough</s>, <strike>strikethrough</strike>, <del>strikethrough</del>
# <b>bold <i>italic bold <s>italic bold strikethrough</s> <u>underline italic bold</u></i> bold</b>
# <a href="http://www.example.com/">inline URL</a>
# <a href="tg://user?id=123456789">inline mention of a user</a>
# <code>inline fixed-width code</code>
# <pre>pre-formatted fixed-width code block</pre>
# <pre><code class="language-python">pre-formatted fixed-width code block
# written in the Python programming language</code></pre>
# extract HTML body to get payload from mail
tg_body = re.sub('.*<body[^>]*>(?P<body>.*)</body>.*$', '\g<body>', message,
flags=(re.DOTALL | re.MULTILINE | re.IGNORECASE))
# remove control chars
tg_body = "".join(ch for ch in tg_body if "C" != unicodedata.category(ch)[0])
# remove all HTML comments
tg_body = re.sub(r'<!--.*?-->', '', tg_body, flags=(re.DOTALL | re.MULTILINE))
# replace img elements by their alt/title attributes
tg_body = re.sub(r'<\s*img\s+[^>]*?((title|alt)\s*=\s*"(?P<alt>[^"]+)")?[^>]*?/?\s*>', '\g<alt>', tg_body,
flags=(re.DOTALL | re.MULTILINE | re.IGNORECASE))
# remove multiple line breaks and spaces (regular Browser logic)
tg_body = re.sub(r'[\r\n]', '', tg_body)
tg_body = re.sub(r'\s[\s]+', ' ', tg_body).strip()
# remove attributes from elements but href of "a"- elements
tg_msg = re.sub(r'<\s*?(?P<elem>\w+)\b\s*?[^>]*?(?P<ref>\s+href\s*=\s*"[^"]+")?[^>]*?>',
'<\g<elem>\g<ref>>', tg_body, flags=(re.DOTALL | re.MULTILINE | re.IGNORECASE))
# remove style and script elements/blocks
tg_msg = re.sub(r'<\s*(?P<elem>script|style)\s*>.*?</\s*(?P=elem)\s*>',
'', tg_msg, flags=(re.DOTALL | re.MULTILINE | re.IGNORECASE))
# preserve NBSPs
tg_msg = re.sub(r' ', ' ', tg_msg, flags=re.IGNORECASE)
# translate paragraphs and line breaks (block elements)
tg_msg = re.sub(r'</?\s*(?P<elem>(p|div|table|h\d+))\s*>', '\n', tg_msg, flags=(re.MULTILINE | re.IGNORECASE))
tg_msg = re.sub(r'</\s*(?P<elem>(tr))\s*>', '\n', tg_msg, flags=(re.MULTILINE | re.IGNORECASE))
tg_msg = re.sub(r'</?\s*(br)\s*[^>]*>', '\n', tg_msg, flags=(re.MULTILINE | re.IGNORECASE))
# prepare list items (migrate list items to "- <text of li element>")
tg_msg = re.sub(r'(<\s*[ou]l\s*>[^<]*)?<\s*li\s*>', '\n- ', tg_msg, flags=(re.MULTILINE | re.IGNORECASE))
tg_msg = re.sub(r'</\s*li\s*>([^<]*</\s*[ou]l\s*>)?', '\n', tg_msg, flags=(re.MULTILINE | re.IGNORECASE))
# remove unsupported tags
regex_filter_elem = re.compile('<\s*(?!/?(bold|strong|i|em|u|ins|s|strike|del|b|a|code|pre))\s*[^>]*?>',
flags=re.MULTILINE)
tg_msg = re.sub(regex_filter_elem, ' ', tg_msg)
tg_msg = re.sub(r'</?\s*(img|span)\s*[^>]*>', '', tg_msg, flags=(re.DOTALL | re.MULTILINE | re.IGNORECASE))
# remove empty links
tg_msg = re.sub(r'<\s*a\s*>(?P<link>[^<]*)</\s*a\s*>', '\g<link> ', tg_msg,
flags=(re.DOTALL | re.MULTILINE | re.IGNORECASE))
# remove links without text (tracking stuff, and none clickable)
tg_msg = re.sub(r'<\s*a\s*[^>]*>\s*</\s*a\s*>', ' ', tg_msg,
flags=(re.DOTALL | re.MULTILINE | re.IGNORECASE))
# remove empty elements
tg_msg = re.sub(r'<\s*\w\s*>\s*</\s*\w\s*>', ' ', tg_msg, flags=(re.DOTALL | re.MULTILINE))
return tg_msg
def send_message(self, mails):
"""
Send mail data over Telegram API to chat/user.
"""
try:
bot: telegram.Bot = telegram.Bot(self.config.tg_bot_token)
tg_chat: telegram.Chat = bot.get_chat(self.config.tg_forward_to_chat_id)
# get chat title
tg_chat_title = tg_chat.full_name
if not tg_chat_title:
tg_chat_title = tg_chat.title
if not tg_chat_title:
tg_chat_title = tg_chat.description
for mail in mails:
try:
if self.config.tg_markdown_version == 2:
parser = telegram.ParseMode.MARKDOWN_V2
else:
parser = telegram.ParseMode.MARKDOWN
if mail.type == MailData.HTML:
parser = telegram.ParseMode.HTML
if self.config.tg_forward_mail_content or not self.config.tg_forward_attachment:
# send mail content (summary)
message = mail.summary
tg_message = bot.send_message(chat_id=self.config.tg_forward_to_chat_id,
parse_mode=parser,
text=message,
disable_web_page_preview=False)
logging.info("Mail summary for '%s' was sent with ID '%i' to '%s' (ID: '%i')"
% (mail.mail_subject, tg_message.message_id,
tg_chat_title, self.config.tg_forward_to_chat_id))
if self.config.tg_forward_attachment and len(mail.attachments) > 0:
for attachment in mail.attachments:
subject = mail.mail_subject
if mail.type == MailData.HTML:
file_name = attachment.name
caption = '<b>' + subject + '</b>:\n' + file_name
else:
file_name = telegram.utils.helpers.escape_markdown(
text=attachment.name, version=self.config.tg_markdown_version)
caption = '*' + subject + '*:\n' + file_name
tg_message = bot.send_document(chat_id=self.config.tg_forward_to_chat_id,
parse_mode=parser,
caption=caption,
document=attachment.file,
filename=attachment.name,
disable_content_type_detection=False)
logging.info("Attachment '%s' was sent with ID '%i' to '%s' (ID: '%s')"
% (attachment.name, tg_message.message_id,
tg_chat_title, str(self.config.tg_forward_to_chat_id)))
except telegram.TelegramError as tg_mail_error:
msg = "Failed to send Telegram message (UID: %s) to '%s': %s" \
% (mail.uid, tg_mail_error.message, str(self.config.tg_forward_to_chat_id))
logging.critical(msg)
try:
# try to send error via telegram, and ignore further errors
bot.send_message(chat_id=self.config.tg_forward_to_chat_id,
parse_mode=telegram.ParseMode.MARKDOWN_V2,
text=telegram.utils.helpers.escape_markdown(msg, version=2),
disable_web_page_preview=False)
finally:
pass
pass
except Exception as send_mail_error:
error_msgs = [Tool.binary_to_string(arg) for arg in send_mail_error.args]
msg = "Failed to send Telegram message (UID: %s) to '%s': %s"\
% (mail.uid, str(self.config.tg_forward_to_chat_id), ', '.join(error_msgs))
logging.critical(msg)
try:
# try to send error via telegram, and ignore further errors
bot.send_message(chat_id=self.config.tg_forward_to_chat_id,
parse_mode=telegram.ParseMode.MARKDOWN_V2,
text=telegram.utils.helpers.escape_markdown(msg, version=2),
disable_web_page_preview=False)
finally:
pass
pass
except telegram.TelegramError as tg_error:
logging.critical("Failed to send Telegram message: %s" % tg_error.message)
return False
except Exception as send_error:
error_msgs = [Tool.binary_to_string(arg) for arg in send_error.args]
logging.critical("Failed to send Telegram message: %s" % ', '.join(error_msgs))
return False
return True
class MailAttachment:
idx = 0
name = ''
file = None
class MailBody:
text = ''
html = ''
attachments = [MailAttachment]
class MailData:
TEXT = 1
HTML = 2
uid = ''
raw = ''
type = TEXT
summary = ''
mail_from = ''
mail_subject = ''
mail_body = ''
attachment_summary = ''
attachments = [MailAttachment]
class Mail:
mailbox: imaplib2.IMAP4_SSL = None
config: Config = None
last_uid: str = ''
previous_error = None
class MailError(Exception):
def __init__(self, message, errors=None):
super().__init__(message)
self.errors = errors
def __init__(self, config):
"""
Login to remote IMAP server.
"""
self.config = config
try:
self.mailbox = imaplib2.IMAP4_SSL(host=config.imap_server,
port=config.imap_port,
timeout=config.imap_timeout)
rv, data = self.mailbox.login(config.imap_user, config.imap_password)
if rv != 'OK':
return
except socket.gaierror as gai_error:
msg = "Connection error '%s:%i': %s" % (config.imap_server,
config.imap_port,
gai_error.strerror)
logging.error(msg)
raise self.MailError(msg, gai_error)
except imaplib2.IMAP4_SSL.error as imap_ssl_error:
error_msgs = [Tool.binary_to_string(arg) for arg in imap_ssl_error.args]
msg = "Login to '%s:%i' failed: %s" % (config.imap_server,
config.imap_port,
', '.join(error_msgs))
logging.error(msg)
raise self.MailError(msg, imap_ssl_error)
except Exception as login_error:
msg = "Mail error during connection to '%s:%i' failed: %s" \
% (config.imap_server, config.imap_port, ', '.join(map(str, login_error.args)))
logging.error(msg)
raise self.MailError(msg, login_error)
rv, mailboxes = self.mailbox.list()
if rv != 'OK':
self.disconnect()
msg = "Can't get list of available mailboxes / folders: %s" % str(rv)
raise self.MailError(msg)
else:
logging.info("Mailboxes:")
logging.info(mailboxes)
rv, data = self.mailbox.select(config.imap_folder)
if rv == 'OK':
logging.info("Processing mailbox...")
else:
msg = "ERROR: Unable to open mailbox: %s" % str(rv)
logging.error(msg)
raise self.MailError(msg)
def disconnect(self):
if self.mailbox is not None:
try:
self.mailbox.close()
self.mailbox.logout()
except Exception as ex:
logging.debug("Cannot close mailbox: %s" % ', '.join(ex.args))
pass
finally:
del self.mailbox
@staticmethod
def decode_body(msg) -> MailBody:
"""
Get payload from message and return structured body data
"""
html_part = None
text_part = None
attachments = []
index = 1
for part in msg.walk():
if part.get_content_type().startswith('multipart/'):
continue
elif part.get_content_type() == 'text/plain':
text_part = part.get_payload(decode=True)
encoding = part.get_content_charset()
if not encoding:
encoding = 'utf-8'
text_part = bytes(text_part).decode(encoding).strip()
elif part.get_content_type() == 'text/html':
html_part = part.get_payload(decode=True)
encoding = part.get_content_charset()
if not encoding:
encoding = 'utf-8'
html_part = bytes(html_part).decode(encoding).strip()
elif part.get_content_type() == 'message/rfc822':
continue
elif part.get_content_type() == 'text/calendar':
attachment = MailAttachment()
attachment.idx = index
attachment.name = 'invite.ics'
attachment.file = part.get_payload(decode=True)
attachments.append(attachment)
index += 1
elif part.get_content_charset() is None and part.get_content_disposition() == 'attachment':
attachment = MailAttachment()
attachment.idx = index
attachment.name = str(part.get_filename())
attachment.file = part.get_payload(decode=True)
attachments.append(attachment)
index += 1
body = MailBody()
body.text = text_part
body.html = html_part
body.attachments = attachments
return body
def get_last_uid(self):
"""
get UID of most recent mail
"""
rv, data = self.mailbox.select(config.imap_folder)
rv, data = self.mailbox.uid('search', None, 'UID *')
if rv != 'OK':
logging.info("No messages found!")
return ''
return Tool.binary_to_string(data[0])
def parse_mail(self, uid, mail):
"""
parse data from mail like subject, body and attachments and return structured mail data
"""
try:
msg = email.message_from_bytes(mail)
# decode body data (text, html, multipart/attachments)
body = self.decode_body(msg)
message_type = MailData.TEXT
content = ''
if self.config.tg_forward_mail_content:
# remove useless content
content = body.text.replace('()', '').replace('[]', '').strip()
if self.config.tg_prefer_html:
# Prefer HTML
if body.html:
message_type = MailData.HTML
content = TelegramBot.cleanup_html(body.html)
elif body.text:
content = telegram.utils.helpers.escape_markdown(text=content,
version=self.config.tg_markdown_version)
else:
if body.text:
content = telegram.utils.helpers.escape_markdown(text=content,
version=self.config.tg_markdown_version)
elif body.html:
message_type = MailData.HTML
content = TelegramBot.cleanup_html(body.html)
if content:
# remove multiple line breaks (keeping up to 1 empty line)
content = re.sub(r'(\s*\r?\n){2,}', "\n\n", content)
if message_type == MailData.HTML:
# add space after links (provide space for touch on link lists)
# '<' keep mail marker together (ex.: <<a href="mailto:[email protected]">[email protected]</a>>)
content = re.sub(r'(?P<a></a>(\s*>)?)\s*', '\g<a>\n\n', content, flags=re.MULTILINE)
# remove spaces and line breaks on start and end (enhanced strip)
content = re.sub(r'^[\s\n]*', '', content)
content = re.sub(r'[\s\n]*$', '', content)
max_len = self.config.imap_max_length
content_len = len(content)
if message_type == MailData.HTML:
# get length from parsed HTML (all tags removed)
content_plain = re.sub(r'<[^>]*>', '', content, flags=re.MULTILINE)
# get new max length based on plain text factor
plain_factor = (len(content_plain) / content_len) + float(1)
max_len = int(max_len * plain_factor)
if content_len > max_len:
content = content[:max_len]
if message_type == MailData.HTML:
# remove incomplete html tag
content = re.sub(r'<(\s*\w*(\s*[^>]*?)?(</[^>]*)?)?$', '', content)
else:
# remove last "\"
content = re.sub(r'\\*$', '', content)
content += "... (first " + str(max_len) + " characters)"
# attachment summary
attachments_summary = ""
if body.attachments:
if message_type == MailData.HTML:
attachments_summary = "\n\n" + chr(10133) + \
" <b>" + str(len(body.attachments)) + " attachments:</b>\n"
else:
attachments_summary = "\n\n" + chr(10133) + \
" **" + str(len(body.attachments)) + " attachments:**\n"
for attachment in body.attachments:
if message_type == MailData.HTML:
file_name = attachment.name
else:
file_name = telegram.utils.helpers.escape_markdown(
text=attachment.name, version=self.config.tg_markdown_version)
attachments_summary += "\n " + str(attachment.idx) + ": " + file_name
# subject
subject = ''
for subject_part in email.header.decode_header(msg['Subject']):
part, encoding = subject_part
subject += Tool.binary_to_string(part, encoding=encoding)
# build summary
mail_from = Tool.binary_to_string(msg['From'])
if self.config.tg_forward_mail_content:
summary_line = "\n=============================\n"
else:
summary_line = "\n"
if message_type == MailData.HTML:
mail_from = html.escape(mail_from, quote=True)
email_text = "<b>From:</b> " + mail_from + "\n<b>Subject:</b> "
else:
subject = telegram.utils.helpers.escape_markdown(text=subject,
version=self.config.tg_markdown_version)
mail_from = telegram.utils.helpers.escape_markdown(text=mail_from,
version=self.config.tg_markdown_version)
summary_line = telegram.utils.helpers.escape_markdown(text=summary_line,
version=self.config.tg_markdown_version)
email_text = "*From:* " + mail_from + "\n*Subject:* "
email_text += subject + summary_line + content + " " + attachments_summary
mail_data = MailData()
mail_data.uid = uid
mail_data.raw = msg
mail_data.type = message_type
mail_data.mail_from = mail_from
mail_data.mail_subject = subject
mail_data.mail_body = content
mail_data.summary = email_text
mail_data.attachment_summary = attachments_summary
mail_data.attachments = body.attachments
return mail_data
except Exception as parse_error:
if len(parse_error.args) > 0:
logging.critical("Cannot process mail: " + parse_error.args[0])
else:
logging.critical("Cannot process mail: " + parse_error.__str__())
return None
def search_mails(self):
"""
Search mail on remote IMAP server and return list of parsed mails.
"""
if self.last_uid is None or self.last_uid == '':
self.last_uid = self.get_last_uid()
logging.info("Most recent UID: %s" % self.last_uid)
# build IMAP search string
search_string = self.config.imap_search
if not search_string:
"(UID " + str(self.last_uid) + ":*)"
else:
search_string = re.sub(r'\${lastUID}', str(self.last_uid), search_string)
if re.match(r'.*\bUID\b\s*:.*', search_string) and self.last_uid == '':
# empty mailbox
return
try:
rv, data = self.mailbox.select(self.config.imap_folder)
rv, data = self.mailbox.uid('search', None, search_string)
if rv != 'OK':
logging.info("No messages found!")
return
except imaplib2.IMAP4_SSL.error as search_error:
error_msgs = [Tool.binary_to_string(arg) for arg in search_error.args]
msg = "Search with '%s' returned: %s" % (search_string, ', '.join(error_msgs))
if msg != self.previous_error:
logging.error(msg)
self.previous_error = msg
return
mails = []
max_num = int(self.last_uid)
for num in sorted(data[0].split()):
current_uid = int(Tool.binary_to_string(num))
if current_uid > max_num:
try:
rv, data = self.mailbox.select(self.config.imap_folder)
rv, data = self.mailbox.uid('fetch', num, '(RFC822)')
if rv != 'OK':
logging.error("ERROR getting message", num)
return
msg_raw = data[0][1]
mail = self.parse_mail(Tool.binary_to_string(num), msg_raw)
if mail is None:
logging.error("Can't parse mail with UID: %s" % num)
else:
mails.append(mail)
except Exception as mail_error:
logging.critical("Cannot process mail: %s" % ', '.join(map(str, mail_error.args)))
finally:
# remember new UID for next loop
max_num = current_uid
if len(mails) > 0:
self.last_uid = str(max_num)
logging.info("Got %i new mail(s) to forward, changed UID to %s" % (len(mails), self.last_uid))
return mails
class SystemdHandler(logging.Handler):
"""
Class to handle logging options.
"""
PREFIX = {
logging.CRITICAL: "<2> " + __appname__ + ": ",
logging.ERROR: "<3> " + __appname__ + ": ",
logging.WARNING: "<4> " + __appname__ + ": ",
logging.INFO: "<6> " + __appname__ + ": ",
logging.DEBUG: "<7> " + __appname__ + ": ",
logging.NOTSET: "<7> " + __appname__ + ": ",
}
def __init__(self, stream=sys.stdout):
self.stream = stream
logging.Handler.__init__(self)
def emit(self, record):
try:
msg = self.PREFIX[record.levelno] + self.format(record) + "\n"
self.stream.write(msg)
self.stream.flush()
except Exception as emit_error:
self.handleError(record)
if len(emit_error.args) > 0:
print("ERROR: SystemdHandler.emit failed with: " + emit_error.args[0])
else:
print("ERROR: SystemdHandler.emit failed with: " + emit_error.__str__())
def main():
"""
Run the main program
"""
root_logger = logging.getLogger()
root_logger.setLevel("INFO")
root_logger.addHandler(SystemdHandler())
args_parser = argparse.ArgumentParser(description='Mail to Telegram Forwarder')
args_parser.add_argument('-c', '--config', type=str, help='Path to config file', required=True)
cmd_args = args_parser.parse_args()
if cmd_args.config is None:
logging.warning("Could not load config file, as no config file was provided.")
sys.exit(2)
mailbox = None
try:
config = Config(cmd_args)
tg_bot = TelegramBot(config)
mailbox = Mail(config)
# Keep polling
while True:
if mailbox is None:
mailbox = Mail(config)
mails = mailbox.search_mails()
if config.imap_disconnect:
# if not reuse previous connection
mailbox.disconnect()
# send mail data via TG bot
if mails is not None and len(mails) > 0:
tg_bot.send_message(mails)
if config.imap_push_mode:
logging.info("IMAP IDLE mode")
else:
time.sleep(float(config.imap_refresh))
except KeyboardInterrupt:
logging.critical('Stopping user aborted with CTRL+C')
except Mail.MailError: # ignore errors already handled by Mail- Class
pass
except Exception as main_error:
if len(main_error.args) > 0:
logging.critical('Error occurred: %s' % ', '.join(map(str, main_error.args)))
else:
logging.critical('Error occurred: ' + main_error.__str__())
finally:
if mailbox is not None:
mailbox.disconnect()
logging.info('Mail to Telegram Forwarder stopped!')
main()