Python Forum

Full Version: Having a hard time combining two parts of code.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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"]}'
(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.
What happens if you try
message = f'price: {data["USD"]["last"]}'.encode('ASCII')
(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.
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
(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!