コード例 #1
0
def send_mail(subject, recipients, template, language, **context):
    """Send an email via the Flask-Mail extension.

    :param subject: Email subject
    :param recipients: List of email recipients
    :param template: The name of the email template
    :param context: The context to render the template with
    """
    sender = config_value('EMAIL_SENDER')
    msg = Message(subject, sender=sender, recipients=recipients)

    if config_value('EMAIL_PLAINTEXT'):
        msg.body = render_template(
            'email/{template}_{language}.txt'.format(template=template,
                                                     language=language),
            **context)
        # msg.html = render_template(
        #     'email/{template}_{language}.html'.format(
        #         template=template,
        #         language=language
        #     ),
        #     **context
        # )

    _send_mail.delay(msg.__dict__)
コード例 #2
0
def robotupload_callback():
    """Handle callback from robotupload.

    If robotupload was successful caches the workflow
    object id that corresponds to the uploaded record,
    so the workflow can be resumed when webcoll finish
    processing that record.
    If robotupload encountered an error sends an email
    to site administrator informing him about the error."""
    request_data = request.get_json()
    id_object = request_data.get("nonce", "")
    results = request_data.get("results", [])
    status = False
    for result in results:
        status = result.get('success', False)
        if status:
            recid = result.get('recid')
            pending_records = cache.get("pending_records") or dict()
            pending_records[str(recid)] = str(id_object)
            cache.set("pending_records", pending_records,
                      timeout=current_app.config["PENDING_RECORDS_CACHE_TIMEOUT"])
        else:
            from invenio_mail.tasks import send_email

            body = ("There was an error when uploading the "
                    "submission with id: %s.\n" % id_object)
            body += "Error message:\n"
            body += result.get('error_message', '')
            send_email.delay(dict(
                sender=current_app.config["CFG_SITE_SUPPORT_EMAIL"],
                receipient=current_app.config["CFG_SITE_ADMIN_EMAIL"],
                subject='BATCHUPLOAD ERROR',
                body=body
            ))
    return jsonify({"result": status})
コード例 #3
0
def _send_notification(to, subject, template, **ctx):
    """Render a template and send as email."""
    msg = Message(subject,
                  sender=current_app.config.get('SUPPORT_EMAIL'),
                  recipients=[to])
    msg.body = render_template(template, **ctx)

    send_email.delay(msg.__dict__)
コード例 #4
0
def send_spam_user_email(recipient):
    """Send email notification to blocked user after spam detection."""
    msg = Message(
        _("Your Zenodo Upload has been automatically marked as Spam."),
        sender=current_app.config.get('SUPPORT_EMAIL'),
        recipients=[recipient],
    )
    msg.body = render_template("zenodo_spam/email/spam_user_email.tpl")
    send_email.delay(msg.__dict__)
コード例 #5
0
def _send_notification(to, subject, template, **ctx):
    """Render a template and send as email."""
    msg = Message(
        subject,
        sender=current_app.config.get('SUPPORT_EMAIL'),
        recipients=[to]
    )
    msg.body = render_template(template, **ctx)

    send_email.delay(msg.__dict__)
コード例 #6
0
def send_spam_admin_email(user, deposit=None, community=None):
    """Send email notification to admins for a spam detection."""
    msg = Message(
        _("Zenodo activity marked as spam."),
        sender=current_app.config.get('SUPPORT_EMAIL'),
        recipients=[current_app.config.get('ZENODO_ADMIN_EMAIL')],
    )
    msg.body = render_template("zenodo_spam/email/spam_admin_email.tpl",
                               user=user,
                               deposit=deposit,
                               community=community)
    send_email.delay(msg.__dict__)
コード例 #7
0
def test_send_message_with_date(email_task_app):
    """Test sending a message with a date."""
    with email_task_app.app_context():
        msg = {
            'subject': 'Test4',
            'sender': '[email protected]',
            'recipients': ['[email protected]'],
            'date': 1456242014.398119
        }

        send_email.delay(msg)

        result_stream = email_task_app.extensions['invenio-mail'].stream
        assert result_stream.getvalue().find('Date: Tue, 23 Feb 2016') != -1
コード例 #8
0
ファイル: dispatcher.py プロジェクト: zannkukai/rero-ils
 def send_mail_for_printing(data):
     """Send the notification by email."""
     patron = data['loan']['patron']
     library = Location.get_record_by_pid(
         data['loan']['pickup_location_pid']).get_library()
     # get the recipient email from the library
     email = library.email_notification_type(data['notification_type'])
     if not email:
         pid = patron['pid']
         current_app.logger.warning(
             f'Notification is lost for patron({pid})')
         return
     msg = Dispatcher._create_email(data, patron, library, [email])
     task_send_email.delay(msg.__dict__)
