Post

100 Days Of Python - Day 32

Day 32

Sending Emails with Python

We can use the smtplib module to send emails with Python.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# main.py
import smtplib

my_email = "your email here"
password = "your password here"

with smtplib.SMTP("smtp.gmail.com") as connection:
    connection.starttls()
    connection.login(user=my_email, password=password)
    connection.sendmail(
        from_addr=my_email,
        to_addrs="recipient email here",
        msg="Subject:Hello\n\nThis is the body of my email."
    )

Monday Motivation Project

We can use the smtplib module to send emails with Python and HTML.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# main.py
import smtplib
import datetime as dt
import random

my_email = "your email here"
password = "your password here"

now = dt.datetime.now()
weekday = now.weekday()

if weekday == 1:
    with open("quotes.txt") as quote_file:
        all_quotes = quote_file.readlines()
        quote = random.choice(all_quotes)

    with smtplib.SMTP("smtp.gmail.com") as connection:
        connection.starttls()
        connection.login(user=my_email, password=password)
        connection.sendmail(
            from_addr=my_email,
            to_addrs="recipient email here",
            msg=f"Subject:Monday Motivation\n\n{quote}"
        )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# quotes.txt
"When you arise in the morning think of what a privilege it is to be alive, to think, to enjoy, to love..."  - Marcus Aurelius
"Either you run the day or the day runs you." - Jim Rohn
"Mondays are the start of the work week which offer new beginnings 52 times a year!" - David Dweck
"You've got to get up every morning with determination if you're going to go to bed with satisfaction." - George Lorimer
"Be miserable. Or motivate yourself. Whatever has to be done, it's always your choice." - Wayne Dyer
"Your Monday morning thoughts set the tone for your whole week. See yourself getting stronger, and living a fulfilling, happier & healthier life." - Germany Kent
"You don't have to be great to start, but you have to start to be great." - Zig Ziglar
"Each morning when I open my eyes I say to myself: I, not events, have the power to make me happy or unhappy today. I can choose which it shall be. Yesterday is dead, tomorrow hasn't arrived yet. I have just one day, today, and I'm going to be happy in it." - Groucho Marx
"Life is full of beauty. Notice it. Notice the bumble bee, the small child, and the smiling faces. Smell the rain, and feel the wind. Live your life to the fullest potential, and fight for your dreams." - Ashley Smith
"When you start to do the things that you truly love, it wouldn't matter whether it is Monday or Friday; you would be so excited to wake up each morning to work on your passions." - Edmond Mbiaka
"A ship is always safe at shore but that is not what it's built for." - Albert Einstein
"Stress is caused by being 'here' but wanting to be 'there.' - Eckhart Tolle
"People rarely succeed unless they have fun in what they are doing." - Dale Carnegie
"Your attitude, not your aptitude, will determine your altitude." - Zig Ziglar
"You may have to fight a battle more than once to win it." - Margaret Thatcher
"If you don't design your own life plan, chances are you'll fall into someone else's plan. And guess what they have planned for you? Not much." - Jim Rohn
"Try not to become a person of success, but rather try to become a person of value." - Albert Einstein
"Morning is an important time of day, because how you spend your morning can often tell you what kind of day you are going to have." - Lemony Snicket
"Just one small positive thought in the morning can change your whole day." - Dalai Lama
"Think of many things; do one." - Portuguese proverb
"One of the best pieces of advice I ever got was from a horse master. He told me to go slow to go fast. I think that applies to everything in life. We live as though there aren't enough hours in the day but if we do each thing calmly and carefully we will get it done quicker and with much less stress." - Viggo Mortensen
"Don't let what you cannot do interfere with what you can do." - John R. Wooden
"Success means doing the best we can with what we have. Success is the doing, not the getting; in the trying, not the triumph. Success is a personal standard, reaching for the highest that is in us, becoming all that we can be." - Zig Ziglar
"The starting point of all achievement is desire." - Napoleon Hill
"I was thinking one day and I realized that if I just had somebody behind me all the way to motivate me I could make a big difference. Nobody came along like that so I just became that person for myself." -  Unknown
"If you don't pay appropriate attention to what has your attention, it will take more of your attention than it deserves." - David Allen
"Spend eighty percent of your time focusing on the opportunities of tomorrow rather than the problems of yesterday." - Brian Tracy
"Keep on going, and the chances are that you will stumble on something, perhaps when you are least expecting it. I never heard of anyone ever stumbling on something sitting down." - Charles F. Kettering
"Shoot for the moon. Even if you miss, you'll land among the stars." - Brian Littrell
"Whether you think you can or think you can't, you're right." - Henry Ford
"The successful warrior is the average man, with laser-like focus." - Bruce Lee
"The only thing worse than starting something and failing ... is not starting something." - Seth Godin
"Do not be embarrassed by your failures, learn from them and start again." - Richard Branson
"For fast-acting relief, try slowing down. " - Lily Tomlin
"There is more to life than increasing its speed." - Mahatma Gandhi
"It is not a daily increase, but a daily decrease. Hack away at the inessentials." - Bruce Lee
"If I had eight hours to chop down a tree, I'd spend six hours sharpening my ax." - Abraham Lincoln
"Keep away from people who try to belittle your ambitions. Small people always do that, but the really great make you feel that you, too, can become great." - Mark Twain
"We can easily manage if we will only take, each day, the burden appointed to it. But the load will be too heavy for us if we carry yesterday's burden over again today, and then add the burden of the morrow before we are required to bear it." - John Newton
"If you want to make an easy job seem mighty hard, just keep putting off doing it." - Olin Miller
"Winners make a habit of manufacturing their own positive expectations in advance of the event." - Brian Tracy
"Follow effective actions with quiet reflection. From the quiet reflection will come even more effective action." - Peter Drucker
"Don't mistake activity with achievement." - John Wooden
"One of the symptoms of an approaching nervous breakdown is the belief that one's work is terribly important." - Bertrand Russell
"I've missed more than 9000 shots in my career. I've lost almost 300 games. 26 times, I've been trusted to take the game winning shot and missed. I've failed over and over and over again in my life. And that is why I succeed." - Michael Jordan
"Start where you are. Use what you have. Do what you can." - Arthur Ashe
"Show me a person who has never made a mistake and I'll show you someone who has never achieved much." - Joan Collins
"If you treat every situation as a life and death matter, you'll die a lot of times." - Dean Smith
"Amateurs sit and wait for inspiration, the rest of us just get up and go to work." - Stephen King
"The secret of getting ahead is getting started. The secret of getting started is breaking your complex overwhelming tasks into small manageable tasks, and then starting on the first one." - Mark Twain
"Things may come to those who wait, but only the things left by those who hustle." - Abraham Lincoln
"People who succeed have momentum. The more they succeed, the more they want to succeed, and the more they find a way to succeed. Similarly, when someone is failing, the tendency is to get on a downward spiral that can even become a self-fulfilling prophecy." - Tony Robbins
"I attribute my success to this: I never gave or took any excuse." - Florence Nightingale
"Take time to deliberate; but when the time for action arrives, stop thinking and go in." - Napoleon Bonaparte
"There is no traffic jam along the extra mile." - Roger Staubach
"Motivation is a fire from within. If someone else tries to light that fire under you, chances are it will burn very briefly." - Stephen R. Covey
"Success is liking yourself, liking what you do, and liking how you do it." - Maya Angelou
"Only put off until tomorrow what you are willing to die having left undone." - Pablo Picasso
"There are two types of people who will tell you that you cannot make a difference in this world: those who are afraid to try and those who are afraid you will succeed." - Ray Goforth
"Everything you want is on the other side of fear." - Jack Canfield
"Life is 10% what happens to us and 90% how we react to it." - Dennis P. Kimbro
"You are never too old to set another goal or to dream a new dream." - C.S. Lewis
"If you genuinely want something, don't wait for it  - teach yourself to be impatient." - Gurbaksh Chahal
"The truth is that our finest moments are most likely to occur when we are feeling deeply uncomfortable, unhappy, or unfulfilled. For it is only in such moments, propelled by our discomfort, that we are likely to step out of our ruts and start searching for different ways or truer answers." - M. Scott Peck
"For most of life, nothing wonderful happens. If you don't enjoy getting up and working and finishing your work and sitting down to a meal with family or friends, then the chances are you're not going to be very happy. If someone bases his/her happiness on major events like a great job, huge amounts of money, a flawlessly happy marriage or a trip to Paris, that person isn't going to be happy much of the time.If, on the other hand, happiness depends on a good breakfast, flowers in the yard, a drink or a nap, then we are more likely to live with quite a bit of happiness." - Andy Rooney
"Love yourself first and everything else falls into line. You really have to love yourself to get anything done in this world." - Lucille Ball
"We become what we think about." - Earl Nightingale
"I don't want to get to the end of my life and find that I lived just the length of it. I want to have lived the width of it as well." - Diane Ackerman
"New beginnings are often disguised as painful endings." - Lao Tzu
"The universe is change; our life is what our thoughts make it." - Marcus Aurelius
"For success, attitude is equally as important as ability." - Harry F. Banks
"Optimism is a happiness magnet. If you stay positive, good things and good people will be drawn to you." - Mary Lou Retton
"Don't wait. The time will never be just right." - Napoleon Hill
"It does not matter how slowly you go, so long as you do not stop." - Confucius
"Life's real failure is when you do not realize how close you were to success when you gave up." - Unknown
"Failure will never overtake me if my determination to succeed is strong enough." -  Og Mandino
"When we long for life without difficulties, remind us that oaks grow strong in contrary winds and diamonds are made under pressure." - Peter Marshall
"What you get by achieving your goals is not as important as what you become by achieving your goals." - Henry David Thoreau
"Ever tried. Ever failed. No matter. Try Again. Fail again. Fail better." - Samuel Beckett
"Success seems to be connected with action. Successful people keep moving. They make mistakes, but they don't quit." - Conrad Hilton
"Life begins at the end of your comfort zone." - Neale Donald Walsh
"When a resolute young fellow steps up to the great bully, the world, and takes him boldly by the beard, he is often surprised to find it comes off in his hand, and that it was only tied on to scare away the timid adventurers." - Ralph Waldo Emerson
"What seems to us as bitter trials are often blessings in disguise." - Oscar Wilde
"Aim for success, not perfection. Never give up your right to be wrong, because then you will lose the ability to learn new things and move forward with your life. Remember that fear always lurks behind perfectionism." - David M. Burns
"I find that the harder I work, the more luck I seem to have." - Thomas Jefferson
"If opportunity doesn't knock, build a door." - Milton Berle
"You can't make positive choices for the rest of your life without an environment that makes those choices easy, natural, and enjoyable." - Deepak Chopra
"Dreams can come true, but there is a secret. They're realized through the magic of persistence, determination, commitment, passion, practice, focus and hard work. They happen a step at a time, manifested over years, not weeks." - Elbert Hubbard
"The past has no power over the present moment." - Eckhart Tolle
"The best way to succeed in this world is to act on the advice you give to others." - Unknown
"If you take responsibility for yourself you will develop a hunger to accomplish your dreams." - Les Brown
"Success is not measured by what you accomplish, but by the opposition you have encountered, and the courage with which you have maintained the struggle against overwhelming odds." - Orison Swett Marden
"Never limit yourself because of others' limited imagination; never limit others because of your own limited imagination." - Mae Jemison
"When I'm old and dying, I plan to look back on my life and say, 'Wow, that was an adventure,' not, 'Wow, I sure felt safe.'" - Tom Preston-Werner
"Our greatest fear should not be of failure but of succeeding at things in life that don't really matter." - Francis Chan
"Nothing in the world can take the place of perseverance. Talent will not; nothing is more common than unsuccessful people with talent. Genius will not; unrewarded genius is almost legendary. Education will not; the world is full of educated derelicts. Perseverance and determination alone are omnipotent." - Calvin Coolidge
"It takes but one positive thought when given a chance to survive and thrive to overpower an entire army of negative thoughts." - Robert H. Schuller
"When we strive to become better than we are, everything around us becomes better too." - Paulo Coelho
"Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover." - Mark Twain
"A year from now you may wish you had started today." - Karen Lamb
"To laugh often and much; to win the respect of intelligent people and the affection of children... to leave the world a better place... to know even one life has breathed easier because you have lived. This is to have succeeded." - Ralph Waldo Emerson
"Don't brood. Get on with living and loving. You don't have forever." - Leo Buscaglia
"Success is not the key to happiness. Happiness is the key to success. If you love what you are doing, you will be successful." - Herman Cain

