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
:
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
)
11 rolesAndMembers
= dict(context
.getRecipients())
13 field
= send_fields
[role
]
14 recipients
[field
].extend( [m
.id for m
in rolesAndMembers
[role
]] )
16 for role
, b
in customized_batch
.items() :
17 field
= send_fields
[role
]
18 recipients
[field
].extend( b
)
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')
26 portal
= context
.portal_url
.getPortalObject()
27 mtool
= portal
.portal_membership
28 MailHost
= portal
.MailHost
30 from quopri
import encodestring
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
)
38 sender
= mtool
.getAuthenticatedMember()
39 sender
= encodeAdr(sender
)
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
50 recipients
['to'].extend(other_adr
)
51 formated
= ', '.join(other_adr
)
52 to
= ', '.join([recipientsFormated
['to'], formated
])
54 recipientsFormated
['to'] = to
57 for field
in ['to', 'cc', 'bcc'] :
58 value
= recipientsFormated
[field
]
60 recipientsHeader
.append('%s: %s' % (field
.capitalize(), value
))
62 recipientsHeader
= '\n'.join(recipientsHeader
)
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'
71 MailHost
.send( message
.encode('utf-8') )
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
,
80 context
.setStatus(True, 'Email sent.')
81 return context
.setRedirect(context
, 'object/view', ajax
=ajax
)