From 698d7502b0bf60989ff3524129f50e41b2532ccb Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Tue, 14 Nov 2017 17:28:48 +0100 Subject: [PATCH] Add auto-split long SMS feature --- mail2sms | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/mail2sms b/mail2sms index 5d397cd..ad116de 100755 --- a/mail2sms +++ b/mail2sms @@ -123,6 +123,26 @@ def mail_address_to_phone_number(txt): return False def send_sms(recipient, text): + if len(text) > 160 and options.split: + logging.debug('Text contain more than 160 caracteres : split in multiple SMS') + start=0 + while True: + if start > len(text)-1: + break + if start==0: + stop=start+157 + msg=text[start:stop]+u'...' + else: + stop=start+154 + if stop>=(len(text)-1): + msg=u'...'+text[start:] + else: + msg=u'...'+text[start:stop]+u'...' + + if not send_sms(recipient, msg): + return False + start=stop + return True url_params={ 'phone': recipient, 'text': text.encode('utf8') @@ -139,6 +159,7 @@ def send_sms(recipient, text): '' ] url=urlparse.urlunparse(url_parts) + logging.debug(u'Send SMS using url : %s' % url) try: request=urllib2.urlopen(url, timeout=options.smstimeout) data=request.read() @@ -200,6 +221,12 @@ parser.add_option('-l', dest="logfile", help="Log file path") +parser.add_option('-s', + '--auto-split', + action="store_true", + dest="split", + help="Auto split long SMS by small SMS of 160 characters") + parser.add_option('-b', '--backup-mail', action="store",