SMTP- Email Help

This is the place for queries that don't fit in any of the other categories.

SMTP- Email Help

Postby SoulZB » Thu Apr 25, 2013 3:26 am

Hi Guys. I need some help with the coding for my program.

This coding is suppose to sort text file according to the latest date and send the latest file. Attach it to my email and sent to another email account. But somehow the program is unable to send email.

Code: Select all
#!/usr/bin/python
import os, glob, smtplib, datetime
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders

process_list=[]
AP_filelist=[]
Client_filelist=[]
BlackClient_filelist=[]
sniff_assoc = 0
i = 0

to = "iamsiaozharbor@hotmail.sg"
gmail_user = "root.phidgets@gmail.com"
gmail_pwd = "rootphidget"

def mail(to, subject, text, attach):
   msg = MIMEMultipart()

   msg['From'] = gmail_user
   msg['To'] = to
   msg['Subject'] = subject

   msg.attach(MIMEText(text))

   part = MIMEBase('application', 'octet-stream')
   part.set_payload(open(attach, 'rb').read())
   Encoders.encode_base64(part)
   part.add_header('Content-Disposition',
           'attachment; filename="%s"' % os.path.basename(attach))
   msg.attach(part)

   mailServer = smtplib.SMTP("smtp.gmail.com", 587)
   mailServer.ehlo()
   mailServer.starttls()
   mailServer.ehlo()
   mailServer.login(gmail_user, gmail_pwd)
   mailServer.sendmail(gmail_user, to, msg.as_string())
   # Should be mailServer.quit(), but that crashes...
   mailServer.close()


Client_files = glob.glob('/root/wifi/Output_assoc/*Rouge-Client*.txt')
def get_oldest_Client_file(Client_files):
    for data in Client_files:
        stats = os.stat(data)
        Client_filelist.append(data)
    sortedClient = sorted(Client_filelist)
    return sortedClient[0]


BlackClient_files = glob.glob('/root/wifi/Output_arp/*DeAuth-Client*.txt')
def get_oldest_BlackClient_file(BlackClient_files):
    for data in BlackClient_files:
        stats = os.stat(data)
        BlackClient_filelist.append(data)
    sortedBlackClient = sorted(BlackClient_filelist)
    return sortedBlackClient[0]


if __name__=='__main__':

    try:
        if os.path.getsize(get_oldest_Client_file(Client_files)) > 1:
            mail("root.phidgets@gmail.com",
                "Rouge Client Found",
                "Client MAC address in the attachment",
                get_oldest_Client_file(Client_files))
            print str(datetime.datetime.now()) + '  --  ' + get_oldest_Client_file(Client_files) + ' was sent.'
            os.remove(get_oldest_Client_file(Client_files))
        else:
            for line in os.popen("ps -e"):
                fields  = line.split()
                process = fields[3]
                process_list.append(process)

            for pro in process_list:
                if pro == 'sniff_assoc':
                    sniff_assoc = 1
                    i+=1

            if sniff_assoc == 0:
                os.remove(get_oldest_Client_file(Client_files))
                print str(datetime.datetime.now()) + '  --  ' + 'program is not running. ' + get_oldest_Client_file(Client_files) + ' is remove.'
            else:
                print str(datetime.datetime.now()) + '  --  ' + 'Client file is still writing.'
    except:
        print str(datetime.datetime.now()) + '  --  ' + 'No AP file to send.'


    try:
        if os.path.getsize(get_oldest_BlackClient_file(BlackClient_files)) > 1:
            mail("root.phidgets@gmail.com",
                "DeAuth Rouge Client",
                "Client MAC address in the attachment.",
                get_oldest_BlackClient_file(BlackClient_files))
            print str(datetime.datetime.now()) + '  --  ' + get_oldest_BlackClient_file(BlackClient_files) + ' was sent.'
            os.remove(get_oldest_BlackClient_file(BlackClient_files))
        else:

            for line in os.popen("ps -e"):
                fields  = line.split()
                process = fields[3]
                process_list.append(process)

            for pro in process_list:
                if pro == 'sniff_arp':
                    sniff_assoc = 1
                    i+=1

            if sniff_assoc == 0:
                os.remove(get_oldest_BlackClient_file(BlackClient_files))
                print str(datetime.datetime.now()) + '  --  ' + 'program is not running. ' + get_oldest_BlackClient_file(BlackClient_files) + ' is REMOVED!!'
            else:
                print str(datetime.datetime.now()) + '  --  ' + 'DeAuth client file is still writing!'
    except:
        print str(datetime.datetime.now()) + '  --  ' + 'No DeAuth client file to send.'
Last edited by SoulZB on Thu Apr 25, 2013 4:45 am, edited 1 time in total.
SoulZB
 
Posts: 2
Joined: Thu Apr 25, 2013 3:25 am

Re: SMTP- Email Help

Postby metulburr » Thu Apr 25, 2013 3:30 am

please post code in code tags and not quote tags for indentation preservation
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1489
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: SMTP- Email Help

Postby Kebap » Thu Apr 25, 2013 9:52 am

SoulZB wrote:somehow the program is unable to send email.
How can you tell?
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 397
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: SMTP- Email Help

Postby SoulZB » Thu Apr 25, 2013 11:14 am

I did not receive the email with the text file when I run the program.
SoulZB
 
Posts: 2
Joined: Thu Apr 25, 2013 3:25 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 4 guests