コード例 #9
0
def test_send_message_with_date(email_task_app):
    """Test sending a message with a date."""
    with email_task_app.app_context():
        msg = {
            'subject': 'Test4',
            'sender': '[email protected]',
            'recipients': ['[email protected]'],
            'date': 1456242014.398119
        }

        send_email.delay(msg)

        result_stream = email_task_app.extensions['invenio-mail'].stream
        assert result_stream.getvalue().find('Date: Tue, 23 Feb 2016') != -1
コード例 #10
0
ファイル: views.py プロジェクト: liamkirsh/inspire-next
def postfeedback():
    """Handler to create a ticket from user feedback."""
    feedback = request.form.get('feedback')
    if feedback == '':
        return jsonify(success=False), 400

    replytoaddr = request.form.get('replytoaddr', None)
    if replytoaddr is None:
        if current_user.is_anonymous:
            return jsonify(success=False), 403
        else:
            replytoaddr = current_user.get('email')
            if replytoaddr == '':
                return jsonify(success=False), 403

    content = 'Feedback:\n{feedback}'.format(feedback=feedback)
    message = {
        'sender': current_app.config['CFG_SITE_SUPPORT_EMAIL'],
        'recipients': [current_app.config['INSPIRELABS_FEEDBACK_EMAIL']],
        'subject': 'INSPIRE Labs Feedback',
        'body': content,
        'reply_to': replytoaddr
    }

    sending = send_email.delay(message)

    if sending.failed():
        return jsonify(success=False), 500
    else:
        return jsonify(success=True)
コード例 #11
0
ファイル: views.py プロジェクト: Kjili/inspire-next
def postfeedback():
    """Handler to create a ticket from user feedback."""
    feedback = request.form.get('feedback')
    if feedback == '':
        return jsonify(success=False), 400

    replytoaddr = request.form.get('replytoaddr', None)
    if replytoaddr is None:
        if current_user.is_anonymous:
            return jsonify(success=False), 403
        else:
            replytoaddr = current_user.get('email')
            if replytoaddr == '':
                return jsonify(success=False), 403

    content = 'Feedback:\n{feedback}'.format(feedback=feedback)
    message = {
        'sender': current_app.config['CFG_SITE_SUPPORT_EMAIL'],
        'recipients': [current_app.config['INSPIRELABS_FEEDBACK_EMAIL']],
        'subject': 'INSPIRE Labs Feedback',
        'body': content,
        'reply_to': replytoaddr
    }

    sending = send_email.delay(message)

    if sending.failed():
        return jsonify(success=False), 500
    else:
        return jsonify(success=True)
コード例 #12
0
def test_send_message_stream(email_task_app):
    """Test sending a message using Task module."""
    with email_task_app.app_context():
        with email_task_app.extensions['mail'].record_messages() as outbox:
            msg = {
                'subject': 'Test2',
                'sender': '[email protected]',
                'recipients': ['[email protected]']
            }

            send_email.delay(msg)

            result_stream = email_task_app.extensions['invenio-mail'].stream
            assert result_stream.getvalue().find(
                'From: [email protected]') != -1
            assert result_stream.getvalue().find('To: [email protected]') != -1
            assert result_stream.getvalue().find('Subject: Test2') != -1
コード例 #13
0
def test_send_message_stream(email_task_app):
    """Test sending a message using Task module."""
    with email_task_app.app_context():
        with email_task_app.extensions['mail'].record_messages() as outbox:
            msg = {
                'subject': 'Test2',
                'sender': '[email protected]',
                'recipients': ['[email protected]']
            }

            send_email.delay(msg)

            result_stream = email_task_app.extensions['invenio-mail'].stream
            assert result_stream.getvalue().find(
                'From: [email protected]') != -1
            assert result_stream.getvalue().find('To: [email protected]') != -1
            assert result_stream.getvalue().find('Subject: Test2') != -1
コード例 #14
0
def test_send_message_outbox(email_task_app):
    """Test sending a message using Task module."""
    with email_task_app.app_context():
        with email_task_app.extensions['mail'].record_messages() as outbox:
            msg = {
                'subject': 'Test1',
                'sender': '[email protected]',
                'recipients': ['[email protected]']
            }

            send_email.delay(msg)

            assert len(outbox) == 1
            sent_msg = outbox[0]
            assert sent_msg.subject == 'Test1'
            assert sent_msg.sender == '[email protected]'
            assert sent_msg.recipients == ['[email protected]']
