[IMP] eml retun python email object and saving mail to windows %temp% dictionary

bzr revid: jam@tinyerp.com-20101007063217-gxt0un71raran4vb
This commit is contained in:
Jigar Amin
2010-10-07 12:02:17 +05:30
parent d47e64b030
commit 034d7f3689
4 changed files with 68 additions and 68 deletions
@@ -196,7 +196,7 @@ class OutlookAddin:
toolbarweb = bars.Item("Web")
item = toolbarweb.Controls.Add(Type = constants.msoControlButton, Temporary = True)
item = self.toolbarButtonPOpenPartner = DispatchWithEvents(item, OpenPartner)
item = self.toolbarButtonOpenPartner = DispatchWithEvents(item, OpenPartner)
item.Caption = "Open Partners"
item.TooltipText = "Click to Open OpenERP Partner Contact Information."
item.Enabled = True
@@ -679,17 +679,21 @@ def CreateCase(btnProcessor,*args):
for rec in r:
if rec[0] == 'res.partner':
partner_ids.append(rec[1])
f = False
#Create new case
try:
NewConn.CreateCase(str(section), mail, partner_ids)
f = NewConn.CreateCase(str(section), mail, partner_ids)
msg="New Document created."
flag=flag_info
except Exception,e:
msg="New Document not created \n\n"+str(e)
flag=flag_error
win32ui.MessageBox(msg, "Create Document", flag)
return
if f:
win32ui.MessageBox(msg, "Create Document", flag)
return
else:
win32ui.MessageBox("Error While creating document.\n Document can not be created.", "Create Document", flag_error)
return
else:
win32ui.MessageBox("Document can not be created. CRM not installed", "Create Object", flag_info)
except Exception, e:
@@ -935,9 +939,13 @@ def SetDefaultContact(txtProcessor,*args):
global partner_ref
global new_con_country
global new_con_state
state = win32gui.GetDlgItemText(txtProcessor.window.hwnd, txtProcessor.other_ids[3])
country = win32gui.GetDlgItemText(txtProcessor.window.hwnd, txtProcessor.other_ids[2])
state = win32gui.GetDlgItemText(txtProcessor.window.hwnd, txtProcessor.other_ids[2])
country = win32gui.GetDlgItemText(txtProcessor.window.hwnd, txtProcessor.other_ids[3])
if txtProcessor.init_done:
if new_con_country == "":
new_con_country = country
if new_con_state == "":
new_con_state = state
win32gui.SetDlgItemText(txtProcessor.window.hwnd, txtProcessor.other_ids[1],partner_ref)
win32gui.SetDlgItemText(txtProcessor.window.hwnd, txtProcessor.control_id,name)
win32gui.SetDlgItemText(txtProcessor.window.hwnd, txtProcessor.other_ids[0],email)
@@ -1063,6 +1071,8 @@ def set_name_email(dialogProcessor,*args):
name = win32gui.GetDlgItemText(dialogProcessor.window.hwnd, dialogProcessor.other_ids[0])
email = win32gui.GetDlgItemText(dialogProcessor.window.hwnd, dialogProcessor.other_ids[1])
#Function for Getting default mail when dialog is loaded and finding contact related to this
#setting values to the appropriate text areas
def GetDefaultEmail(txtProcessor,*args):
from win32com.client import Dispatch
@@ -1403,27 +1413,32 @@ def OpenPartnerForm(txtProcessor,*args):
def SerachOpenDocuemnt(txtProcessor,*args):
from win32com.client import Dispatch
import win32con
import win32ui
b = check()
if not b:
return
global web_server
global web_server_port
ex = txtProcessor.window.manager.outlook.ActiveExplorer()
assert ex.Selection.Count == 1
mail = ex.Selection.Item(1)
link_box = None
#Acquiring control of the text box
link_box = txtProcessor.GetControl()
try:
link_box = txtProcessor.GetControl()
except Exception,e:
win32ui.MessageBox(str(e),"Excet")
if web_server.strip() == "" or web_server.strip() == "http:\\\\":
win32gui.SendMessage(link_box, win32con.WM_SETTEXT, 0, " <Invalid Server Address>")
txtProcessor.init_done=True
return
linktodoc = ""
#Reading Current Selected Email.
vals = []
ex = txtProcessor.window.manager.outlook.ActiveExplorer()
assert ex.Selection.Count == 1
mail = ex.Selection.Item(1)
e_id = ustr(mail.EntryID).encode('iso-8859-1')
vals = list(NewConn.SearchEmailResources(mail))
win32ui.MessageBox(str(mail),"mail")
try:
vals = NewConn.SearchEmailResources(mail)
except Exception,e:
win32ui.MessageBox(str(e),"Excep")
if vals == None:
win32gui.SendMessage(link_box, win32con.WM_SETTEXT, 0, "< Mail is still not archived.>")
txtProcessor.init_done=True
@@ -1629,9 +1644,6 @@ dialog_map = {
"IDD_SERVER_PORT_DIALOG" : (
(CloseButtonProcessor, "IDCANCEL"),
(OKButtonProcessor, "IDOK ID_SERVER ID_PORT IDR_XML_PROTOCOL"),
# (RadioButtonProcessor, "IDR_XML_PROTOCOL", GetConn, ()),
# (RadioButtonProcessor, "IDR_XMLS_PROTOCOL", GetConn, ()),
# (RadioButtonProcessor, "IDR_NETRPC_PROTOCOL", GetConn, ()),
),
"IDD_SYNC" : (
@@ -26,7 +26,8 @@ import chilkat
import os
from manager import ustr
import win32ui
email = chilkat.CkEmail()
import email
cemail = chilkat.CkEmail()
dt = chilkat.SYSTEMTIME()
def generateEML(mail):
sub = (mail.Subject).replace(' ','')
@@ -36,20 +37,21 @@ def generateEML(mail):
sender_name = mail.SenderEmailAddress
attachments=mail.Attachments
email = chilkat.CkEmail()
email.put_Subject (ustr(sub).encode('iso-8859-1'))
email.put_Body (ustr(body).encode('utf-8'))
email.put_FromAddress (ustr(sender_email).encode('iso-8859-1'))
email.put_From (ustr(sender_email).encode('iso-8859-1'))
cemail = chilkat.CkEmail()
cemail.put_Subject (ustr(sub).encode('iso-8859-1'))
cemail.put_Body (ustr(body).encode('utf-8'))
cemail.put_FromAddress (ustr(sender_email).encode('iso-8859-1'))
cemail.put_From (ustr(sender_email).encode('iso-8859-1'))
for i in xrange(1, recipients.Count+1):
name = ustr(recipients.Item(i).Name).encode('iso-8859-1')
address = ustr(recipients.Item(i).Address).encode('iso-8859-1')
email.AddTo(name,address)
cemail.AddTo(name,address)
eml_name= ustr(sub).encode('iso-8859-1')+'-'+str(mail.EntryID)[-9:]
ls = ['*', '/', '\\', '<', '>', ':', '?', '"', '|', '\t', '\n']
mails_folder_path = os.path.abspath(os.path.dirname(__file__)+"\\dialogs\\resources\\mails\\")
#mails_folder_path = os.path.abspath("%temp%\\dialogs\\resources\\mails\\")
mails_folder_path = os.path.abspath("C:\\printing")
attachments_folder_path = mails_folder_path + "\\attachments\\"
if not os.path.exists(attachments_folder_path):
os.makedirs(attachments_folder_path)
@@ -70,7 +72,7 @@ def generateEML(mail):
os.remove(att_file)
f1 = att_file
attachments[i].SaveAsFile(att_file)
contentType = email.addFileAttachment(att_file)
contentType = cemail.addFileAttachment(att_file)
if (contentType == None ):
print mail.lastErrorText()
sys.exit()
@@ -85,10 +87,11 @@ def generateEML(mail):
f = eml_name.split('-')
eml_name = f[0][0:l] + '.' + f[-1]
eml_path = ustr(os.path.join(mails_folder_path,eml_name+".eml")).encode('iso-8859-1')
success = email.SaveEml(eml_path)
success = cemail.SaveEml(eml_path)
fp = open(eml_path, 'rb')
new_mail = email.message_from_file(fp)
fp.close()
if (success == False):
print email.lastErrorText()
print cemail.lastErrorText()
sys.exit()
print "Saved EML!",eml_path
return eml_path
return new_mail
@@ -137,19 +137,8 @@ class XMLRpcConn(object):
conn = xmlrpclib.ServerProxy(self._uri + '/xmlrpc/object')
import eml
new_msg = files = ext_msg =""
eml_path=eml.generateEML(mail)
att_name = ustr(eml_path.split('\\')[-1])
flag=False
new_mail=eml.generateEML(mail)
attachments=mail.Attachments
try:
fp = open(eml_path, 'rb')
msg =fp.read()
fp.close()
new_mail = email.message_from_string(str(msg))
except Exception,e:
win32ui.MessageBox(str(e),"Reading Error Mail")
for rec in recs: #[('res.partner', 3, 'Agrolait')]
model = rec[0]
res_id = rec[1]
@@ -185,10 +174,8 @@ class XMLRpcConn(object):
result = {}
if attachments:
result = self.MakeAttachment([rec], mail)
attachment_ids = result.get(model, {}).get(res_id, [])
ids = execute(conn,'execute',self._dbname,int(self._uid),self._pwd,'email.server.tools','history',model, res_id, msg, attachment_ids)
new_msg += """- {0} : {1}\n""".format(object_name,str(rec[2]))
flag = True
@@ -251,22 +238,24 @@ class XMLRpcConn(object):
return res
def CreateCase(self, section, mail, partner_ids, with_attachments=True):
res={}
import win32ui
import eml
section=str(section)
partner_ids=eval(str(partner_ids))
conn = xmlrpclib.ServerProxy(self._uri+ '/xmlrpc/object')
flag = False
id = -1
try:
eml_path=eml.generateEML(mail)
fp = open(eml_path, 'rb')
msg =fp.read()
fp.close()
new_mail = email.message_from_string(str(msg))
execute(conn,'execute',self._dbname,int(self._uid),self._pwd,'email.server.tools','process_email',section, str(new_mail))
conn = xmlrpclib.ServerProxy(self._uri+ '/xmlrpc/object')
email=eml.generateEML(mail)
id = execute(conn,'execute',self._dbname,int(self._uid),self._pwd,'email.server.tools','process_email',section, email)
if id > 0:
flag = True
return flag
else:
flag = False
return flag
except Exception,e:
win32ui.MessageBox(str(e),"Mail Reading Error")
pass
win32ui.MessageBox("Create Case\n"+str(e),"Mail Reading Error")
return flag
def MakeAttachment(self, recs, mail):
attachments = mail.Attachments
@@ -412,23 +401,19 @@ class XMLRpcConn(object):
def SearchEmailResources(self, mail):
import win32ui
import eml
conn = xmlrpclib.ServerProxy(self._uri+ '/xmlrpc/object')
sub = ""
res_vals = []
try:
parse_mail = execute(conn,'execute',self._dbname,int(self._uid),self._pwd,'email.server.tools','parse_message',mail)
win32ui.MessageBox(str(parse_mail),"Temp")
eml_path=eml.generateEML(mail)
fp = open(eml_path, 'rb')
msg =fp.read()
fp.close()
new_mail = email.message_from_string(str(msg))
sub = new_mail.get('Subject')
win32ui.MessageBox(str(sub),"Sub")
# new_mail = eml.generateEML(mail)
email.SaveAs()
message_id = str(new_mail.get('message-id'))
except Exception,e:
win32ui.MessageBox(str(e),"Mail Reading Error")
return None
mail_id = execute( conn, 'execute', self._dbname, int(self._uid), self._pwd, 'mailgate.message', 'search', [('name','=',sub)])
mail_id = execute( conn, 'execute', self._dbname, int(self._uid), self._pwd, 'mailgate.message', 'search', [('message_id','=',message_id)])
if not mail_id:
return None
address = execute( conn, 'execute', self._dbname, int(self._uid), self._pwd, 'mailgate.message','read',mail_id[0],['model','res_id'])