
December 14th, 2004, 08:17 PM
|
|
Contributing User
|
|
Join Date: Nov 2004
Posts: 40
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
|