Python Forum
Having a hard time combining two parts of code.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Having a hard time combining two parts of code.
#1
Hey everyone!
It's been a week since I started making this script. The main idea was to make a script that will send a bitcoin price to my email every 30 minutes using an API.
I decided to make it two parts, and then combine it together. The first part of the price updates every 30 minutes was pretty easy to make. The second part with the email was a little bit harder, but I understood how it works anyways. When sending an email to myself with a simple message, everything seems to be working fine, but I just can't understand how do I send the price, because the way that I did it, makes it just send a blank email.

import smtplib
import time
import datetime
import requests


email_sender_address = 'n/a'
email_receiver_address = 'n/a'
email_password = 'n/a'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(email_sender_address, email_password)

while True:
    response = requests.get('https://blockchain.info/ticker')

    if response.status_code == 200:
        data = response.json()
        now  = datetime.datetime.now()
        message = ("price:%d" %(data["USD"]["last"]))
        server.sendmail(email_sender_address, email_receiver_address, message)
    time.sleep(1800)
Reply
#2
If message needs to be a string then the () brackets are the problem.
Hopefully you use a python version >= 3.6 then i would suggest using f-string

message = f'price: {data["USD"]["last"]}'
Reply
#3
(May-09-2020, 04:09 PM)ThomasL Wrote: If message needs to be a string then the () brackets are the problem.
Hopefully you use a python version >= 3.6 then i would suggest using f-string

message = f'price: {data["USD"]["last"]}'

Well, that did something. If I delete the "Price:", and then try to send it, I actually get the price, but as soon as I put any words into the message, I get a blank message. Now the problem is that not only that I want the "Price:" word, I also want a time stamp that I originally planned to put there.
Reply
#4
What happens if you try
message = f'price: {data["USD"]["last"]}'.encode('ASCII')
Reply
#5
(May-09-2020, 06:20 PM)ThomasL Wrote: What happens if you try
message = f'price: {data["USD"]["last"]}'.encode('ASCII')

Seems to be the same.
I noticed, if I put anything before
{data["USD"]["last"]}
then the email seems to be blank. If I don't put anything before, such as the word "Price" it seems to be working fine, sending only the price number.
Reply
#6
Use the MIMEText when send message as plain text.
Working test.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import datetime

fromaddr = "[email protected]"
toaddr = "[email protected]"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Test send mail"

response = requests.get('https://blockchain.info/ticker')
if response.status_code == 200:
    data = response.json()
    now  = datetime.datetime.now()
    text_message = f'price: {data["USD"]["last"]} at time: {now}'
    msg.attach(MIMEText(text_message, 'plain'))
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login('[email protected]', 'pass')
    message = msg.as_string()
    server.sendmail(fromaddr, toaddr, message)
    server.quit()
Received mail:
Output:
Test send mail [email protected] 23:49 (for 3 minutter siden) to me price: 9651.26 at time: 2020-05-09 23:49:03.008874
Reply
#7
(May-09-2020, 09:54 PM)snippsat Wrote: Use the MIMEText when send message as plain text.
Working test.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import datetime

fromaddr = "[email protected]"
toaddr = "[email protected]"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Test send mail"

response = requests.get('https://blockchain.info/ticker')
if response.status_code == 200:
    data = response.json()
    now  = datetime.datetime.now()
    text_message = f'price: {data["USD"]["last"]} at time: {now}'
    msg.attach(MIMEText(text_message, 'plain'))
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login('[email protected]', 'pass')
    message = msg.as_string()
    server.sendmail(fromaddr, toaddr, message)
    server.quit()
Received mail:
Output:
Test send mail [email protected] 23:49 (for 3 minutter siden) to me price: 9651.26 at time: 2020-05-09 23:49:03.008874


Oh well, that looks promising!
I am not able to check it right now, but if you got output, then I guess i should get too.
Thanks a lot for helping!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Missing parts of Code Felipe1991_GVT 3 191 Mar-22-2024, 05:58 PM
Last Post: deanhystad
  Hard time trying to figure out the difference between two strings carecavoador 2 646 Aug-16-2023, 04:53 PM
Last Post: carecavoador
  How to expand and collapse individual parts of the code in Atom Lora 2 1,104 Oct-06-2022, 07:32 AM
Last Post: Lora
  python Extract sql data by combining below code. mg24 1 915 Oct-03-2022, 10:25 AM
Last Post: mg24
  Assistance with running a few lines of code at an EXACT time nethatar 5 3,167 Feb-24-2021, 10:43 PM
Last Post: nilamo
  Stumped by my own code (ratio & epoch-time calculation). MvGulik 2 2,090 Dec-30-2020, 12:04 AM
Last Post: MvGulik
  Code taking too much time to process ErPipex 11 4,820 Nov-16-2020, 09:42 AM
Last Post: DeaD_EyE
  What is the run time complexity of this code and please explain? samlee916 2 2,258 Nov-06-2020, 02:37 PM
Last Post: deanhystad
  Having a hard time conceptualizing how to print something MysticLord 6 3,042 Sep-19-2020, 10:43 PM
Last Post: MysticLord
  Having hard time understanding the function self-returning itself twice jagasrik 2 2,450 Aug-15-2020, 08:50 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020