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
|
From 49d6d0809e7f81107623d1258bc59a63c77a2f80 Mon Sep 17 00:00:00 2001
From: atom long <atom.long@hotmail.com>
Date: Tue, 7 Mar 2023 23:33:41 +0800
Subject: [PATCH 2/2] send mail via curl command. MTA is not needed anymore.
---
CHANGES.md | 1 +
README.md | 3 +-
freenom.conf | 14 +++++----
freenom.sh | 80 ++++++++++++++++++++++++++++++++++++++++++++--------
4 files changed, 80 insertions(+), 18 deletions(-)
diff --git a/CHANGES.md b/CHANGES.md
index bfae997..4ae2699 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -14,6 +14,7 @@ _order: latest/newest on top_
- `APPRISE="/usr/local/bin/apprise"`
- `APPRISE_SERVER_URLS=( )`
- [**20211207**] fixed issue [#52](https://github.com/mkorthof/freenom-script/issues/52): sed removes CR using \r instead of ^M
+- [**20210914**] send mail via curl command.
- [**20210821**] fixed issue [#50](https://github.com/mkorthof/freenom-script/issues/50)
- [**20210618**] added more config checks (PR [#43](https://github.com/mkorthof/freenom-script/issues/43) from nathanfranke)
- [**20210612**] check for uaString and ipCmd settings ([#42](https://github.com/mkorthof/freenom-script/issues/42))
diff --git a/README.md b/README.md
index cc3da5e..a9157f7 100644
--- a/README.md
+++ b/README.md
@@ -57,7 +57,8 @@ Examples ./freenom.sh -r example.com
Using a full Linux distro including coreutils is recommended (e.g. Debian). Embedded and BusyBox based systems are untested and will probably not work correctly or at all.
-_Note that this shell script requires recent versions of "Bash" and "cURL"_
+_Note that this shell script requires recent versions of "Bash" and "cURL";
+If you want to use your own domain mailbox to send notifications, you also need the "Ping" command._
### Auto Installer
diff --git a/freenom.conf b/freenom.conf
index 29806ac..ac7b60f 100644
--- a/freenom.conf
+++ b/freenom.conf
@@ -102,15 +102,19 @@ ipCmd+=("dig -4 +short whoami.akamai.net @ns1-1.akamaitech.net")
# Mail Notifications #
######################
-# Mail Transfer Agent. A lightweight send-only MTA such as SSMTP should work fine. Or leave empty to disable sending mail.
-# If you do not have a MTA installed you can use "bashmail.sh": https://git.io/JJdto. Default is "/usr/sbin/sendmail"
-MTA="/usr/sbin/sendmail"
+# Mail account to send notifications
+# Note: If gmail is used, check https://myaccount.google.com/u/1/lesssecureapps?pageId=none
+# to see if you allow for less secure email clients
+#MAILUSERNAME="youraccount@gmail.com"
+
+# Mail server password
+#MAILPASSWD="password"
# E-mail adress to receive notifications e.g. "admin@example.com". Default is "$freenom_email"
-#RCPTTO="admin@example.com"
+#RCPTTO="$freenom_email"
# Optional e-mail address from which to sent notifications ("From:"). Default is none
-#MAILFROM="Freenom Script <freenom-script@example.com>"
+#MAILFROM="Freenom Script <${MAILUSERNAME}>"
#########################
# Apprise Notifications #
diff --git a/freenom.sh b/freenom.sh
index c30f28a..8a114a3 100755
--- a/freenom.sh
+++ b/freenom.sh
@@ -169,14 +169,6 @@ if [ -z "$RCPTTO" ]; then
RCPTTO="$freenom_email"
fi
-if [ ! -x "$MTA" ]; then
- if [ -x "/usr/sbin/sendmail" ]; then
- MTA="/usr/sbin/sendmail"
- else
- MTA=""
- echo "Warning: No MTA found, cant send email"
- fi
-fi
# set a few general variables
@@ -483,22 +475,86 @@ func_sleep() {
fi
}
+# Function sleep: send mail
+func_mailto()
+{
+local MAILHOST MAILPORT
+local ssl_opt smtp_proto
+
+case ${MAILUSERNAME} in
+ *@gmail.com)
+ MAILHOST="smtp.gmail.com"
+ MAILPORT=587
+ ;;
+ *@qq.com)
+ MAILHOST="smtp.qq.com"
+ MAILPORT=587
+ ;;
+ @163.com)
+ MAILHOST="smtp.163.com"
+ MAILPORT=465
+ ;;
+ *@vip.163.com)
+ MAILHOST="smtp.vip.163.com"
+ MAILPORT=465
+ ;;
+ *@outlook.com)
+ MAILHOST="smtp.office365.com"
+ MAILPORT=587
+ ;;
+ *)
+ MAILHOST=smtp.${MAILUSERNAME#*@}
+ MAILHOST=$(ping -c 1 ${MAILHOST} | grep -Po '^PING\s*\K\S+')
+ [ -z "${MAILPORT}" ] && curl smtp://${MAILHOST}:587 --ssl-reqd -X "QUIT" &>/dev/null && MAILPORT=587
+ [ -z "${MAILPORT}" ] && curl smtps://${MAILHOST}:465 --ssl-reqd -X "QUIT" &>/dev/null && MAILPORT=465
+ [ -z "${MAILPORT}" ] && curl smtp://${MAILHOST}:25 -X "QUIT" &>/dev/null && MAILPORT=25
+ [ -z "${MAILPORT}" ] && {
+ echo "Error: Unsupport MAILHOST: ${MAILHOST}"
+ return 1
+ }
+ ;;
+esac
+
+case ${MAILPORT} in
+ 25) ssl_opt=""
+ smtp_proto="smtp"
+ ;;
+ 465) ssl_opt="--ssl-reqd"
+ smtp_proto="smtps"
+ ;;
+ 587) ssl_opt="--ssl-reqd"
+ smtp_proto="smtp"
+ ;;
+ *)
+ echo "Error: Unknown MAILPORT: ${MAILPORT}."
+ return 1
+ ;;
+esac
+
+curl --silent ${ssl_opt} --login-options AUTH=LOGIN \
+ --url "${smtp_proto}://${MAILHOST}:${MAILPORT}" \
+ --mail-from "${MAILUSERNAME}" \
+ --mail-rcpt "${1}" \
+ --user "${MAILUSERNAME}:${MAILPASSWD}" \
+ --upload-file -
+}
+
# Function mailEvent: send mail
# parameters: $1: $event 2: $messages
mailEvent() {
- if [ "$MTA" != "" ] && [ "$RCPTTO" != "" ]; then
+ if [ "$MAILUSERNAME" != "" ] && [ "$MAILPASSWD" != "" ]; then
if [ "$debug" -ge 1 ]; then
- echo "DEBUG: $(date '+%H:%M:%S') email $pad4 HOSTNAME=$HOSTNAME MTA=$MTA RCPTTO=$RCPTTO 1=$1 2=$2"
+ echo "DEBUG: $(date '+%H:%M:%S') email $pad4 HOSTNAME=$HOSTNAME RCPTTO=$RCPTTO 1=$1 2=$2"
fi
[ "$debug" -ge 3 ] && set -x
HEADER="To: <$RCPTTO>\n"
if [ "$MAILFROM" ]; then
HEADER+="From: $MAILFROM\n"
fi
- echo -e "${HEADER}Subject: freenom.sh: \"$1\" on \"$HOSTNAME\"\n\nDate: $(date +%F\ %T)\n\n$2" | "$MTA" "$RCPTTO"
+ echo -e "${HEADER}Subject: freenom.sh: \"$1\" on \"$HOSTNAME\"\n\nDate: $( date +%F\ %T )\n\n$2" | func_mailto "$RCPTTO"
EXITCODE="$?"
if [ "$EXITCODE" -ne 0 ]; then
- echo "Error: exit code \"$EXITCODE\" while running $MTA"
+ echo "Error: exit code \"$EXITCODE\" while sending mail."
fi
[ "$debug" -ge 3 ] && set +x
fi
--
2.39.2
|