Hi, I have a newbie question about running Ubuntu 16.04 LTS on a VPS. What is the simplest/most effective way for sending e-mail from the command line, The reason I ask is because I want my VPS to be able to install updates automatically and e-mail me what was updated. Thanks for your advice.
I made a Python script a while back to send notifications to my gmail account when things happen on my servers.
I mostly just pipe commands to it like this:
sudo apt-get dist-upgrade | ./sendmail.py --subject Updates --mime plain
Which sends me this:
Reading package lists... Building dependency tree... Reading state information... Calculating upgrade... 0 to upgrade, 0 to newly install, 0 to remove and 0 not to upgrade.
Or run something like this on a cronjob:
cat /var/log/dpkg.log | grep $(date +"%Y-%m-%d") | grep "\ install\ " | ./sendmail.py -s Updates
This will send a list of all the packages that have been installed on the current day and will output something like this:
2016-09-24 02:18:37 install php7.0-sqlite3:amd64 <none> 7.0.8-0ubuntu0.16.04.2 2016-09-24 02:19:28 install libdbi1:amd64 <none> 0.9.0-4 2016-09-24 02:19:28 install libdbd-sqlite3:amd64 <none> 0.9.0-3ubuntu2 2016-09-24 02:19:56 install libsqlite3-dev:amd64 <none> 3.11.0-1ubuntu1 2016-09-24 02:23:31 install cmake-qt-gui:amd64 <none> 3.5.1-1ubuntu2 2016-09-24 02:25:03 install libqt5quickparticles5:amd64 <none> 5.5.1-2ubuntu6 2016-09-24 02:25:03 install qtdeclarative5-dev:amd64 <none> 5.5.1-2ubuntu6 2016-09-24 02:25:03 install libqt5webkit5-dev:amd64 <none> 5.5.1+dfsg-2ubuntu1 2016-09-24 02:26:05 install qt5keychain-dev:amd64 <none> 0.7.0-9.1
So to set this up in a cronjob first run
crontab -e and add the following to the file:
30 23 * * * cat /var/log/dpkg.log | grep $(date +"%Y-%m-%d") | grep "\ install\ " | /path/to/script/sendmail.py -s Updates
This will send the message every day at 11:30PM and if packages were install any time before 11:30PM on the same day they will show up in the message.
The script is below:
You'll have to change some values for this to work with your email account such as the SMTP server, sender address, email password, and destination. I set it up with
smtp.google.com and used my gmail address for both the sender and destination addresses so I'm effectively e-emailing myself the message.
#!/usr/bin/env python3 from smtplib import SMTP_SSL as SMTP from email.mime.text import MIMEText from optparse import OptionParser import sys class Mail(object): SMTPserver = 'smtp.example.com' sender, destination, password = '[email protected]', '[email protected]', 'password' opts = None def __init__(self): parser = OptionParser() parser.add_option("-s", "--subject", dest="subject", default="Message") parser.add_option("-m", "--mime", dest="mime", default="plain") (self.opts, args) = parser.parse_args() def get_message(self): input_stream = sys.stdin msg = MIMEText(input_stream.read(), self.opts.mime) msg['Subject'] = self.opts.subject msg['From'] = self.sender msg['To'] = self.destination return msg def send(self, msg): try: with SMTP(self.SMTPserver) as smtp: smtp.login(self.sender, self.password) smtp.sendmail(self.sender, self.destination, msg.as_string()) print("done") except(Exception) as e: print(e.__class__.__name__, e) sys.exit(0) m = Mail() m.send(m.get_message())
I also uploaded it to gist here: https://gist.github.com/michaellindman/c1bc3a2b8e5f12a0055c65e55b230b64
Thanks for the script, I'll give it a try.
If you have any problems let me know and I'll see if I can help. The script was something I quickly throw together because I had a need for it and thus isn't very robust but it should be fairly easy to understand.
It works with my gmail account. Thanks again.
Well I'm using your request words "easiest".
Mail -s "Subject You want in Email" [email protected] < /location/of/file/with/message
If that does not work you don't have basic email packages installed and they should be if its a default install.
It may be the easiest but it has many issues. Most email services would flag messages sent like this as spam and it doesn't encrypt the message and given the potentially sensitive nature of whats being sent encryption is essential.
No where does the OP list security, or did he send you a PM?
I have hundreds of systems sending me such emails and I send everyone I work with such emails and unless I send bogus links or huge attachments none have gone to spam, my vendors also keeps sending me product so I think they're getting to them.
Mail is the classic Unix mail, if it doesn't work you have major issues with your system.
The OP doesn't mention security but it is an important factor especially if you are sending yourself system logs.
Also, on an unconfigured system messages would be sent by
[email protected] which isn't a domain and cannot be verified by DNS meaning most mail services would classify this message as spam because of that reason.