树莓派笔记2:华为E353s用python转发短信到邮箱

2018-12-29 RaspberryPi

环境:

华为E353s联通3G上网卡
Raspbian Stretch Lite
Gammu version 1.38.1
Gammu-smsd version 1.38.1
Python 2.7.13

安装gammu:

sudo apt-get install gammu
sudo apt-get install usb-modeswitch
sudo apt-get install ppp

配置usb-modeswitch:

sudo nano /etc/usb_modeswitch.d/huawei.conf

粘贴以下代码,CTRL+X保存:

########################################################
# Huawei, newer modems

DefaultVendor=0x12d1
DefaultProduct=0x14fe

TargetVendor=0x12d1
TargetProductList="1001,1406,140b,140c,1412,141b,1433,14ac,1506"

CheckSuccess=20

MessageContent="55534243123456780000000000000011062000000100000000000000000000"

生效配置文件:

/usr/sbin/usb_modeswitch -c /etc/usb_modeswitch.d/huawei.conf

70-huawei-3G-dongle.rules:

sudo nano /etc/udev/rules.d/70-huawei-3G-dongle.rules

粘贴以下代码,CTRL+X保存:

# add for HUAWEI E353 3G dongle

# usb_modeswitch from 12d1:14fe ==> 12d1:1506
ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="12d1", ATTR{idProduct}=="14fe", MODE="0666", RUN+="/usr/sbin/usb_modeswitch -c /etc/usb_modeswitch.d/huawei.conf"

重启生效。

配置gammu:

sudo gammu-config
->/dev/ttyUSB0
sudo gammu --identify
#echo "test" | sudo gammu sendsms TEXT 186xxxxxxxx

配置gammu-smsd:

sudo apt-get install gammu-smsd
sudo nano /etc/gammu-smsdrc

粘贴以下代码,CTRL+X保存:

[gammu]
port = /dev/ttyUSB0
connection = at

[smsd]
OutboxFormat = unicode
TransmitFormat = unicode
RunOnReceive = sudo python /home/pi/sms/rec_sms.py
service = files
logfile = /home/pi/sms.log
debuglevel = 0
inboxpath = /home/pi/sms/inbox/
outboxpath = /home/pi/sms/outbox/
sentsmspath = /home/pi/sms/sent/
errorsmspath = /home/pi/sms/error/
PIN = 1234

gammu-smsd使用的文本模式,建立文件夹:

mkdir -p sms/inbox sms/outbox sms/sent sms/error

启动gammu-smsd:

sudo gammu-smsd --config /etc/gammu-smsdrc --pid /var/run/gammu-smsd.pid --daemon --user gammu --group gammu

调试代码:

#简单发信方式
#echo "SAFSDFE"|sudo gammu-smsd-inject TEXT 186XXXXXXXX
#中文短信发送
#echo "中国功夫安抚阿斯蒂芬"|sudo gammu-smsd-inject TEXT 186XXXXXXXX -unicode
#查看gammu-smsd状态
#sudo gammu-smsd-monitor

安装PYTHON需要模块:

sudo apt-get install python-pip
sudo pip install yagmail

转发邮件脚本/home/pi/sms/rec_sms.py代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#接受短信并用QQ企业邮箱转发
#如果转发失败。则使用短信转发到备用手机号

import sys
import yagmail

sms_inbox_PATH = '/home/pi/sms/inbox/'
sms_outbox_PATH = '/home/pi/sms/outbox/'
sms_safe_phone = '186XXXXXXXX'

#https://wammu.eu/docs/manual/smsd/files.html#gammu-smsd-files
#The filename will be IN<date>_<time>_<serial>_<sender>_<sequence>.<ext>, 
#单条短信:rec_sms.py IN20181229_171215_00_+86186XXXXXXXX_00.txt
#长短信:rec_sms.py IN20181229_171215_00_+86186XXXXXXXX_00.txt IN20181229_171215_00_+86186XXXXXXXX_01.txt

#如果大于2个参数。则为多条短信。合并
numparts = len(sys.argv)

#首个参数
my_argv = sys.argv[1]
my_argv1 = my_argv[2:-4]
my_argv2 = my_argv1.split('_')

#短信值
sms_phone = my_argv2[3]
sms_time = my_argv2[0][0:4] + '-' + my_argv2[0][4:6] + '-' + my_argv2[0][6:8] + ' ' + my_argv2[1][0:2] + ':' + my_argv2[1][2:4] + ':'+ my_argv2[0][4:6]
sms_text = ''

#发信人和时间写入邮件正文
sms_text = '发件人:'+sms_phone
sms_text = sms_text + '\n收信时间:'+ sms_time + ' \n\n'


#普通短信处理
if numparts == 2:
    with open( sms_inbox_PATH + my_argv, 'r') as f:
        sms_text = sms_text + f.read()

#长条短信合并
if numparts > 2:
    for i in range(1, numparts ):
        with open( sms_inbox_PATH + sys.argv[i], 'r') as f:
            sms_text = sms_text +  f.read()


# 邮箱账号信息
from_addr = 'pi@xx.com'
password1 = 'XBeXXXXXXTFYX' #授权码
to_addr = 'm@xx.com'
smtp_server = 'smtp.exmail.qq.com' # 腾讯服务器地址

try:
    #链接邮箱服务器
    yag = yagmail.SMTP( user=from_addr, password=password1, host=smtp_server)
    # 邮箱正文
    contents = [ sms_text ]
    # 发送邮件
    yag.send( to_addr , '邮件标题', contents)

except:
    #如果邮箱发送失败,尝试短信发送
    tip = 'mail error.\n\n'
    #文件新建放入发件箱,发送短信,有几秒延迟
    #OUT<priority><date>_<time>_<serial>_<recipient>_<note>.<ext>
    #发件箱短信文件格式:OUTC20181231_104630_123_+86186XXXXXXXX_note.txt
    outbox_filename = sms_outbox_PATH + 'OUTC' + my_argv2[0][0:8] +'_' + my_argv2[1][0:6] +'_123_' + sms_phone +'_note.txt'
    with open( outbox_filename , "w", ) as f:
        f.write( tip + sms_text )

部分参考内容:

https://wammu.eu/docs/manual/
https://blog.csdn.net/houxn22/article/details/38439107
https://wiki.archlinux.org/index.php/Huawei_E1550_3G_modem
https://serverfault.com/questions/611577/gammu-smsd-inject-dont-send-utf-8-characters-%C3%A6%C3%B8%C3%A5