Birthday Wisher App

This app will send a birthday wish email to the birthday person.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# main.py
import datetime as dt
import pandas
import random
import smtplib

my_email = "your email here"
password = "your password here"

today = dt.datetime.now()
today_tuple = (today.month, today.day)

data = pandas.read_csv("birthdays.csv")
birthdays_dict = {(data_row.month, data_row.day): data_row for (index, data_row) in data.iterrows()}

if today_tuple in birthdays_dict:
    birthday_person = birthdays_dict[today_tuple]
    file_path = f"letter_templates/letter_{random.randint(1, 3)}.txt"
    with open(file_path) as letter_file:
        contents = letter_file.read()
        contents = contents.replace("[NAME]", birthday_person["name"])

    with smtplib.SMTP("smtp.gmail.com") as connection:
        connection.starttls()
        connection.login(user=my_email, password=password)
        connection.sendmail(
            from_addr=my_email,
            to_addrs=birthday_person["email"],
            msg=f"Subject:Happy Birthday!\n\n{contents}"
        )
1
2
3
# birthdays.csv
name,email,year,month,day
Aretas,ndimaret@gmail.com,1986,09,29
1
2
3
4
5
6
7
8
# letter_1.txt

Dear [NAME],

Happy Birthday!

From,
Aretas
This post is licensed under CC BY 4.0 by the author.