Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Email Results of Function
#11
That was it, thx! Adding "return" makes total sense. With "print", it was outputting to stdout before the script sent the email.

Here's the final code:

from email.headerregistry import Address
from email.message import EmailMessage
from email.mime.text import MIMEText
import os
import smtplib
import psutil

# mail server details
mail_server = 'localhost'
mail_server_port = 25

# email addresses
email_address = ''
recipient_address = ''
subject_desc = 'Test'

# linux mount points
def mounts():
    results = []
    devs = psutil.disk_partitions()
    for dev in devs:
            part = int(psutil.disk_usage(dev.mountpoint).percent)
            if part > 40:
                    results.append([dev.mountpoint, part])
    return results

list_mounts = list(mounts())

def list_out(list_in):
    return '\n'.join('Drive: {0:10s} Used Space: {1:5.2f}'.format(*drive) for drive in list_in)

email_body = list_out(list_mounts)

# email function
def create_email_message(from_address, to_address, subject, body):
        msg = EmailMessage()
        msg['From'] = from_address
        msg['To'] = to_address
        msg['Subject'] = subject
        msg.set_content(body)
        return msg

if __name__ == '__main__':
        msg = create_email_message(
                from_address=email_address,
                to_address=recipient_address,
                subject=subject_desc,
                body=email_body
        )


with smtplib.SMTP(mail_server, mail_server_port) as smtp_server:
        smtp_server.ehlo()
        smtp_server.send_message(msg)

print('Email sent successfully')
Thanks everyone for your help with this!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug New to coding, Using the zip() function to create Diret and getting weird results Shagamatula 6 1,438 Apr-09-2023, 02:35 PM
Last Post: Shagamatula
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,233 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  How to append one function1 results to function2 results SriRajesh 5 3,145 Jan-02-2020, 12:11 PM
Last Post: Killertjuh
  Python function returns inconsistent results bluethundr 4 3,187 Dec-21-2019, 02:11 AM
Last Post: stullis
  How do you add the results together each time a function is called? Exsul 10 5,116 Aug-09-2019, 09:18 PM
Last Post: ichabod801
  Greenplum Query results to Email HTML table krux_rap 1 2,505 Apr-10-2018, 09:12 PM
Last Post: Larz60+
  An email with inline jpg cannot be read by all email clients fpiraneo 4 3,978 Feb-25-2018, 07:17 PM
Last Post: fpiraneo
  Email - Send email using Windows Live Mail cyberzen 2 5,906 Apr-13-2017, 03:14 AM
Last Post: cyberzen

Forum Jump:

User Panel Messages

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