[TOC]

1. email 邮件命令

CentOS/RHEL安装命令:

1
yum -y install mailx

mail 配置:
vim /etc/mail.rc

1
2
3
4
5
6
7
8
9
10
11
12
13
#配置参数
from: 对方收到邮件时显示的发件人
smtp: 指定第三方发送邮件的smtp服务器地址
smtp-auth: SMTP的认证方式。默认是LOGIN,也可改为CRAM-MD5或PLAIN方式
smtp-auth-user: 第三方发邮件的用户名
smtp-auth-password: 用户名对应密码

#配置详情
set from="[email protected]"
set smtp="smtp.qq.com"
set smtp-auth-user="[email protected]"
set smtp-auth-password="aawbubhyfhuucbdd"
set smtp-auth=login

mail 命令:
mail –h
-s <邮件主题>:指定邮件的主题;
-c <地址>:添加邮件抄送人,多个人时用逗号隔开;
-b <地址>:添加邮件暗送人;
-a <附件>: 添加附件。
注:部分系统参数稍有差异,最好看帮助

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 语法:
mail -s "主题" 收件地址 # 无邮件正文
mail -s "主题" 收件地址< 文件(邮件正文.txt) # 有邮件正文
echo "邮件正文" | mail -s 邮件主题 收件地址
cat 邮件正文.txt | mail -s 邮件主题 收件地址
mail -s "主题" -a 附件 收件地址 < 文件(邮件正文.txt) # 带附件


#案例:
% mail -s "测试" [email protected]
% mail -s "邮件主题" [email protected] < /data/findyou.txt
% echo "邮件正文内容" | mail -s "邮件主题" [email protected]
% cat /data/findyou.txt | mail -s "邮件主题" [email protected]
% mail -s "邮件主题" -a /data/findyou.tar.gz [email protected] < /data/findyou.txt #注意发信地址放在后面

[[email protected] ~]$ echo "邮件正文内容" | mail -s "mail测试发信" [email protected]
[[email protected] ~]$ echo "test test" | mail -s "测试附件" -a "/var/log/yum.log" [email protected]
WeiyiGeek.测试发信

WeiyiGeek.测试发信

脚本:

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
#!/bin/bash
help(){
echo "eg: $0 [Subject] [address] [content_file] [file]"
echo ""
exit 1
}
if [ ! -n "$1" ] ; then
help
fi

cDate=`date +%Y%m%d`
if [ ! -n "$2" ] ; then
help
else
mail_to=$2
echo " Send Mail to ${mail_to}"
fi


if [ ! -n "$4" ] ; then
mail -s $1 ${mail_to}<$3
else
mail -s $1 -a $4 ${mail_to}<$3
fi

[[email protected]]$ ./sendmail.sh test [email protected] abc.txt
Send Mail to [email protected]


F&Q

邮件服务启动:

1
systemctl status postfix

问题1:sendmail重启报libmysqlclient.so.18不存在
问题描述:

1
2
sendmail restart
sendmail: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory

问题原因:
1
2
3
4
5
6
[[email protected] ld.so.conf.d]# ldd /usr/sbin/sendmail
linux-vdso.so.1 => (0x00007ffce43fa000)
libldap-2.4.so.2 => /lib64/libldap-2.4.so.2 (0x00007f97fc570000)
liblber-2.4.so.2 => /lib64/liblber-2.4.so.2 (0x00007f97fc361000)
libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f97fc0ff000)
libmysqlclient.so.18 => not found #发现缺少该库

问题解决:
1
2
3
4
#下载该动态链接库和重新刷新加载lib
sudo wget -O /usr/lib/libmysqlclient.so.18 http://files.directadmin.com/services/es_7.0_64/libmysqlclient.so.18
#echo "/usr/lib/" >>/etc/ld.so.conf
ldconfig