TIL: Mailing bot with Python

TIL: Mailing bot with Python

Bots are a really cool way to automate boring tasks. To finish this chore i wished to code a mailer bot and i finally succeeded using the Flask-mail library in python.

I started out by referring to the most commonly available codes for this task, however, the official documentation of the Flask-mail extension did not help me at all. It was just extra. I did refer multiple Youtube tutorials and stumbled upon codes that were perfect... well, almost.

Here's the thing though, SMTP authentication errors are not easy to rectify. What with the TSL AND SSL ports and their never ending expectations, it made me almost want to give up on this task.

OnPaste.20200515-165300.png This kept showing up on my window and it got frustrating to work with.

After spending hours staring at the screen, wondering what was wrong and searching for all possible error solutions online i stumbled upon a particular setting that had been from my mail host that had been turned off.

OnPaste.20200515-170118.png Such a small thing, and i wasted so much time figuring it out. The secret to my success lay not in my code but in the settings of my account! Even after changing the settings, my code, which i had picked up online, wasn't working as expected. I altered and made changes wherever i needed to and fixed it, pronto, and voila!

My mailer can send mails to as many people as you want, as many times as you'd like it to, (well, of course it's a spam bot ;) ) albeit it is being used as an invitation list tracker.

Here's the key part of the source code.

  • Importing the right libraries
    import smtplib, ssl
    
  • Setting the right port, according to the mail server you use
    port = 465
    
  • Use the updated version of functions, which are currently supported
    context = ssl.create_default_context()
    

Head over to my github repository for the whole code.

github.com/LordOfDexterity/Mailer

And that's what i learnt today, that these codes online, may or may not work, but if you code with a purpose, there's nothing that can stop you from doing your tasks satisfactorily.