1

I'm making a program that will retrieve the data from google sheet which is a spending diary of how much I spend a week. After successfully retrieving the data, program will send an email to me that how much I spend day by day in a week.

Photo of my spending diary: enter image description here

I'm using gspread to retrieve the data, and I was able to retrive the data column by column using for loop

And it gives me data as a list. For example, if I do

num_days = 7
goods = []

for day in range(2, num_days + 2):
    goods.append(worksheet.col_values(day))


OUTPUT: [['Mon 04/08/2019', 'Edeka 20.07'], ['Tue 04/09/2019', 'Edeka 9.77', 'Gym 60'], ['Wed 04/10/2019', 'Party 5', 'Jiujitsu System 30', 'Jiujitsu card 5', 'Jiujitsu Monthly fee 54'], ['Thu 04/11/2019', 'Laundry 3.5', 'Part 7.5 Euro'], ['Fri 04/12/2019', 'Edeka 5.95', 'Laundry 2'], ['Sat 04/13/2019', 'Food Festival 20 ', 'DM Hair Roller 3.25', 'Rewe 9.97'], ['Sun 04/14/2019', 'Monitor 224.99']]

So I am able to retrieve the column by column and put them in a list as a list.

That's all good. But I want to send email in this following format.

EX)

Mon: Edeka 20.07

Tue: Edeka 9.77, Gym 60

...

Sun: Monitor 224.99

And here is the photo of email that has arrived into my mailbox. I want first elements to be gone to just show what goods I bought, excluding the date. enter image description here

How can I form my message of email to be sent like the photo above?

I'm currently using smtplib to send an email, but I don't know how to fit just goods that I bought into the message.

def send_email_for_spending(money_spent_week):
    with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
        smtp.ehlo()
        smtp.starttls()
        smtp.ehlo()

        smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)

        # title for your email
        subject = "Here is how much you spent in this week."

        # Number of Days
        num_days = 7


        goods = []

        # it starts from 2 because column that has monday is 2nd  
        # column
        for day in range(2, num_days + 2):
            goods.append(worksheet.col_values(day))

        message = f"""
            MON:
                {goods[0]}
            TUE:
                {goods[1]}
            WED:
                {goods[2]}
            THU:
                {goods[3]}
            FRI:
                {goods[4]}
            SAT:
                {goods[5]}
            SUN:
                {goods[6]}
        """

        smtp.sendmail(EMAIL_ADDRESS, 'dudeindaegu@gmail.com', message)
Sambo Kim
  • 1,383
  • 2
  • 11
  • 12
  • You should keep using smptlib as it is the best email sending module in python (if there are any others). However, to fix your problem, you can add `day.pop(0)` after the `for day in range (2, num_days + 2): ` You can find more info about this [here](https://stackoverflow.com/questions/4426663/how-to-remove-the-first-item-from-a-list) – Arnav Poddar Jun 12 '19 at 19:37
  • 1
    Oh that will work! However, is there any way that I can print it like normal sentence? for example, if I do **day.pop(0)**, it will print out **'[edeka 20.7, gym 60]'** like so, But I do want to print out **'edeka 20.7, gym 60'** like this without bracket. Is there any way to do it? – Sambo Kim Jun 16 '19 at 07:20
  • Yes there is. After you pop, you can use the `join` method like so: `output="".join(goods)` – Arnav Poddar Jun 18 '19 at 14:25
  • Put this after the place where you put the `.pop()` thing – Arnav Poddar Jun 18 '19 at 14:25
  • @ArnavPoddar Yes, it worked perfectly fine. Thanks a lot! – Sambo Kim Jun 18 '19 at 20:38

1 Answers1

1

Yes, there is a solution to your problem. You should keep using smptlib as it is the best email sending module in python (if there are any others). However, to fix your problem, you can add day.pop(0) after the for day in range (2, num_days + 2): You can find more info about this here. After you pop, you can use the join method like so: output="".join(goods) Put this after the place where you put the .pop() thing

Arnav Poddar
  • 354
  • 2
  • 18