I have postfix+dovecot. I want to make bash script which can use SMTP for this. I don't want use sendmail.
Is it possible? May be someone has some examples of code?
I have postfix+dovecot. I want to make bash script which can use SMTP for this. I don't want use sendmail.
Is it possible? May be someone has some examples of code?
Boy, when that gauntlet is thrown, it always bash
es me right upside the head! :-)
#!/bin/sh
function checkStatus {
expect=250
if [ $# -eq 3 ] ; then
expect="${3}"
fi
if [ $1 -ne $expect ] ; then
echo "Error: ${2}"
exit
fi
}
MyHost=`hostname`
read -p "Enter your mail host: " MailHost
MailPort=25
read -p "From: " FromAddr
read -p "To: " ToAddr
read -p "Subject: " Subject
read -p "Message: " Message
exec 3<>/dev/tcp/${MailHost}/${MailPort}
read -u 3 sts line
checkStatus "${sts}" "${line}" 220
echo "HELO ${MyHost}" >&3
read -u 3 sts line
checkStatus "$sts" "$line"
echo "MAIL FROM: ${FromAddr}" >&3
read -u 3 sts line
checkStatus "$sts" "$line"
echo "RCPT TO: ${ToAddr}" >&3
read -u 3 sts line
checkStatus "$sts" "$line"
echo "DATA" >&3
read -u 3 sts line
checkStatus "$sts" "$line" 354
echo "Subject: ${Subject}" >&3
echo "${Message}" >&3
echo "." >&3
read -u 3 sts line
checkStatus "$sts" "$line"
Have just found this tiny but wonderful utility sendemail
(not sendmail
). The syntax is too simple to explain.
Example:
SERVER="smtp.company.com"
FROM="[email protected]"
TO="[email protected]"
SUBJ="Some subject"
MESSAGE="Some message"
CHARSET="utf-8"
sendemail -f $FROM -t $TO -u $SUBJ -s $SERVER -m $MESSAGE -v -o message-charset=$CHARSET
More info available through help or at the author's site: https://github.com/mogaal/sendemail.
Tested with gmail and it currently works.
#!/bin/bash
# Use "host -t mx yourispdomain" to find out yourispmailserver
exec 1<>/dev/tcp/yourispmailserver/25
a=$(cat <<"MAILEND"
HELO local.domain.name
MAIL FROM: <[email protected]>
RCPT TO: <[email protected]>
DATA
From: [email protected]
To: [email protected]
Subject: test
send your orders for pizza to the administrator.
.
QUIT
.
MAILEND
)
IFS='
'
declare -a b=($a)
for x in "${b[@]}"
do
echo $x
sleep 1
done
Install sSMTP, for instance:
apt-get install ssmtp
Configure ssmtp:
sudo nano /etc/ssmtp/ssmtp.conf
· Server: mailhub=smtp.1und1.de:587
· Hostname: hostname=subdomain.domain.com
· User: [email protected]
· Pass: AuthPass=your_password
Then in your sh file, do what you need and pipe it to mail, for instance:
#!/bin/bash
du -sh | mail -s "Disk usage report" [email protected]
OR
#!/bin/bash
echo "Today's DB backup is ok." | mail -s "DB daily backup alert" [email protected]
sSMTP[19315]: 554 Message rejected: Email address is not verified. The following identities failed the check in region US-EAST-1
–
Semifinalist It's not clear to me when you say that you don't want to use sendmail. May be you don't want to use the sendmail process.
Postfix has an executable called "sendmail", and may be you could want to use it because I cannot think why you should not.
#/bin/bash
FROM='[email protected]'
TO='[email protected]'
SUBJECT='This is a test message'
BODY="This is a test mail message body.
Hi there.
"
printf "From: <%s>\nTo: <%s>\nSubject: %s\n\n%s" "$FROM" "$TO" "$SUBJECT" "$BODY" | sendmail -f "$FROM"
You could use SSMTP. Maybe this one helps too:
http://tecadmin.net/send-email-smtp-server-linux-command-line-ssmtp/
Also wanted to throw in another solution here, useful if you don't have access to sendmail etc. Most linux servers will have telnet, so we can use that.
function send_email{
echo "EHLO my_mail_server.example.com" # replace this with your mail server domain
sleep 1
echo "MAIL FROM: [email protected]
sleep 1
echo "RCPT TO: [email protected]
sleep 1
echo "DATA
sleep 1
echo "Subject: My Subject Line"
sleep 1
echo "To: [email protected]"
sleep 1
echo "From: [email protected]"
sleep 1
echo "This is the body of my email. Put whatever in here."
sleep 1
echo "." # signifies the end of data entry, mail will be queued to send.
}
send_email | telnet 1.2.3.4 25 # call the function and pass it into telnet command.
You want bash
to talk directly to an SMTP server? That's not really going to happen. It might technically be possible using the support for network communication available in bash, but realistically you don't want to go down that path.
That means that what you really need is to call an external program that will take of SMTP for you. Typically, that's going to be sendmail
, but if you're trying to avoid that there are lots of other alternatives, including:
Both of these can handle communication with a remote SMTP server without involving sendmail.
I love dldnh's answer!
Perfect solution for sending administrative alerts via your dedicated smtp relay (I know you've got one). No sense putting smtp/Postfix/etc on more than one server, right?
I do have one suggestion: Send one blank line before your message text to let the relay know that no more headers are coming. Without the blank line, the text of your message may not be sent; for example if there is a colon in that first line of the message.
Here is a small rewrite I took liberties with:
#!/bin/bash
MyHost=$(hostname)
MailHost="mail"
MailPort=25
function checkStatus {
read -u 3 sts line
expect=250
if [ $# -eq 1 ] ; then
expect="${1}"
fi
if [ $sts -ne $expect ] ; then
echo "Error: ${line}"
exit
fi
}
FromAddr="$(whoami)@${MyHost}"
ToAddr="[email protected]"
Subject="Status Alert"
Message='Msg: hello bozo!!'
# Brilliant!!
exec 3<>/dev/tcp/${MailHost}/${MailPort} ; checkStatus 220
echo "HELO ${MyHost}" >&3 ; checkStatus
echo "MAIL FROM: ${FromAddr}" >&3 ; checkStatus
echo "RCPT TO: ${ToAddr}" >&3 ; checkStatus
echo "DATA" >&3 ; checkStatus 354
echo "Subject: ${Subject}" >&3
# Insert one blank line here to let the relay know you are done sending headers
# Otherwise a colon ":" in the message text will result in no text being sent.
echo "" >&3
# Send the message text and close
echo "${Message}" >&3
echo "." >&3 ; checkStatus
© 2022 - 2024 — McMap. All rights reserved.
sendmail
wrapper? – Goles