Python – Send e-mails with Python

Sending e-mail through the internet is quite a trivial trick in many programming languages. Some time ago, I have shown how to send MS Excel range as an e-mail and how to send a mail through VBA, with HTML.python-logo-master-v3-TM

As far as now I am interested in Python, a few days ago I have developed a simple banking software system, as a respond of this task. One of the features of the bank system, was to send a password with a hashed code, if the original password was forgotten. Thus, I have registered a new account in Yahoo.Mail and I have started to spam myself in a way to learn how to do it. 🙂

After some research I found three things:

  1. There is a library called smtplib, which helps you a lot in the idea!
  2. If I am going to publish this in the internet, it is a great idea to import the username and the password from another folder. Thus, in my case, these are imported from settings.
  3. The mails are received about 2 to 3 minutes after they are sent, but this was time I was willing to wait.

At the end, I have received managed to receive something like this:

export

Well, that was my target, so I managed it! 🙂

At the end, as usual, here comes the self-explanatory code:

A few notes on the code:

  1. You may decide to set “debuglevel” to “False”, in order to miss the 20 lines of interaction with the yahoo servers.
  2. In the current code, you are sending an e-mail to yourself. In my case jobanana500 is sending to jobanana500. I did this, with the idea not to spam someone by mistake. It is rather easy to find a way how to send a mail to someone else.
  3. Last but not least, do not forget that spamming is a crime!

That’s all folks! 🙂

Tagged with: , , , ,