Python Forum
smtplib mail without subject - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: smtplib mail without subject (/thread-17763.html)



smtplib mail without subject - anna - Apr-23-2019

Hi,

I am capturing os command output and taking all files in list, checking last file creation time, if time is > 10min, sending mail, however mail is coming without subject line, please support.


import smtplib
import os.path, time
import datetime
FMT = '%Y-%m-%d %H:%M:%S'
data_Files = []
def send_mail():
    import smtplib
    import email
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    #from email.MIMEText import MIMEText
    #from email.mimetext import MIMEText
    mailserver = smtplib.SMTP('smtp.office365.com',587)
    mailserver.ehlo()
    mailserver.starttls()
    mailserver.login('[email protected]','xxxxx')
    fromaddr = '[email protected]'
    toaddr = '[email protected]'
    msg = MIMEMultipart()
    msg['From'] = fromaddr
    msg['To'] = toaddr
    msg['Subject'] = 'Netcool data transfer is not sucessful'
    msg = MIMEText('Netcool data transfer is not sucessful, please check FTP service')
    #body = 'Netcool data transfer is not sucessful, please check FTP service'
    #msg.attach(MIMEText(body, 'plain'))
    mailserver.sendmail('[email protected]','[email protected]',msg.as_string())
    mailserver.quit()
#Capturing command output
files  = os.popen('ls -ltrh *.xls  |tail -5').read()
# split output and append to List
for line in files.split('\n'):
    data_Files.append(line.strip().split(' ')[-1])
#Current File or last file in list
CurrentDataFile = data_Files[-2]
#get file creation time
date_time = time.ctime(os.path.getmtime(CurrentDataFile))
filetime = datetime.datetime.strptime(date_time, "%a %b %d %H:%M:%S %Y")
currentDT = datetime.datetime.now()
#Current OS time
oScurrentDT = currentDT.strftime("%Y-%m-%d %H:%M:%S")
tdelta = datetime.datetime.strptime(str(oScurrentDT), FMT) - datetime.datetime.strptime(str(filetime), FMT)
tdelta_in_min = tdelta.total_seconds() // 60
if tdelta_in_min > 10.0:
    #print(CurrentDataFile,tdelta,tdelta_in_min)
    send_mail()



RE: smtplib mail without subject - rlgoodman - Apr-23-2019

Instead of your line 23, you want

body = MIMEText('Netcool data transfer is not sucessful, please check FTP service')
msg.attach(body)



RE: smtplib mail without subject - anna - Apr-24-2019

Thanks.. its working now.