使用LEDE/OPENWRT 路由器自动发送邮件
今天研究实现了手动或者定时自动通过LEDE/openwrt路由对外发送邮件的功能。
1.安装软件包。
opkg update opkg install mutt ssmtp ca-certificates ca-bundle
2.配置ssmtp
vi /etc/ssmtp/ssmtp.conf
内容为:
# # /etc/ssmtp.conf -- a config file for sSMTP sendmail. # # The person who gets all mail for userids < 1000 # Make this empty to disable rewriting. root=MY@TEST.com # The place where the mail goes. The actual machine name is required # no MX records are consulted. Commonly mailhosts are named mail.domain.com # The example will fit if you are in domain.com and your mailhub is so named. # 不同邮箱供应商的配置不同,需查阅供应商手册 mailhub=smtp.TEST.com:465 # Example for SMTP port number 2525 # mailhub=mail.your.domain:2525 # Example for SMTP port number 25 (Standard/RFC) # mailhub=mail.your.domain # Example for SSL encrypted connection # mailhub=mail.your.domain:465 # Where will the mail seem to come from? rewriteDomain=TEST.com # The full hostname hostname=TEST.com # Set this to never rewrite the "From:" line (unless not given) and to # use that address in the "from line" of the envelope. FromLineOverride=YES # Use SSL/TLS to send secure messages to server. UseTLS=YES # Use SSL/TLS certificate to authenticate against smtp host. #UseTLSCert=YES # Use this RSA certificate. #TLSCert=/etc/ssl/certs/ssmtp.pem # Get enhanced (*really* enhanced) debugging information in the logs # If you want to have debugging of the config file parsing, move this option # to the top of the config file and uncomment #Debug=YES
然后:
vi /etc/ssmtp/revaliases
内容为:
# sSMTP aliases # # Format: local_account:outgoing_address:mailhub # # Example: root:your_login@your.domain:mailhub.your.domain[:port] # where [:port] is an optional port number that defaults to 25. root:MY@TEST.com:smtp.TEST.com:465
3.配置mutt
vi ~/.muttrc
内容为:
mailboxes /tmp/mail set sendmail="ssmtp -v -auMY@TEST.com -apMYPASSWORD" set from="MY@TEST.com" # Mail folder setup. set folder=/tmp/mail set mbox_type=mbox set spoolfile=+inbox set mbox=+received set postponed=+postponed set record=+sent
4.编写脚本sendmail.sh
vi ./sendmail.sh chmod +x ./sendmail.sh
内容为:
mkdir /tmp/mail cat /tmp/sysinfo/model | mutt -s "Mail from Router of Coffeecat" receiver@TEST.com -a 'attachment.txt'
然后运行:
./sendmail.sh
就可以看到结果了(如果是从vip.qq.com发送的话):
[<-] 220 smtp.qq.com Esmtp QQ Mail Server [->] EHLO vip.qq.com [<-] 250 8BITMIME [->] AUTH LOGIN [<-] 334 XXYlcm5hbDF6 [->] Z2FyACDDbTT4dBB2aXAucWSuY29t [<-] 334 REWzc3dverH6 [<-] 235 Authentication successful [->] MAIL FROM:<MY@TEST.com> [<-] 250 Ok [->] RCPT TO:<reciever@TEST.com> [<-] 250 Ok [->] DATA [<-] 354 End data with <CR><LF>.<CR><LF> [->] Received: by vip.qq.com (sSMTP sendmail emulation); Tue, 30 Jan 2018 22:25:31 +0800 [->] Date: Tue, 30 Jan 2018 22:25:31 +0800 [->] From: root <MY@TEST.com> [->] To: reciever@TEST.com [->] Subject: Mail from Router of Coffeecat [->] Message-ID: <20180130142531.GA1968@Exciting.lan> [->] MIME-Version: 1.0 [->] Content-Type: multipart/mixed; boundary="Nq2Wo0NMKNjxTN9z" [->] Content-Disposition: inline [->] User-Agent: Mutt/1.9.2 (2017-12-15) [->] [->] [->] --Nq2Wo0NMKNjxTN9z [->] Content-Type: text/plain; charset=us-ascii [->] Content-Disposition: inline [->] [->] HiWiFi HC5962 [->] [->] --Nq2Wo0NMKNjxTN9z [->] Content-Type: text/plain; charset=us-ascii [->] Content-Disposition: attachment; filename="maclist.txt" [->] ....... [->] [->] --Nq2Wo0NMKNjxTN9z-- [->] . [<-] 250 Ok: queued as [->] QUIT [<-] 221 Bye