[OpenWrt How-To] send mail with attachment with mutt and msmtp (gmail)

opkg update
opkg install msmtp mutt

nano /etc/msmtprc

account default

host smtp.gmail.com
port 587
auth on
user user@gmail.com
password gmail-password

auto_from off
from user@gmail.com

tls on
tls_starttls on
tls_certcheck off

logfile
syslog LOG_MAIL

nano ~/.muttrc

set sendmail="/usr/bin/msmtp"
set use_from=yes
set realname="OpenWrt"
set from=user@gmail.com
set envelope_from=yes

note: change user@gmail.com and gmail-password with your gmail credentials

  • command to send mail with attachment:

echo "Hello this is the body message, we are sending email with attachement using mutt and msmtp" | mutt -a "/root/file-to-attach" -s "this is the subject of the message" -- recipient@domain.com

  • message without attachment:

echo "Hello this is the body message, we are sending email using mutt and msmtp" | mutt -s "this is the subject of the message" -- recipient@domain.com

4 Likes

Users should be aware that saving "sensitive" credentials on a router or other exposed device is poor security practice. Encryption cannot be used to reduce the risk in any case where the use of the credentials is automated, without user intervention to "unlock" the credentials for that specific use. With user input needed, the user might as well enter the password directly, rather than "a password to access the password".

everything is esposed today, it's just a matter to adopt some practices to reduce the risks, like using iptables rules to allow only authorized ip to login and using non standard ports for services like ssh and luci (uhttp), also it is important to disable all services not in use.
I'm sure it is more secure an OpenWrt router than any Windows desktop/laptop PC or Android smartphone :grinning:

Maybe using an application password would be a better option, taking in consideration that these command line mailers don't support 2FA.

You can also use a second mail account that is only used for that purpose...

2 Likes

Send multiple files with mutt (it does not work if OpenWrt is older than 18.06):

echo "This is the body of the message" | mutt $( printf -- '-a \%q ' /root/*.csv ) -s "Subject of the message" -- receiver@provider.com

older version of printf does not support "%q"

use instead /usr/bin/mutt -a 'ls /root/*.csv'

1 Like

Thank you,work
image

Did have some good book or reference practice for security management or programming in OpenWrt, I have many devices to manage, but newer to openwrt programming:)