-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmail.lib.sh
More file actions
181 lines (152 loc) · 6.13 KB
/
mail.lib.sh
File metadata and controls
181 lines (152 loc) · 6.13 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#
# Copyright (c) 2010-2011 Linagora
# Patrick Guiran <pguiran@linagora.com>
# http://github.com/Tauop/ScriptHelper
#
# ScriptHelper is free software, you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# ScriptHelper is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# README ---------------------------------------------------------------------
# This library helps to deal with mails (prepare and send)
#
# Global variables ===========================================================
# IMPORTANT: Please to write to those variables
# __LIB_MAIL__ : 'Loaded' when the lib is 'source'd
# __MAIL_FILE__ : path the log file in which messages will be stored
# ----------------------------------------------------------------------------
# don't load several times this file
if [ "${__LIB_MAIL__:-}" != 'Loaded' ]; then
__LIB_MAIL__='Loaded'
__MAIL_FILE__=''
# load dependencies
load() {
local var= value= file=
var="$1"; file="$2"
value=$( eval "printf '%s' \"\${${var}:-}\"" )
[ -n "${value}" ] && return 1;
if [ -f "${file}" ]; then
. "${file}"
else
printf "ERROR: Unable to load ${file}\n"
exit 2
fi
return 0;
}
# Load configuration file
load SCRIPT_HELPER_DIRECTORY /etc/ScriptHelper.conf
SCRIPT_HELPER_DIRECTORY="${SCRIPT_HELPER_DIRECTORY:-}"
SCRIPT_HELPER_DIRECTORY="${SCRIPT_HELPER_DIRECTORY%%/}"
load __LIB_MESSAGE__ "${SCRIPT_HELPER_DIRECTORY}/message.lib.sh"
load __LIB_RANDOM__ "${SCRIPT_HELPER_DIRECTORY}/random.lib.sh"
# ----------------------------------------------------------------------------
# usage: MAIL_CREATE [ <mail_filename> ]
# desc: creates a file to store the mail content.
# arguments: <mail_filename> : mail file to use, which MUST exist
# note: if no file is given, a random-named file is created in /tmp
MAIL_CREATE () {
local mail_file=
[ $# -eq 0 ] && mail_file="/tmp/mail.$(RANDOM)" || mail_file="$1"
[ -z "${mail_file}" ] && FATAL "Can't create a mail with empty filename"
touch "${mail_file}" 2>/dev/null
if [ $? -ne 0 -o ! -w "${mail_file}" ]; then
FATAL "MAIL_CREATE can't create temp mail file ${mail_file}"
return 1;
fi
__MAIL_FILE__="${mail_file}"
LOG "MAIL_CREATE: ${__MAIL_FILE__}"
return 0;
}
# ----------------------------------------------------------------------------
# usage: MAIL_GET_FILE
# desc: return the content of __MAIL_FILE__, ie the current mail file used
MAIL_GET_FILE () {
printf '%s' "${__MAIL_FILE__}";
return 0;
}
# usage: MAIL_SET_FILE <mail_filename>
# desc: set the current mail file to use, ie the content of __MAIL_FILE__
# arguments:
# <mail_filename> : mail file to use, which MUST exist
MAIL_SET_FILE () {
[ $# -ne 1 ] && FATAL "MAIL_SET_FILE: Bad argument(s)"
[ ! -w "$1" ] && FATAL "MAIL_SET_FILE: $1 is not writable"
__MAIL_FILE__="$1"
return 0;
}
# ----------------------------------------------------------------------------
# usage: MAIL_APPEND [ <mail_filename> ] <message>
# desc: adds <message> to the mail file
# arguments:
# <mail_filename> : mail file to use (optional)
# <message> : the message to add to the mail content.
# note: if <mail_filename> is not given, use __MAIL_FILE__ instead
MAIL_APPEND () {
local mail_file="${__MAIL_FILE__:-}"
[ $# -ne 1 -a $# -ne 2 ] && FATAL "MAIL_APPEND: Bad argument(s)"
if [ $# -eq 2 ]; then
mail_file="$1"; shift
fi
[ -z "${mail_file}" ] && FATAL "MAIL_APPEND: no mail file was setup"
[ -w "${mail_file}" ] || FATAL "MAIL_APPEND: can't write to mail file"
printf '%s\n' "$*" >> "${mail_file}"
LOG "MAIL_APPEND[${mail_file}]> $*"
return 0;
}
# ----------------------------------------------------------------------------
# usage: MAIL_PRINT [ <mail_filename> ]
# desc: prints the content of the mail to send
# arguments: <mail_filename> : mail file to use (optional)
# note: if <mail_filename> is not given, use __MAIL_FILE__ instead
MAIL_PRINT () {
local mail_file=
[ $# -eq 1 ] && mail_file="$1" || mail_file="${__MAIL_FILE__:-}"
[ -z "${mail_file}" ] && FATAL "MAIL_PRINT: no mail file was setup"
[ -r "${mail_file}" ] || FATAL "MAIL_PRINT: can't read mail file"
cat "${mail_file}"
return 0;
}
# ----------------------------------------------------------------------------
# usage: MAIL_SEND [ <mail_filename> ] <subject> <addresses>
# desc: use default system mail options to send the mail
# arguments:
# <mail_filename> : mail file to use (optional)
# <subject> : subject of the mail.
# <addresse> : A commat separated list of mail addresses.
# note: if <mail_filename> is not given, use __MAIL_FILE__ instead
# note: for more specific usages of the mail command use
# MAIL_PRINT | mail [options] ...
MAIL_SEND () {
local mail_file="${__MAIL_FILE__:-}"
[ $# -ne 2 -a $# -ne 3 ] && FATAL "MAIL_SEND: Bad argument(s)"
if [ $# -eq 3 ]; then
mail_file="$1"; shift
fi
[ -z "${mail_file}" ] && FATAL "MAIL_SEND: no mail file was setup"
if [ ! -s "${mail_file}" ]; then
NOTICE "No modification noticed. Prepared e-mail will not be sent."
LOG "MAIL_SEND[${mail_file}] Not sent, as file is empty"
rm -f "${mail_file}"
return 2;
fi
which mail >/dev/null
if [ $? -eq 0 ]; then
MAIL_PRINT | mail -s "$1" "$2"
LOG "MAIL_SEND[${mail_file}] Mail sent"
else
NOTICE "'mail' command can't be found. The mail wasn't sent. See '${mail_file}' file."
LOG "MAIL_SEND[${mail_file}] Not sent, as 'mail' command wasn't found."
fi
rm -f "${mail_file}"
return 0;
}
fi # end of: if [ "${__LIB_MAIL__:-}" != 'Loaded' ]; then