#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    40
    Rep Power
    10

    Unhappy SMTP duplicated...


    hey!

    i had wrote some code to send mail with attachment by using MIMEMultipart.

    the .py is looks like this...
    i have written a .py file (test_mail.py) to provide several input that needs to send a mail with some attachments. it calls the implementation file.
    besides, i also have a file called send_mail.py which serves as the implemetation for formatting & send out the mail. all MIMEMultipart object is defined inside this .py file.

    the error i have is the mail will be duplicated sent when i execute test_mail.py for the 2nd, 3rd... time
    eg: if i execute test_mail.py for 3 times, i will received 3 mails. if the file size of the 1st mail is 50kb, the 2nd mail's file size is 100kb, and the 3rd mail file size will be 150kb. all message and attachment files will be duplicated as long as i call the test_mail.py again.

    i need some help from you guys..
    thanks for advice,

    ah new
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    i need some help from you guys..
    Umm... don't do whatever it is you did to cause it to send multiple mails?


    (hint: post some code )
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    40
    Rep Power
    10
    here is the code for the test drive file...
    Code:
    import os
    import test_mail
    
    import FDFxml
    #dictionary
    d_actionMail = FDFxml.getData('FDFactionMail.xml')
    
    def outsider():
        # general info
        SENDER = str(d_actionMail.get('Sender'))
        RECEIVER = str(d_actionMail.get('Receiver'))
        SUBJECT = str(d_actionMail.get('Subject'))
    
        # msg info
        os.chdir('C:\\mail\\')
        fp_info = open('att_mail.txt', 'r')
        m_info = fp_info.read()
        fp_info.close()
        
        # attachment files
        l_att = []
        l_att.append(abc.doc')
        l_att.append('xyz.pdf')
    
        # call implementation file
        test_mail.main(SENDER, RECEIVER, SUBJECT, m_info, l_att)
    
        l_att = None # set the list to NULL
    
            
    if __name__ == '__main__' :
        outsider()

    the implementation file...

    Code:
    import smtplib, sys, os, string, mimetypes, time
    from email import Encoders
    from email.MIMEBase import MIMEBase
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEText import MIMEText
    
    import FDFxml
    #get info from xml file
    d_actionMail = FDFxml.getData('FDFactionMail.xml')
    
    #define a MIMEMultipart object
    mainMsg = MIMEMultipart() 
    
    
    def main(sender, receiver, subject, message_info, list_att):
        ''' main method '''
        # call sub modules
        make_message(message_info)  
        make_attachment(list_att)  
        send_mail(sender, receiver, subject, mainMsg) 
        write_log(sender, receiver, subject, list_att)
        
    
    def make_message(msgInfo):
        ''' build message by copy content from a text file '''
        mainMsg.premamble = msgInfo + "\n"
        mainMsg.epilogue = ''
        msgString =  msgInfo + "\n"
        msg = MIMEText(msgString) 
        mainMsg.attach(msg) #attach as plain text
    
    
    def make_attachment(lst_att):
        ''' convert file to attachment '''
        os.chdir('C:\\mail\\')
        fileNames = [f for f in os.listdir(os.curdir) if os.path.isfile(f)] # check - file exists
        done_att = 0 # no of attechement that been attached
        no_att = len(lst_att) # no of item in list
        for i in range (no_att): # loop in to the list
            for fileName in fileNames: # loop in to the folder
                # check - the file which want to attach & no of file havent attach
                if fileName == lst_att[i] and done_att <= no_att:
                    fp_att = open(fileName,'rb')
                    attach = MIMEBase('application','mixed')
                    attach.set_payload(fp_att.read())
                    fp_att.close()
                    Encoders.encode_base64(attach)
                    attach.add_header('Content-Disposition','attachment', filename=fileName) #set as an attachment
                    mainMsg.attach(attach) #attach as attachment
                    done_att = done_att + 1
    
       
    def send_mail(s_der, r_ver, title, mainMsg):
        ''' send the mail '''
        #get value from xml file
        MAIL_SERVER = str(d_actionMail.get('Mail_server'))
        #initial MIMEMultipart object
        mainMsg['Subject'] = title
        mainMsg['From'] = s_der
        
        s = smtplib.SMTP(MAIL_SERVER)
        s.sendmail(mainMsg['From'] , r_ver, mainMsg.as_string())
        s.close()
    
        mainMsg = None
       
        
    def write_log(s_der, r_ver, title, lst_att):
        ''' write to log file for every transaction '''
        #get curent date & time
        currDate = time.strftime("%Y-%m-%d", time.localtime(time.time()))
        currTime = time.strftime("%H:%M:%S", time.localtime(time.time()))
    
        no_att = len(lst_att)
        os.chdir('C:\\mail\\log\\')
        fp_log = open(currDate + '.txt','a+')
        print >> fp_log, "Transaction at " + currTime # begin of a transaction
        print >> fp_log, "From: " + s_der + "\tTo: ", r_ver + "\tTitle: " + title
        for i in range(0, len(lst_att)):
            print >> fp_log, i+1, " " + lst_att[i]
        print >> fp_log, "\n" # end of a transaction
        fp_log.close()
    
    
    
    if __name__ == '__main__' :
        main(sender, receiver, subject, message_info, list_att)
    thanks for advice...

    ah new

IMN logo majestic logo threadwatch logo seochat tools logo