Пример #1
0
    def test_format_mail(self):
        self.set_option('sender-addr', '*****@*****.**')
        self.set_option('sender-name', 'BimBam')

        mail = format_mail({
            'name': 'oim',
            'email': '*****@*****.**'
        }, ['*****@*****.**'],
                           u'un petit cöucou',
                           u'bïjour',
                           config=self.config)
        result = mail.as_string()
        result = re.sub('^Date: .*$', 'Date: now', result, flags=re.MULTILINE)
        self.assertMultiLineEqual(
            result, """\
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
Subject: =?utf-8?q?b=C3=AFjour?=
From: =?utf-8?q?oim?= <*****@*****.**>
Reply-to: =?utf-8?q?oim?= <*****@*****.**>, =?utf-8?q?BimBam?= <*****@*****.**>
X-CW: data
To: [email protected]
Date: now

dW4gcGV0aXQgY8O2dWNvdQ==
""")
        msg = message_from_string(mail.as_string())
        self.assertEqual(msg.get('subject'), u'bïjour')
        self.assertEqual(msg.get('from'), u'oim <*****@*****.**>')
        self.assertEqual(msg.get('to'), u'*****@*****.**')
        self.assertEqual(msg.get('reply-to'),
                         u'oim <*****@*****.**>, BimBam <*****@*****.**>')
        self.assertEqual(msg.get_payload(decode=True), u'un petit cöucou')
Пример #2
0
    def test_format_mail_euro(self):
        mail = format_mail({
            'name': u'oîm',
            'email': u'*****@*****.**'
        }, ['*****@*****.**'], u'un petit cöucou €', u'bïjour €')
        result = mail.as_string()
        result = re.sub('^Date: .*$', 'Date: now', result, flags=re.MULTILINE)
        self.assertMultiLineEqual(
            result, """\
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
Subject: =?utf-8?b?YsOvam91ciDigqw=?=
From: =?utf-8?q?o=C3=AEm?= <*****@*****.**>
Reply-to: =?utf-8?q?o=C3=AEm?= <*****@*****.**>
To: [email protected]
Date: now

dW4gcGV0aXQgY8O2dWNvdSDigqw=
""")
        msg = message_from_string(mail.as_string())
        self.assertEqual(msg.get('subject'), u'bïjour €')
        self.assertEqual(msg.get('from'), u'oîm <*****@*****.**>')
        self.assertEqual(msg.get('to'), u'*****@*****.**')
        self.assertEqual(msg.get('reply-to'), u'oîm <*****@*****.**>')
        self.assertEqual(msg.get_payload(decode=True), u'un petit cöucou €')
Пример #3
0
 def sendmail(self, recipient, subject, body):
     senderemail = self._cw.user.cw_adapt_to('IEmailable').get_email()
     msg = format_mail(
         {
             'email': senderemail,
             'name': self._cw.user.dc_title(),
         }, [recipient], body, subject)
     if not self._cw.vreg.config.sendmails([(msg, [recipient])]):
         msg = self._cw._('could not connect to the SMTP server')
         url = self._cw.build_url(__message=msg)
         raise Redirect(url)
Пример #4
0
 def _prepare_email(self):
     cnx = self.cnx
     config = cnx.vreg.config
     uinfo = {'email': config['sender-addr'], 'name': config['sender-name']}
     view = self._get_view()
     content = view.render(
         changes=cnx.transaction_data.get('pendingchanges'))
     recipients = view.recipients()
     msg = format_mail(uinfo,
                       recipients,
                       content,
                       view.subject(),
                       config=config)
     self.to_send = [(msg, recipients)]
Пример #5
0
    def build_email(self, recipient, data):
        """ Create the confimration email content.
        """
        # Build url before modifying data
        activationurl = self.activation_url(data)

        # Add base url and action url
        data.update({"base_url": self._cw.base_url(secure=True),
                     "url": activationurl})

        # Format mail content
        content = self._cw._(self.content) % data
        subject = self._cw._(self.subject) % data
        return mail.format_mail({}, [recipient], content=content,
                                subject=subject, config=self._cw.vreg.config)
Пример #6
0
    def build_email(self, recipient, data):
        """ Create the confimration email content.
        """
        # Build url before modifying data
        activationurl = self.activation_url(data)

        # Add base url and action url
        data.update({
            "base_url": self._cw.base_url(secure=True),
            "url": activationurl
        })

        # Format mail content
        content = self._cw._(self.content) % data
        subject = self._cw._(self.subject) % data
        return mail.format_mail({}, [recipient],
                                content=content,
                                subject=subject,
                                config=self._cw.vreg.config)
