Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 13th, 2004, 10:44 PM
ah new ah new is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 40 ah new User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 h 1 m 21 sec
Reputation Power: 5
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

Reply With Quote
  #2  
Old December 14th, 2004, 05:38 AM
sfb sfb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 447 sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 1 h 43 m 45 sec
Reputation Power: 10
Quote:
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 )

Reply With Quote
  #3  
Old December 14th, 2004, 08:17 PM
ah new ah new is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 40 ah new User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 h 1 m 21 sec
Reputation Power: 5
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > SMTP duplicated...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT