Copie depuis le svn du cri à l'état :
[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 if other_adr :
42 recipients['to'].extend(other_adr)
43
44 recipientsFormated = {'to':'', 'cc':'', 'bcc':''}
45 for field, b in recipients.items() :
46 formated = map(encodeAdr, mtool.getMembers(b))
47 formated = filter(None, formated)
48 formated = ', '.join(formated)
49 recipientsFormated[field] = formated
50
51 recipientsHeader = []
52 for field in ['to', 'cc', 'bcc'] :
53 value = recipientsFormated[field]
54 if value :
55 recipientsHeader.append('%s: %s' % (field.capitalize(), value))
56
57 recipientsHeader = '\n'.join(recipientsHeader)
58
59 message = context.echange_mail_template( From = sender
60 , recipients = recipientsHeader
61 , Subject = "=?utf-8?q?%s?=" % encodestring(subject).replace('=\n', '')
62 , ContentType = 'text/plain'
63 , charset = 'UTF-8'
64 , body=text_body)
65
66 MailHost.send( message.encode('utf-8') )
67
68 if wfid is not None :
69 wtool = portal.portal_workflow
70 email_sent = reduce(lambda a, b : a+b, recipients.values())
71 email_sent.extend(other_adr)
72 wtool.doActionFor(context, 'send_email', wf_id=wfid,
73 email_sent=email_sent,
74 comment=text_body)
75
76 context.setStatus(True, 'Email sent.')
77 return context.setRedirect(context, 'object/view', ajax=ajax)