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