78 content = None # body of the mail |
78 content = None # body of the mail |
79 message = None # action verb of the subject |
79 message = None # action verb of the subject |
80 |
80 |
81 # this is usually the method to call |
81 # this is usually the method to call |
82 def render_and_send(self, **kwargs): |
82 def render_and_send(self, **kwargs): |
83 """generate and send an email message for this view""" |
83 """generate and send email messages for this view""" |
84 delayed = kwargs.pop('delay_to_commit', None) |
84 self._cw.vreg.config.sendmails(self.render_emails(**kwargs)) |
85 for recipients, msg in self.render_emails(**kwargs): |
|
86 if delayed is None: |
|
87 self.send(recipients, msg) |
|
88 elif delayed: |
|
89 self.send_on_commit(recipients, msg) |
|
90 else: |
|
91 self.send_now(recipients, msg) |
|
92 |
85 |
93 def cell_call(self, row, col=0, **kwargs): |
86 def cell_call(self, row, col=0, **kwargs): |
94 self.w(self._cw._(self.content) % self.context(**kwargs)) |
87 self.w(self._cw._(self.content) % self.context(**kwargs)) |
95 |
88 |
96 def render_emails(self, **kwargs): |
89 def render_emails(self, **kwargs): |
144 # occurs due to rollback on such error |
137 # occurs due to rollback on such error |
145 self.exception(str(ex)) |
138 self.exception(str(ex)) |
146 continue |
139 continue |
147 msg = format_mail(self.user_data, [emailaddr], content, subject, |
140 msg = format_mail(self.user_data, [emailaddr], content, subject, |
148 config=self._cw.vreg.config, msgid=msgid, references=refs) |
141 config=self._cw.vreg.config, msgid=msgid, references=refs) |
149 yield [emailaddr], msg |
142 yield msg, [emailaddr] |
150 finally: |
143 finally: |
151 # ensure we have a cnxset since commit will fail if there is |
|
152 # some operation but no cnxset. This may occurs in this very |
|
153 # specific case (eg SendMailOp) |
|
154 with cnx.ensure_cnx_set: |
|
155 cnx.commit() |
|
156 self._cw = req |
144 self._cw = req |
157 |
145 |
158 # recipients / email sending ############################################### |
146 # recipients handling ###################################################### |
159 |
147 |
160 def recipients(self): |
148 def recipients(self): |
161 """return a list of either 2-uple (email, language) or user entity to |
149 """return a list of either 2-uple (email, language) or user entity to |
162 whom this email should be sent |
150 whom this email should be sent |
163 """ |
151 """ |
164 finder = self._cw.vreg['components'].select( |
152 finder = self._cw.vreg['components'].select( |
165 'recipients_finder', self._cw, rset=self.cw_rset, |
153 'recipients_finder', self._cw, rset=self.cw_rset, |
166 row=self.cw_row or 0, col=self.cw_col or 0) |
154 row=self.cw_row or 0, col=self.cw_col or 0) |
167 return finder.recipients() |
155 return finder.recipients() |
168 |
|
169 def send_now(self, recipients, msg): |
|
170 self._cw.vreg.config.sendmails([(msg, recipients)]) |
|
171 |
|
172 def send_on_commit(self, recipients, msg): |
|
173 SendMailOp(self._cw, recipients=recipients, msg=msg) |
|
174 send = send_on_commit |
|
175 |
156 |
176 # email generation helpers ################################################# |
157 # email generation helpers ################################################# |
177 |
158 |
178 def construct_message_id(self, eid): |
159 def construct_message_id(self, eid): |
179 return construct_message_id(self._cw.vreg.config.appid, eid, |
160 return construct_message_id(self._cw.vreg.config.appid, eid, |