3548
by Fabien Pinckaers
Adding Sednacom Zarafa Module |
1 |
#!/usr/bin/env python
|
2 |
#coding: utf-8
|
|
3 |
#
|
|
4 |
# HTML mail module for OpenERP
|
|
5 |
#
|
|
6 |
# (c) 2007-2008 Sednacom <http://www.sednacom.fr>
|
|
7 |
#
|
|
8 |
# authors :
|
|
9 |
# - Brice V. < brice@sednacom.fr >
|
|
10 |
||
11 |
import smtplib |
|
12 |
from email.MIMEText import MIMEText |
|
13 |
from email.MIMEBase import MIMEBase |
|
14 |
from email.MIMEMultipart import MIMEMultipart |
|
15 |
from email.Header import Header |
|
16 |
from email.Utils import formatdate, COMMASPACE |
|
17 |
from email import Encoders |
|
18 |
import mimetypes |
|
19 |
import os |
|
20 |
from tools import config |
|
21 |
import urllib |
|
22 |
import urllib2 |
|
23 |
import base64 |
|
24 |
||
25 |
_ZARAFA_MAIL_URL = 'http://%(zarafa_server)s/webaccess/sednamail.php' |
|
26 |
||
27 |
def zarafa_email_html_send_attach(obj, cr, uid, email_from, email_to, subject, body, |
|
28 |
email_cc=None, email_bcc=None, on_error=False, |
|
29 |
reply_to=False, attach=None, tinycrm=False): |
|
30 |
||
31 |
o_ru = obj.pool.get('res.users') |
|
32 |
zud = o_ru.read(cr, uid, [uid,], ['zarafa_server', 'zarafa_user', 'zarafa_password'])[0] |
|
33 |
||
34 |
zto = '|'.join(email_to) |
|
35 |
data = { |
|
36 |
'user': zud['zarafa_user'] , |
|
37 |
'password': zud['zarafa_password'] , |
|
38 |
'subject' : subject , |
|
39 |
'body' : body , |
|
40 |
'recipients' : zto , |
|
41 |
'from_email' : email_from, |
|
42 |
}
|
|
43 |
if attach : |
|
44 |
zattach = '|'.join([ '%s|%s' % (n, base64.b64encode(c)) for n,c in attach ]) |
|
45 |
data['attachments'] = zattach |
|
46 |
||
47 |
postvars = urllib.urlencode(data) |
|
48 |
zurl = _ZARAFA_MAIL_URL % zud |
|
49 |
||
50 |
ures = urllib2.urlopen(zurl, postvars) |
|
51 |
||
52 |
return True |
|
53 |
||
54 |
def email_html_send_attach(email_from, email_to, subject, body, email_cc=None, email_bcc=None, on_error=False, reply_to=False, attach=None, tinycrm=False): |
|
55 |
if not email_cc: |
|
56 |
email_cc=[] |
|
57 |
if not email_bcc: |
|
58 |
email_bcc=[] |
|
59 |
if not attach: |
|
60 |
attach=[] |
|
61 |
||
62 |
msg = MIMEMultipart('related') |
|
63 |
||
64 |
msg['Subject'] = Header(subject.decode('utf8'), 'utf-8') |
|
65 |
msg['From'] = email_from |
|
66 |
del msg['Reply-To'] |
|
67 |
if reply_to: |
|
68 |
msg['Reply-To'] = reply_to |
|
69 |
msg['To'] = COMMASPACE.join(email_to) |
|
70 |
if email_cc: |
|
71 |
msg['Cc'] = COMMASPACE.join(email_cc) |
|
72 |
if email_bcc: |
|
73 |
msg['Bcc'] = COMMASPACE.join(email_bcc) |
|
74 |
if tinycrm: |
|
75 |
msg['Message-Id'] = '<'+str(time.time())+'-tinycrm-'+str(tinycrm)+'@'+socket.gethostname()+'>' |
|
76 |
msg['Date'] = formatdate(localtime=True) |
|
77 |
||
78 |
msg_alt = MIMEMultipart('alternative') |
|
79 |
||
80 |
msg_alt.attach(MIMEText("Vous devez activez l'affichage en mode HTML pour lire ce message.")) |
|
81 |
||
82 |
body_part = MIMEText(body , _subtype='html') |
|
83 |
body_part.set_charset('UTF-8') |
|
84 |
||
85 |
msg_alt.attach(body_part) |
|
86 |
||
87 |
msg.attach(msg_alt) |
|
88 |
||
89 |
for (fname,fcontent) in attach: |
|
90 |
part = MIMEBase('application', "octet-stream") |
|
91 |
part.set_payload( fcontent ) |
|
92 |
Encoders.encode_base64(part) |
|
93 |
part.add_header('Content-Disposition', 'attachment; filename="%s"' % (fname,)) |
|
94 |
msg.attach(part) |
|
95 |
try: |
|
96 |
s = smtplib.SMTP() |
|
97 |
s.connect(config['smtp_server']) |
|
98 |
if config['smtp_user'] or config['smtp_password']: |
|
99 |
s.login(config['smtp_user'], config['smtp_password']) |
|
100 |
s.sendmail(email_from, email_to + email_cc + email_bcc, msg.as_string()) |
|
101 |
s.quit() |
|
102 |
except Exception, e: |
|
103 |
import logging |
|
104 |
logging.getLogger().info(str(e)) |
|
105 |
return True |