Trying to Send Email via Python

时间:2019-04-16 23:33:13

标签: python web-scraping

So I have this one code and I'm trying to scrap a content from a link and then send it via E-mail (using the same code). But I'm getting an error. Could someone help?

import requests
import ast
import smtplib

def get_data():
    source = requests.get('http://apps.cptm.sp.gov.br:8080/AppMobileService/api/LinhasMetropolitanas')
    content_list=ast.literal_eval(source.content.decode("utf-8"))
    #for d in content_list:
    #   print(d['Nome'],d['Status'])
    return content_list

# LOGIN E SENHA DO ROBO QUE ENVIA
gmail_user = '@gmail.com'
gmail_app_password = 'pass'

# AS INFORMAÇÕES DE EMAIL
sent_from = gmail_user
sent_to = ['@gmail.com']
sent_subject = "Email Teste"
sent_body = get_data()

email_text = """\
From: %s
To: %s
Subject: %s

%s
""" % (sent_from, ", ".join(sent_to), sent_subject, sent_body)
# SEND EMAIL OR DIE TRYING!!!

try:
    server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    server.ehlo()
    server.login(gmail_user, gmail_app_password)
    server.sendmail(sent_from, sent_to, email_text)
    server.close()

    print('Email sent!')
except Exception as exception:
    print("Error: %s!\n\n" % exception)

Error: 'ascii' codec can't encode characters in position 96-97: ordinal not in range(128)!

I'd like to get the result from def get_data and then send it via email.

0 个答案:

没有答案