~openerp-dev/openerp-outlook-plugin/openerp-outlook-plugin

« back to all changes in this revision

Viewing changes to eml.py

  • Committer: pap(openerp)
  • Date: 2010-04-23 10:01:35 UTC
  • Revision ID: pap@tinyerp.co.in-20100423100135-sweujs3a0wz0hn60
[IMP]:added alert if mail contains attachments

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import sys
2
 
import chilkat
3
 
import os
4
 
from manager import ustr
5
 
 
6
 
def generateEML(mail):
7
 
    sub = (mail.Subject).replace(' ','')
8
 
    body = mail.Body.encode("utf-8")
9
 
    recipients=mail.Recipients
10
 
    sender=mail.SenderEmailAddress
11
 
    attachments=mail.Attachments
12
 
 
13
 
    email = chilkat.CkEmail()
14
 
    email.put_Subject(ustr(sub).encode('iso-8859-1'))
15
 
    email.put_Body(ustr(body).encode('iso-8859-1'))
16
 
    email.put_From(ustr(sender).encode('iso-8859-1'))
17
 
 
18
 
    for i in xrange(1, recipients.Count+1):
19
 
        name = ustr(recipients.Item(i).Name).encode('iso-8859-1')
20
 
        address = ustr(recipients.Item(i).Address).encode('iso-8859-1')
21
 
        email.AddTo(name,address)
22
 
 
23
 
    eml_name= ustr(sub).encode('iso-8859-1')+'-'+str(mail.EntryID)[-9:]
24
 
    ls = ['*', '/', '\\', '<', '>', ':', '?', '"', '|']
25
 
 
26
 
    attachments_folder_path = os.path.abspath(os.path.dirname(__file__)+"\\dialogs\\resources\\attachments\\")
27
 
    if not os.path.exists(attachments_folder_path):
28
 
        os.makedirs(attachments_folder_path)
29
 
    for i in xrange(1, attachments.Count+1):
30
 
        fn = eml_name + '-' + ustr(attachments[i].FileName).encode('iso-8859-1')
31
 
        for c in ls:
32
 
            fn = fn.replace(c,'')
33
 
        if len(fn) > 64:
34
 
            l = 64 - len(fn)
35
 
            f = fn.split('-')
36
 
            fn = '-'.join(f[1:])
37
 
            if len(fn) > 64:
38
 
                l = 64 - len(fn)
39
 
                f = fn.split('.')
40
 
                fn = f[0][0:l] + '.' + f[-1]
41
 
        att_file = os.path.join(attachments_folder_path, fn)
42
 
        if os.path.exists(att_file):
43
 
            os.remove(att_file)
44
 
        f1  = att_file
45
 
        attachments[i].SaveAsFile(att_file)
46
 
        contentType = email.addFileAttachment(att_file)
47
 
        if (contentType == None ):
48
 
            print mail.lastErrorText()
49
 
            sys.exit()
50
 
 
51
 
    mails_folder_path = os.path.abspath(os.path.dirname(__file__)+"\\dialogs\\resources\\mails\\")
52
 
    if not os.path.exists(mails_folder_path):
53
 
        os.makedirs(mails_folder_path)
54
 
    for c in ls:
55
 
        eml_name = eml_name.replace(c,'')
56
 
    if len(eml_name) > 64:
57
 
       l = 64 - len(eml_name)
58
 
       f = eml_name.split('-')
59
 
       eml_name = f[0][0:l] + '.' + f[-1]
60
 
    eml_path = ustr(os.path.join(mails_folder_path,eml_name+".eml")).encode('iso-8859-1')
61
 
    success = email.SaveEml(eml_path)
62
 
    if (success == False):
63
 
        print email.lastErrorText()
64
 
        sys.exit()
65
 
 
66
 
    print "Saved EML!",eml_path
67
 
    return eml_path
 
 
b'\\ No newline at end of file'