Luke D'Souza
Luke D'Souza

Reputation: 1

Trouble getting text in to an email

I'm trying to send an email to someone with information I've scraped from the web but I can't get the contents to send. I keep receiving empty emails. Any help would be great. I've tried all sorts of different numbers of ' and +s and i can't figure it out.

def singaporeweather():
    singaporehigh=singapore_soup.find(class_='tab-temp-high').text
    singaporelow=singapore_soup.find(class_='tab-temp-low').text
    print('There will be highs of ' + singaporehigh + ' and lows of ' + 
    singaporelow + '.')

def singaporesuns():
    singaporesunsets=singapore_soup.find(class_='row col-sm-5')
    suns_singapore=singaporesunsets.find_all('time')
    sunset_singapore=suns_singapore[1].text
    sunrise_singapore=suns_singapore[0].text
    print('Sunrise: ' + sunrise_singapore)
    print('Sunset: ' + sunset_singapore)


def ukweather():
    ukhigh= uk_soup.find('span', class_='tab-temp-high').text
    uklow= uk_soup.find(class_='tab-temp-low').text
    print('There will be highs of ' + ukhigh + ' and lows of ' + uklow + 
    '.')

def uksuns():
    uk_humid = uk_soup.find('div', class_='row col-sm-5')
    humidity=uk_humid.find_all('time')
    sunrise_uk=humidity[0].text
    sunset_uk= humidity[1].text
    print('Sunrise: '+str(sunrise_uk))
    print('Sunset: '+str(sunset_uk))

def ukdesc():
    uk_desc=uk_soup.find('div',class_='summary-text hide-xs-only')
    uk_desc_2=uk_desc.find('span')
    print(uk_desc_2.text)`enter code here`

def quotes():
    quote_text=quote_soup.find(class_='b-qt qt_914910 oncl_q').text
    author=quote_soup.find(class_='bq-aut qa_914910 oncl_a').text
    print('Daily quote:\n' + '\"'+quote_text +'\"'+ ' - ' + author +'\n')





def message():
    print('Subject:Testing\n\n')
    print(('Morning ' + 
    nameslist[random.randint(1(len(nameslist)-1))]).center(30,'*'), 
    end='\n'*2)

    quotes()

    print('UK'.center(30,'_') + '\n')
    ukweather()
    ukdesc()
    uksuns()

    print('\n' + 'Singapore'.center(30,'_') + '\n')
    singaporeweather()
    singaporedesc()
    singaporesuns()

smtpthing.sendmail('[email protected]', '[email protected]', str(message()))

Upvotes: 0

Views: 27

Answers (1)

Anis R.
Anis R.

Reputation: 6902

In your functions, instead of printing the results to the console, you should use return statements so that you can use the function's result in your main program. Otherwise, message() is returning null, which is why your email is empty (the main program cannot see message()'s result unless it is returned).

Try something like:

def singaporeweather():
    singaporehigh=singapore_soup.find(class_='tab-temp-high').text
    singaporelow=singapore_soup.find(class_='tab-temp-low').text
    return 'There will be highs of ' + singaporehigh + ' and lows of ' + 
    singaporelow + '.'

By using a return statement like this one, you will be able to use singaporeweather()'s result in your main program, e.g.:

var result = singaporeweather()

Using returns in the rest of your methods as well, you will be able to do the following in your function message():

def message():
    body = "" #your message
    body += 'Subject:Testing\n\n'
    body += ('Morning ' + nameslist[random.randint(1(len(nameslist)-1))]).center(30,'*')

    body += quotes()

    body += 'UK'.center(30,'_') + '\n'
     + ukweather()
     + ukdesc()
     + uksuns()

     + '\n' + 'Singapore'.center(30,'_') + '\n'
     + singaporeweather()
     + singaporedesc()
     + singaporesuns()

    #finally, don't forget to return!
    return body

Now you are returning body, now you can use message()'s result in your main program to send your email correctly:

smtpthing.sendmail('[email protected]', '[email protected]', str(message()))

Upvotes: 2

Related Questions