コード例 #15
0
def test_send_message_outbox(email_task_app):
    """Test sending a message using Task module."""
    with email_task_app.app_context():
        with email_task_app.extensions['mail'].record_messages() as outbox:
            msg = {
                'subject': 'Test1',
                'sender': '[email protected]',
                'recipients': ['[email protected]']
            }

            send_email.delay(msg)

            assert len(outbox) == 1
            sent_msg = outbox[0]
            assert sent_msg.subject == 'Test1'
            assert sent_msg.sender == '[email protected]'
            assert sent_msg.recipients == ['[email protected]']
コード例 #16
0
def send_community_request_email(increq):
    """Signal for sending emails after community inclusion request."""
    from flask_mail import Message
    from invenio_mail.tasks import send_email

    msg_body = format_request_email_body(increq)
    msg_title = format_request_email_title(increq)

    sender = current_app.config['COMMUNITIES_REQUEST_EMAIL_SENDER']

    msg = Message(
        msg_title,
        sender=sender,
        recipients=[increq.community.owner.email, ],
        body=msg_body
    )

    send_email.delay(msg.__dict__)
コード例 #17
0
def send_community_request_email(increq):
    """Signal for sending emails after community inclusion request."""
    from flask_mail import Message
    from invenio_mail.tasks import send_email

    msg_body = format_request_email_body(increq)
    msg_title = format_request_email_title(increq)

    sender = current_app.config['COMMUNITIES_REQUEST_EMAIL_SENDER']

    msg = Message(
        msg_title,
        sender=sender,
        recipients=[increq.community.owner.email, ],
        body=msg_body
    )

    send_email.delay(msg.__dict__)
コード例 #18
0
def send_invitation_email(membership_request, recipients, community):
    """Send email notification for a member invitation."""
    msg = Message(
        _("Community membership invitation"),
        sender=current_app.config.get('SUPPORT_EMAIL'),
        recipients=recipients,
    )
    template = ("invitation_email.tpl" if membership_request.is_invite else
                "member_request_email.tpl")
    msg.body = render_template(
        "invenio_communities/{}".format(template),
        community=community,
        days=timedelta(
            seconds=current_app.
            config["COMMUNITIES_MEMBERSHIP_REQUESTS_CONFIRMLINK_EXPIRES_IN"]).
        days,
        membership_request=membership_request,
        invitation_link=format_url_template(
            # TODO: change to use value from config
            '/communities/members/requests/{req_id}',
            req_id=str(membership_request.id),
        ))
    send_email.delay(msg.__dict__)
コード例 #19
0
def test_send_message_with_attachments(email_task_app):
    """Test sending a message with attachments."""
    with email_task_app.app_context():
        filename = pkg_resources.resource_filename(
            __name__, os.path.join('attachments', 'invenio.svg'))
        content_type = 'image/svg+xml'
        data = pkg_resources.resource_string(
            __name__, os.path.join('attachments', 'invenio.svg'))

        attachments = [Attachment(filename, content_type, data)]
        msg = {
            'subject': 'Test3',
            'sender': '[email protected]',
            'recipients': ['[email protected]'],
            'attachments': attachments
        }

        send_email.delay(msg)

        result_stream = email_task_app.extensions['invenio-mail'].stream
        assert result_stream.getvalue().find(
            'Content-Transfer-Encoding: base64') != -1
        assert result_stream.getvalue().find(
            'Content-Disposition: attachment;') != -1
コード例 #20
0
def test_send_message_with_attachments(email_task_app):
    """Test sending a message with attachments."""
    with email_task_app.app_context():
        filename = pkg_resources.resource_filename(
            __name__, os.path.join('attachments', 'invenio.svg'))
        content_type = 'image/svg+xml'
        data = pkg_resources.resource_string(
            __name__, os.path.join('attachments', 'invenio.svg'))

        attachments = [Attachment(filename, content_type, data)]
        msg = {
            'subject': 'Test3',
            'sender': '[email protected]',
            'recipients': ['[email protected]'],
            'attachments': attachments
        }

        send_email.delay(msg)

        result_stream = email_task_app.extensions['invenio-mail'].stream
        assert result_stream.getvalue().find(
            'Content-Transfer-Encoding: base64') != -1
        assert result_stream.getvalue().find(
            'Content-Disposition: attachment;') != -1