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 simple, we will tell you through a script Python to send us an email, in this way when we check the mailbox, we will read if the server had no problems to place the order.

The script left them here: send-email.py

To use it is very simple, open a terminal, in it write the following 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 looks like the following: 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, something quite simple ... but it can help us on certain occasions 😉

regards


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   jondarlek said

    it's very good .. but i want something like this that works for windows but i can't find it 🙁

    1.    KZKG ^ Gaara said

      For Windows ... mmm ... you must install Python on Windows friend: http://www.python.org/getit/windows/

    2.    john said

      for windows there is a tool called Cobian, it is very good and intuitive

  2.   Carlos T. said

    how to enable the script to send with gmail (smtp)?

    1.    elwuilmer said

      I have the same question: /

      I could:

      s = SMTP ('smtp.gmail.com')
      s.starttls () # If you use TLS
      s.ehlo ()

      However it gives me this error:

      python send-email.py "Here we put the subject" aquimi@correo.com
      Traceback (most recent call last):
      File "send-email.py", line 14, in
      s = SMTP ('smtp.gmail.com')
      File "/usr/lib/python2.6/smtplib.py", line 239, in __init__
      (code, msg) = self.connect (host, port)
      File "/usr/lib/python2.6/smtplib.py", line 295, in connect
      self.sock = self._get_socket (host, port, self.timeout)
      File "/usr/lib/python2.6/smtplib.py", line 273, in _get_socket
      return socket.create_connection ((port, host), timeout)
      File "/usr/lib/python2.6/socket.py", line 561, in create_connection
      raise error, msg
      socket.error: [Errno 101] The network is unreachable

  3.   reedyseth said

    It's good, and then you program it with cron and ready to report: D.

    1.    KZKG ^ Gaara said

      Yes! hehe, save a lot of time automate things with scipts 🙂

  4.   luweeds said

    Thanks for the post, I find it interesting and adding the functionality that Reedyseth cites has one more utility. 😉 greetings compas!

    1.    KZKG ^ Gaara said

      Thanks to you for the comment 😀
      Greetings friend.

  5.   Neo61 said

    Gaara, my friend, I'm sure this article is very good as everyone here highlights but I - NO-PUE-DO-SE-LOS-E-JEM-PLOS-POR-MI-MAL-DI-TA-CO-NEC- TION
    WHAT DO I DO TO SEE THEM, CUT MY THROAT?

    1.    KZKG ^ Gaara said

      Can't open this link? - » http://paste.desdelinux.net/89

  6.   Neo61 said

    Let me ask you something, could I make a script so that on a certain day I can send a specific message to all the users of my center? I guess so, but you are the specialist and if you confirm this, could you help me make one?

    1.    KZKG ^ Gaara said

      Yes, to do "something" on a given day you must use crontab.
      Then, assuming you want to send a .PDF file (for example, note.pdf) to your users, to send to only 1 email would be:
      mail -s "Este es el asunto del correo" direccionemail@loquesea.cu < nota.pdf

      Now, to send that but not only to one address, but to many more ... you would need to have the addresses in a text file (separated by a line break), and then use a loop for. For example, if you have the emails in addresses.txt, it would be:

      for i in `cat direcciones.txt`;
      do
      mail -s "Este es el asunto del correo" $i < nota.pdf
      done

  7.   David candle said

    Thanks buddy, the script works great.

  8.   Frank said

    Where is the script if you can help me

  9.   areli said

    Can you please share the examples ????