Data Science Some Days: Let’s write an email

Ladies and gentlemen, this has been a long time coming.

So, I’ve finally started programming. Technically, I started months ago but I’ve made such a piss-poor effort at being consistent, that I’ve done next to nothing.

I was growing frustrated – often having nightmares asking the question – will I ever be able to say “Hello World”?

Evidently, simply thinking about it would never work. I can buy as many Udemy courses as I want – that won’t turn me into a data scientist. My wonderful solution to this is to go straight into a project and learn as I go. I’m learning python 3.6. But first…

print("Hello World")

I have joined the elites.


Ok, the project goes as follows:

I need to send emails to specific groups of people a week before the event starts. The email should also include attachments specific to the person I’m sending the email to and it will have HTML elements to it.

To break it down…

  • I need to send an email
  • I need to send a HTML email
  • I need to send a HTML email with attachments
  • I need to send a HTML email with attachments to certain people

There’s more but I’ve only managed the first two so far. This programming stuff is difficult and the only reason why I’m not computer illiterate is because I was born in the 90s.

I started off by opening these tutorials:

They’re both great and use slightly different methods to achieve the same result. Maybe you’ll notice that my final solution ends up being a desperate cry for help combination of them both.

LET US BEGIN.


Here is the first iteration of the code:

import smtplib
#sets up simple mail transer protocol

smtpObj = smtplib.SMTP('smtp-mail.outlook.com', 587)
type(smtpObj)
#Connects to the outlok SMTP server

smtpObj.ehlo()
#Says "hello" to the server

smtpObj.starttls()
#Puts SMTP connection in TLS mode.
#I didn't get any confirmation when I ran the program though...

smtpObj.login('email1@email.com', input("Please enter password: ")
#Calls an argument to log into the server and input password.

smtpObj.sendmail('email1@email', 'email2@gmail.com', 'Subject: Hello mate \nLet\'s hope you get this mail')
#Email it's coming from, email it's going to, the message

smtpObj.quit()
#ends the session

print('Session ended')
#Tells me in the terminal it's now complete


Boom, pretty simple right? I mainly took everything from Automate the Boring stuff and just swapped in my details. Well, of course not. I kept on getting an error – nothing was happening.

Well – I was somehow using the WRONG EMAIL. FUCK. It took me an hour to realise that.


Next step… let’s send an email with bold and italics.

This was frustrating because nothing worked. All it really requires is for you to put in the message in HTML format. Because I’m not learning HTML, I decided to just use this nifty HTML converter to make this part less painful.

Here are my errors…

#regularly get "Syntax error" with smtpObj.sendmail - I was missing a fucking bracket

This literally made me to go bed angry.


'''everything in the HTML goes into the subject line -
smtpObj.sendmail('email@email', 'email2@gmail.com', "Subject: " f"Hello mate \n {html}")'''

This was a surprise but I figured out to stop it…

'''Now nothing shows up in the body of the message:
smtpObj.sendmail('zctylak@ucl.ac.uk', 'oj.akhigbe@gmail.com', f"Subject: \n Hello mate {html}")
Solved by putting {html} next to \n'''

…then it somehow got worse…

'''Now... the email doesn't actually show up in html format
smtpObj.sendmail('zctylak@ucl.ac.uk', 'oj.akhigbe@gmail.com', f"Subject: Hello mate\n{html}")'''

…and even worse.


At this point, I changed tactic and, in the process, the entirety of my code. I won’t show you everything here otherwise this post will look too technical to those who have no experience with coding but I’ve uploaded my progress so far onto Github.

To summarise, rather than trying to send HTML with the technique I used earlier, I used a module that was essentially created to make sending emails with python much easier (email.mime). This essentially means, it contains prewritten code that you can then use to create other programs.

But… I was successful in sending myself a HTML email. Now the next step is to add a bloody attachment without putting my head through my computer.


Dear reader, you may be wondering, “why is he putting himself through so much suffering? He sounds incredibly angry.

I’m not, I promise. This has actually been the most fun I’ve had in my free time in a while. It was a good challenge and I could sense myself improving after every mistake.

Granted, I probably should have just completed an online course or something before trying to jump into this project but that would have been less fun.

Onto the next one…

Thanks for reading!

Don’t depend on technology for better discipline

The internet and all of its accessories have the potential to be extremely helpful. It provides us with relaxation, helpful resources and knowledge. On the other side, it can be harmful and prevent us from doing the things we want to do. We easily push our work to the side and procrastinate into the early hours of the morning then curse under our breath when we notice that we haven’t completed anything useful.

You may have resorted to helpful programs that will help you do more work. Some applications block our most visited sites if we just waste time on them. Others help us control the amount of time we spend on different tasks or help us slowly build habits by telling us how long we’ve been doing certain things.

I did (and still do) use apps to aid my discipline. However, they can easily be used as a crutch rather than a tool.

When we go to applications as solutions for our problems, we’re making an important mistake. Self discipline should be solved by the person. Not an external application found on the chrome store.

I find this approach problematic because you’ll find extremely easy ways to circumvent them. For example, ‘StayFocusd’ can be avoided if you do your browsing incognito mode or by using a different browser. There are stronger blocks out there but the point still stands.   Maybe you’ll feel guilty after finding a way around them. Maybe not. Either way, you probably won’t do what you need to be doing.

When you’re looking for apps to help you with productivity, keep in mind that they’re just applications. Not all knowing problem solvers. Not your key to becoming super disciplined. Not the only things that can make you work. Just applications. You shouldn’t have multiple apps that do the same thing. Or apps that contain too many features which will simply prove to distract you even more. Do you really need a note taking app that stores pictures, news snippets you’ll never read, gives you compliments and tells you the weather a year in advance?

If you find yourself downloading endless productivity apps or researching far too much about the perfect app that’ll help you become disciplined, you probably need to assess your problem a bit further.

Applications are helpful but they should be used as tools. Not crutches. Remember, you can always unplug yourself from the internet completely.

Do you use productivity applications? Do you find them essential or just convenient?