How to send mail from the terminal using a script

For X or Y reasons, sometimes we need to program the server of our company to do a certain task, and we want to know if this task was executed without problems, for this we program that if everything went smoothly, notify us… but… ¿ How can the server notify us?

Very simply, we will tell it through a Python script to send us an email, this way when we check the mailbox, we will see if the server had no problems carrying out the order.

I've included the script here: send-email.py

To use it is very simple, open a terminal, type the following into it and press [Enter] :

cd $HOME/ && wget http://paste.desdelinux.net/paste/?dl=89 && mv index.html\?dl\=89 send-email.py && chmod +s send-email.py

I leave you how the process would be:

% CODE1%

Once this is done, you must edit the script to put your own data, I will edit it by putting my data, that is, the data from here my work.

Open the script (send-email.py) and change this data:

  • your email@desdelinux.net by your email (in this example - » kzkggaara@ipichcb.rimed.cu)
  • Message body by the content of the email (in this example - » This is a test email)
  • you.mailserver.cu by your mail server (in this example - » 192.168.1.2)
  • your email by your user (in this example - » kzkggaara)
  • your-password for your password (in this example - » HAHA… yeah… sure… LOL)

It now looks like this: send-email.py (Modified)

And voila, it only remains to send the email ... for this we will put the following:

  • python send-email.py "Here we put the subject" recipient@domain.com

In other words, we must execute the script and pass it the "Subject" parameter (that is, the subject that the email will take) and also to whom (email address) it will be sent.

I will send myself an email, here is the example:

% CODE2%

And now 

As you can see, it's quite simple... but it can help us on certain occasions 

regards


Add as preferred source in Google