What's new

SendMail not working

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

Serpien

New Around Here
I have an openvpn-event script running which should send an email message. I used logging commands in the script to validate that indeed the script is being called and is running to completion.

HOWEVER - and this is weird - the sendmail portion of the code only works if the script is called directly from the shell. In that case, I can see all the terminal output from sendmail and the email is sent. When run by the router as part of the openvpn-event, no email is sent. However, all logging messages are present in the router logs, right down to the last one.

Maybe a permissions issue with calling openssl and./or sendmail?

Any thoughts most appreciated 🙏

Bash:
#!/bin/sh

echo "Starting script openvpn-event" | logger -t "openvpn-event"

FROM="myacct@gmail.com"
AUTH="myacct@gmail.com"
PASS="myapppassword"
FROMNAME="Asus Router"
TO="myacct@hotmail.com"

echo "script_type=$script_type"
echo "script_type=$script_type" | logger -t "openvpn-event"

echo "Calling ntpclient" | logger -t "openvpn-event"
ntpclient -h pool.ntp.org -s &> /dev/null
sleep 5

echo "Writing temp email file" | logger -t "openvpn-event"
echo "Subject: Router OpenVPN Event" >/tmp/mail.txt
echo "From: \"$FROMNAME\"<$FROM>" >>/tmp/mail.txt
echo "Date: $(date -R)" >>/tmp/mail.txt
echo "" >>/tmp/mail.txt
echo "Event: $script_type" >>/tmp/mail.txt
echo "" >>/tmp/mail.txt

echo "Calling sendmail" | logger -t "openvpn-event"
cat /tmp/mail.txt | /usr/sbin/sendmail  \
    -H "exec openssl s_client -quiet \
    -starttls smtp \
    -connect smtp.gmail.com:587  \
    -no_ssl3 -no_tls1" \
    -f "$FROM" -au"$AUTH" -ap"$PASS" "$TO" -v

echo "Deleting temp email file" | logger -t "openvpn-event"
rm /tmp/mail.txt
echo "Completed script openvpn-event" | logger -t "openvpn-event"
 
tons of posts here how people leverage amtm with curl for sending mail.
 
I did configure amtm, and it's very cool. I was able to send a test email from the options menu.

HOWEVER, I'm still seeing the exact same behavior - the email gets sent when the script is run directly from the shell, but not when called by the router.

I modified my script according to another thread I found on here:

Bash:
#!/bin/sh

echo "Starting script openvpn-event" | logger -t "openvpn-event"
echo "script_type=$script_type" | logger -t "openvpn-event"

echo "Writing temp email file" | logger -t "openvpn-event"
echo "Subject: Router OpenVPN Event" >/tmp/mail.txt
echo "From: \"Router\"<$FROM_ADDRESS>" >>/tmp/mail.txt
echo "Date: $(date -R)" >>/tmp/mail.txt
echo "" >>/tmp/mail.txt
echo "Event: $script_type" >>/tmp/mail.txt
echo "" >>/tmp/mail.txt

echo "Sending mail via amtm mail" | logger -t "openvpn-event"
. /jffs/addons/amtm/mail/email.conf
EMAIL_DIR=/jffs/addons/amtm/mail
/usr/sbin/curl $verbose --url $PROTOCOL://$SMTP:$PORT \
--mail-from "$FROM_ADDRESS" --mail-rcpt "$TO_ADDRESS" \
--upload-file /tmp/mail.txt \
--ssl-reqd \
--user "$USERNAME:$(/usr/sbin/openssl aes-256-cbc $emailPwEnc -d -in "${EMAIL_DIR}/emailpw.enc" -pass pass:ditbabot,isoi)" $SSL_FLAG

echo "Deleting temp email file" | logger -t "openvpn-event"
rm /tmp/mail.txt
echo "Completed script openvpn-event" | logger -t "openvpn-event"
 
I am monitoring this thread. I had a similar issue and couldn't get mail to work in a background script either. Ended up solving it by using the screen app I installed from Entware.
 
I am monitoring this thread. I had a similar issue and couldn't get mail to work in a background script either. Ended up solving it by using the screen app I installed from Entware.
Maybe you could tell me a bit more about how you did it?
 
Maybe you could tell me a bit more about how you did it?
Once I installed screen, the following command will fork and run the script in the background which won't get killed off:

Bash:
screen -S iphere -dm sh -c "/mnt/opt/home/iphered.sh $IPNAME $IPTARGET > /mnt/opt/home/iphere.log"

You can, of course, change the script name, args, and log location. Again, there could be other ways to do this, but it worked for me.
 
I believe this is part of the script security functionality within openvpn, years ago myself I simply used the openvpn script call to call my main script in the background

Code:
#!/bin/sh
sh /jffs/scripts/vpnconnectlog.sh &
 
This makes sense - will give this a try first before going the screen route, which (it appears) does essentially the same thing by running it on a separate thread.
 

Similar threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top