Пример #1
0
    def _create_email(data, patron, library, recipients):
        """."""
        from flask_babelex import Locale

        from .api import get_template_to_use
        from ..loans.api import Loan

        language = patron['patron']['communication_language']
        # set the current language for translations in template
        with current_app.test_request_context() as ctx:
            ctx.babel_locale = Locale.parse(language)
            loan = Loan.get_record_by_pid(data['loan']['pid'])
            tpl_path = get_template_to_use(loan, data).rstrip('/')
            template = f'{tpl_path}/{language}.txt'
            # get the sender email from
            # loan.pickup_location_pid.location.library.email
            sender = library['email']
            msg = TemplatedMessage(template_body=template,
                                   sender=sender,
                                   recipients=recipients,
                                   ctx=data['loan'])
            text = msg.body.split('\n')
            # subject is the first line
            msg.subject = text[0]
            # body
            msg.body = '\n'.join(text[1:])
            return msg
Пример #2
0
 def send_mail(self, data):
     """Send email."""
     notification_type = data.get('notification_type')
     language = data['loan']['patron']['communication_language']
     template = 'email/{type}/{lang}'.format(
         type=notification_type,
         lang=language
     )
     recipient = data['loan']['patron']['email']
     msg = TemplatedMessage(
         # template_html='{template}.html'.format(template=template),
         template_body='{template}.txt'.format(template=template),
         sender=config_value('EMAIL_SENDER'),
         recipients=[recipient],
         ctx=data['loan']
     )
     text = msg.body.split('\n')
     msg.subject = text[0]
     msg.body = '\n'.join(text[1:])
     try:
         send_email.run(msg.__dict__)
         # TODO: investigate why delay does not work
         # send_email.delay(msg.__dict__)
         # current_app.extensions['mail'].send(msg)
     except Exception as e:
         raise(e)
Пример #3
0
 def send_mail(data):
     """Send the notification by email."""
     notification_type = data.get('notification_type')
     language = data['loan']['patron']['communication_language']
     template = 'email/{type}/{lang}.txt'.format(
         type=notification_type,
         lang=language
     )
     # get the recipient email from loan.patron.email
     recipient = data['loan']['patron']['email']
     # get the sender email from
     # loan.pickup_location_pid.location.library.email
     library = Location.get_record_by_pid(
         data['loan']['pickup_location_pid']).get_library()
     sender = library['email']
     msg = TemplatedMessage(
         template_body=template,
         sender=sender,
         recipients=[recipient],
         ctx=data['loan']
     )
     text = msg.body.split('\n')
     msg.subject = text[0]
     msg.body = '\n'.join(text[1:])
     task_send_email.run(msg.__dict__)
Пример #4
0
    def _create_email(recipients, reply_to, ctx_data, template):
        """Create email message from template.

        :param recipients: List of emails to send the message too.
        :param reply_to: Reply to email address.
        :param ctx_data: Dictionary with informations used in template.
        :param template: Template to use to create TemplatedMessage.
        :returns: Message created.
        """
        msg = TemplatedMessage(
            template_body=template,
            sender=current_app.config.get('DEFAULT_SENDER_EMAIL',
                                          '*****@*****.**'),
            reply_to=','.join(reply_to),  # the client is unable to manage list
            recipients=recipients,
            ctx=ctx_data)
        # subject is the first line, body is the rest
        text = msg.body.split('\n')
        msg.subject = text[0]
        msg.body = '\n'.join(text[1:])
        return msg
Пример #5
0
def send_notification_to_location(loan, item, location):
    """Send a notification to the location defined email.

    :param loan: the loan to be parsed
    :param item: the requested item
    :param location: the location to inform
    """
    if not location.get('send_notification', False) \
            or not location.get('notification_email'):
        return
    template = 'email/others/location_notification.txt'
    recipient = location.get('notification_email')
    msg = TemplatedMessage(template_body=template,
                           sender=config_value('EMAIL_SENDER'),
                           recipients=[recipient],
                           ctx=_build_notification_email_context(
                               loan, item, location))
    text = msg.body.split('\n')
    msg.subject = text[0]
    msg.body = '\n'.join(text[1:])
    send_email.run(msg.__dict__)
Пример #6
0
    def send_mail(data):
        """Send the notification by email."""
        patron = data['loan']['patron']
        # get the recipient email from loan.patron.patron.email
        recipient = patron.get('email')
        # do nothing if the patron does not have an email
        if not recipient:
            current_app.logger.warning(
                'Patron (pid: {pid}) does not have an email'.format(
                    pid=patron['pid']))
            return
        notification_type = data.get('notification_type')
        language = patron['patron']['communication_language']
        template = 'email/{type}/{lang}.txt'.format(
            type=notification_type,
            lang=language
        )

        # get the sender email from
        # loan.pickup_location_pid.location.library.email
        library = Location.get_record_by_pid(
            data['loan']['pickup_location_pid']).get_library()
        sender = library['email']
        msg = TemplatedMessage(
            template_body=template,
            sender=sender,
            recipients=[recipient],
            ctx=data['loan']
        )
        # additional recipient
        add_recipient = patron['patron'].get('additional_communication_email')
        if add_recipient:
            msg.add_recipient(add_recipient)
        text = msg.body.split('\n')
        msg.subject = text[0]
        msg.body = '\n'.join(text[1:])
        task_send_email.run(msg.__dict__)