sendEmail 的一个小应用

sendEmail 的安装

粗放的安装就是直接网上down个安装包然后解压建目录赋权,然后就偶了,具体如下

#下载安装包
wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
#创建目录
mkdir -p /usr/local/bin
#解压
tar zxf sendEmail-v1.56.tar.gz -C /usr/src/
#进入解压目录
cd /usr/src/sendEmail-v1.56/
#复制程序到指定目录
cp -a sendEmail /usr/local/bin/
#给执行权限
chmod +x /usr/local/bin/sendEmail
#安装组件
yum install perl-Net-SSLeay perl-IO-Socket-SSL -y
需要注意的是CentOS 7默认安装的是perl 5.16.3 ,网上有的文章说sendEmail-v1.56必须要运行在perl 5.10.1上,我实际测试的结果是CentOS7 用5.16.3没问题,CentOS 6.10运行是perl 5.10.1.

sendEmail 的使用说明

例如运行如下命令

/usr/local/bin/sendEmail -f  [email protected] -t [email protected] -s smtp.126.com -u "我是邮件主题" -xu [email protected]  -xp xxxxxx -m "test-1234567890" -l /var/log/sendMyEmail.log

具体含义如下:
/usr/local/bin/sendEmail 命令主程序
-f [email protected] 发件人邮箱
-t [email protected] 收件人邮箱
-s smtp.126.com 发件人邮箱的smtp服务器,默认用25端口,有的禁止了25端口则需要用465端口, 我暂时 没试成功465端口,25端口试成功了
-u “我是邮件主题” 邮件的标题
-o message-content-type=html 邮件内容的格式,html表示它是html格式
-o message-charset=utf8 邮件内容编码
-xu [email protected] 发件人邮箱的用户名
-xp xxxxxx 发件人邮箱密码
-m “我是邮件内容” 邮件的具体内容
-l /var/log/sendMyEmail.log 非必输项,邮件发送日志记录到日志文

下面是sendEmail的帮助说明,从网上找的写的很详细

Synopsis: sendEmail -f ADDRESS [options]

Required:
-f ADDRESS from (sender) email address //发送人地址,除此之外至少使用一个-t或者-cc或者-bcc,邮件消息主体必须至少使用一个-m,或者标准输入读取,或者-o message-file
* At least one recipient required via -t, -cc, or -bcc
* Message body required via -m, STDIN, or -o message-file=FILE

Common:
-t ADDRESS [ADDR …] to email address(es) //收件人地址
-u SUBJECT message subject //邮件标题
-m MESSAGE message body //邮件正文
-s SERVER[:PORT] smtp mail relay, default is localhost:25 //所使用发送邮件服务器(SMTP)

Optional:
-a FILE [FILE …] file attachment(s) //添加附件
-cc ADDRESS [ADDR …] cc email address(es) //普通抄送
-bcc ADDRESS [ADDR …] bcc email address(es) //暗抄送,其他人除发件人外看不见被暗抄送者
-xu USERNAME username for SMTP authentication //邮件服务器认证用户名
-xp PASSWORD password for SMTP authentication //邮件服务器认证密码

Paranormal:
-b BINDADDR[:PORT] local host bind address //绑定特定地址和端口号
-l LOGFILE log to the specified file //记录到特定日志文件
-v verbosity, use multiple times for greater effect //显示某些详细信息,用途未验证
-q be quiet (i.e. no STDOUT output) //静默模式,屏蔽屏幕输出
-o NAME=VALUE advanced options, for details try: --help misc //指定特定参数项和其值
-o message-content-type=<auto|text|html|other> //指定正文文本格式,auto,text,html等等
-o message-file=FILE -o message-format=raw //message-file指定邮件正文从文件读取,message-format指定消息正文格
-o message-header=HEADER -o message-charset=CHARSET //message-header指定正文头部,message-charset指定正文字符集
-o reply-to=ADDRESS -o timeout=SECONDS //reply-to回复给指定地址,timeout指定超时时间
-o username=USERNAME -o password=PASSWORD //username指定用户名,password指定密码
-o tls=<auto|yes|no> -o fqdn=FQDN //tls指定安全传输层协议(透明局域网服务?未验证)启用与否 fqdn意思是邮件地址显示全域名(Fully Qualified Domain Name)

Help:
–help the helpful overview you’re reading now //列出帮助信息
–help addressing explain addressing and related options //列出邮件地址类相关选项的帮助
–help message explain message body input and related options //列出邮件正文类选项的帮助
–help networking explain -s, -b, etc //列出网络类选项的帮助
–help output explain logging and other output options //列出输出信息类选项的帮助
–help misc explain -o options, TLS, SMTP auth, and more //列出杂项类帮助

sendEmail 的应用实例

运用sendEmail我做了这样一个小实验,任何用户远程或本地登录后发送邮件信息。
在这里插入图片描述
具体实现如下
编写脚本 /usr/src/scripts/my-server-login-mail.sh

#!/bin/bash
//邮件正文信息
function HEAD {
    Kernel_version=`uname -r`
    Login_user=`last -a | grep "logged in" | wc -l`
    Up_lastime=`date -d "$(awk -F. '{print $1}' /proc/uptime) second ago" +"%Y-%m-%d %H:%M:%S"`
    Up_runtime=`cat /proc/uptime| awk -F. '{run_days=$1 / 86400;run_hour=($1 % 86400)/3600;run_minute=($1 % 3600)/60;run_s
econd=$1 % 60;printf("%d:%d:%d:%d",run_days,run_hour,run_minute,run_second)}'`
    Last_user=`last | awk '(/pts/) && (/-/){print "User: "$1" - ""OlineTime: "$NF" - ""IP: "$3" - ""LoginTime: "$4" "$5" "
$6" " 
$7}'| head -1 | sed -e 's/(//g' -e 's/)//g'`
    echo -e ""
    echo -e "\
    邮件提示: 未知身份来源使用${USER}用户登录系统 
    -------------------------------------------------------------
                            System information
    主机名: $HOSTNAME
    内核版本: $Kernel_version
    系统已运行时间: $Up_runtime
    上一次重启时间: $Up_lastime
    当前登入用户数: $Login_user
    上一次登入用户: $Last_user
    -------------------------------------------------------------
    " 
};HEAD >/tmp/.loginmail
 
 
smtp=smtp.126.com //smtp服务器地址
smtp_auth_user=xxx//发件人邮箱账号
smtp_auth_password=xxxx// 发件人邮箱密码,126邮箱使用的是授权码
[email protected]/发件人邮箱账号
title="主机:`echo $HOSTNAME`登录提示 (`date "+%F %T"`)"//邮件主题
body=`cat /tmp/.loginmail` //邮件正文信息
[email protected] //收件人邮箱

sendEmail -s "$smtp" -xu "${smtp_auth_user}" -xp "${smtp_auth_password}" -f "$from" -t "$to" -u "$title" -m "$body" &>/dev/null && rm -rf /tmp/.loginmail

将脚本绝对路径名添加到/etc/profile
在这里插入图片描述

参考文章链接

  1. https://blog.51cto.com/7424593/1922111
  2. https://blog.51cto.com/888888hfer/2156197?source=dra
  3. http://haibing.org/578
  4. https://www.linuxidc.com/Linux/2017-07/145360.htm
  5. https://www.cnblogs.com/yxy-linux/p/5780302.html
  6. https://blog.csdn.net/ithomer/article/details/83514232
  7. https://www.cnblogs.com/ithomer/p/9873803.html
发布了8 篇原创文章 · 获赞 3 · 访问量 450

猜你喜欢

转载自blog.csdn.net/fire2009/article/details/93497644