IHM pour édition des indexes délégués à Solr.
[Plinn.git] / skins / control / send_email.py
1 ##parameters= other_adr=[], batchM=[], customized_batch={}, expand='', collapse='', subject='', text_body='', wfid=None, send_fields={}, ajax=''
2 recipients = {'to':[], 'cc':[], 'bcc':[]}
3 if expand or collapse :
4 if expand :
5 expand = expand.keys()[0]
6 return context.send_email_form(other_adr=other_adr, expand=expand,
7 batchM=batchM, customized_batch=customized_batch,
8 subject=subject, text_body=text_body,
9 send_fields=send_fields)
10 if batchM :
11 rolesAndMembers = dict(context.getRecipients())
12 for role in batchM :
13 field = send_fields[role]
14 recipients[field].extend( [m.id for m in rolesAndMembers[role]] )
15
16 for role, b in customized_batch.items() :
17 field = send_fields[role]
18 recipients[field].extend( b )
19
20
21 other_adr = filter(None, other_adr)
22 if not (reduce(lambda a, b : a+b, recipients.values()) or other_adr) :
23 context.setStatus(False, 'No email sent : no recipient specified.')
24 return context.setRedirect(context, 'object/view')
25
26 portal = context.portal_url.getPortalObject()
27 mtool = portal.portal_membership
28 MailHost = portal.MailHost
29
30 from quopri import encodestring
31
32 def encodeAdr(member) :
33 name = member.getMemberFullName(nameBefore=0)
34 email = member.getProperty('email')
35 qpName = encodestring(name).replace('=\n', '')
36 return '''"=?utf-8?q?%s?=" <%s>''' % (qpName, email)
37
38 sender = mtool.getAuthenticatedMember()
39 sender = encodeAdr(sender)
40
41
42 recipientsFormated = {'to':'', 'cc':'', 'bcc':''}
43 for field, b in recipients.items() :
44 formated = map(encodeAdr, mtool.getMembers(b))
45 formated = filter(None, formated)
46 formated = ', '.join(formated)
47 recipientsFormated[field] = formated
48
49 if other_adr :
50 recipients['to'].extend(other_adr)
51 formated = ', '.join(other_adr)
52 to = ', '.join([recipientsFormated['to'], formated])
53 to = to.strip(', ')
54 recipientsFormated['to'] = to
55
56 recipientsHeader = []
57 for field in ['to', 'cc', 'bcc'] :
58 value = recipientsFormated[field]
59 if value :
60 recipientsHeader.append('%s: %s' % (field.capitalize(), value))
61
62 recipientsHeader = '\n'.join(recipientsHeader)
63
64 message = context.echange_mail_template( From = sender
65 , recipients = recipientsHeader
66 , Subject = "=?utf-8?q?%s?=" % encodestring(subject).replace('=\n', '')
67 , ContentType = 'text/plain'
68 , charset = 'UTF-8'
69 , body=text_body)
70
71 MailHost.send( message.encode('utf-8') )
72
73 if wfid is not None :
74 wtool = portal.portal_workflow
75 email_sent = reduce(lambda a, b : a+b, recipients.values())
76 wtool.doActionFor(context, 'send_email', wf_id=wfid,
77 email_sent=email_sent,
78 comment=text_body)
79
80 context.setStatus(True, 'Email sent.')
81 return context.setRedirect(context, 'object/view', ajax=ajax)