Пример #7
0
 def render_emails(self, **kwargs):
     """generate and send emails for this view (one per recipient)"""
     self._kwargs = kwargs
     recipients = self.recipients()
     if not recipients:
         self.info('skipping %s notification, no recipients',
                   self.__regid__)
         return
     if self.cw_rset is not None:
         entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col
                                          or 0)
         # if the view is using timestamp in message ids, no way to reference
         # previous email
         if not self.msgid_timestamp:
             refs = [
                 self.construct_message_id(eid)
                 for eid in entity.cw_adapt_to(
                     'INotifiable').notification_references(self)
             ]
         else:
             refs = ()
         msgid = self.construct_message_id(entity.eid)
     else:
         refs = ()
         msgid = None
     req = self._cw
     self.user_data = req.user_data()
     for something in recipients:
         if isinstance(something, tuple):
             emailaddr, lang = something
             user = InternalManager(lang=lang)
         else:
             emailaddr = something.cw_adapt_to('IEmailable').get_email()
             user = something
         # hi-jack self._cw to get a session for the returned user
         with Connection(self._cw.repo, user) as cnx:
             self._cw = cnx
             try:
                 # since the same view (eg self) may be called multiple time and we
                 # need a fresh stream at each iteration, reset it explicitly
                 self._w = None
                 try:
                     # XXX forcing the row & col here may make the content and
                     #     subject inconsistent because subject will depend on
                     #     self.cw_row & self.cw_col if they are set.
                     content = self.render(row=0, col=0, **kwargs)
                     subject = self.subject()
                 except SkipEmail:
                     continue
                 except Exception as ex:
                     # shouldn't make the whole transaction fail because of rendering
                     # error (unauthorized or such) XXX check it doesn't actually
                     # occurs due to rollback on such error
                     self.exception(str(ex))
                     continue
                 msg = format_mail(self.user_data, [emailaddr],
                                   content,
                                   subject,
                                   config=self._cw.vreg.config,
                                   msgid=msgid,
                                   references=refs)
                 yield msg, [emailaddr]
             finally:
                 self._cw = req
Пример #8
0
 def test_format_mail_from_reply_to(self):
     # no sender-name, sender-addr in the configuration
     self.set_option('sender-name', '')
     self.set_option('sender-addr', '')
     msg = format_mail({
         'name': u'',
         'email': u''
     }, ['*****@*****.**'],
                       u'un petit cöucou €',
                       u'bïjour €',
                       config=self.config)
     self.assertEqual(msg.get('from'), u'')
     self.assertEqual(msg.get('reply-to'), None)
     msg = format_mail({
         'name': u'tutu',
         'email': u'*****@*****.**'
     }, ['*****@*****.**'],
                       u'un petit cöucou €',
                       u'bïjour €',
                       config=self.config)
     msg = message_from_string(msg.as_string())
     self.assertEqual(msg.get('from'), u'tutu <*****@*****.**>')
     self.assertEqual(msg.get('reply-to'), u'tutu <*****@*****.**>')
     msg = format_mail({
         'name': u'tutu',
         'email': u'*****@*****.**'
     }, ['*****@*****.**'], u'un petit cöucou €', u'bïjour €')
     msg = message_from_string(msg.as_string())
     self.assertEqual(msg.get('from'), u'tutu <*****@*****.**>')
     self.assertEqual(msg.get('reply-to'), u'tutu <*****@*****.**>')
     # set sender name and address as expected
     self.set_option('sender-name', 'cubicweb-test')
     self.set_option('sender-addr', '*****@*****.**')
     # anonymous notification: no name and no email specified
     msg = format_mail({
         'name': u'',
         'email': u''
     }, ['*****@*****.**'],
                       u'un petit cöucou €',
                       u'bïjour €',
                       config=self.config)
     msg = message_from_string(msg.as_string())
     self.assertEqual(msg.get('from'),
                      u'cubicweb-test <*****@*****.**>')
     self.assertEqual(msg.get('reply-to'),
                      u'cubicweb-test <*****@*****.**>')
     # anonymous notification: only email specified
     msg = format_mail({'email': u'*****@*****.**'}, ['*****@*****.**'],
                       u'un petit cöucou €',
                       u'bïjour €',
                       config=self.config)
     msg = message_from_string(msg.as_string())
     self.assertEqual(msg.get('from'), u'cubicweb-test <*****@*****.**>')
     self.assertEqual(
         msg.get('reply-to'),
         u'cubicweb-test <*****@*****.**>, cubicweb-test <*****@*****.**>'
     )
     # anonymous notification: only name specified
     msg = format_mail({'name': u'tutu'}, ['*****@*****.**'],
                       u'un petit cöucou €',
                       u'bïjour €',
                       config=self.config)
     msg = message_from_string(msg.as_string())
     self.assertEqual(msg.get('from'), u'tutu <*****@*****.**>')
     self.assertEqual(msg.get('reply-to'),
                      u'tutu <*****@*****.**>')