Python Forum
smtplib mail without subject
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
smtplib mail without subject
#1
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()
Reply
#2
Instead of your line 23, you want

body = MIMEText('Netcool data transfer is not sucessful, please check FTP service')
msg.attach(body)
Reply
#3
Thanks.. its working now.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sending Attachments via smtplib [SOLVED] AlphaInc 3 2,167 Oct-28-2021, 12:39 PM
Last Post: AlphaInc
  Sending random images via smtplib [SOLVED] AlphaInc 0 1,690 Oct-19-2021, 10:10 AM
Last Post: AlphaInc
  [UnicodeEncodeError from smtplib] yoohooos 0 3,391 Sep-25-2021, 04:27 AM
Last Post: yoohooos
  Mail issue Mihil 3 2,663 Dec-03-2020, 05:25 AM
Last Post: Mihil
  Understanding The Arguments for SMTPlib - sendmail JoeDainton123 3 2,727 Aug-03-2020, 08:34 AM
Last Post: buran
  TimeOutError when trying to initiate smtplib.SMTP thecosmos 0 3,339 Jun-19-2020, 05:30 AM
Last Post: thecosmos
  SMTPlib help tpolim008 4 3,833 Apr-27-2020, 11:11 PM
Last Post: tpolim008
  smtplib: string formatting not carrying over to email ClassicalSoul 1 2,658 Apr-22-2020, 09:58 PM
Last Post: bowlofred
  Issue with text encoding ans smtplib.SMTP.sendmail() JustSomeUsername383 1 4,154 Jul-23-2019, 03:15 PM
Last Post: JustSomeUsername383
  o365 special subject mail download issue anna 3 2,908 May-16-2019, 07:16 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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