def post(self, received_json):
        try:
            email_details = received_json.get('email')
            if email_details:
                email.send(
                    email_details.get('sender') or get_setup().email_credentials.email_sender,
                    email_details['receivers'],
                    email_details['subject'],
                    email_details['text'],
                    email_details['html'],
                    email_details['pdfs'],
                    email_details['data'],
                )

            sms_details = received_json.get('sms')
            if sms_details:
                sms.send(
                    sms_details.get('sender') or get_setup().sms_credentials.phone_number,
                    sms_details['receiver'],
                    sms_details['body'],
                    sms_details['data'],
                )
        except OwnException as e:
            return self.send_failed(e.message)

        return self.send_success({})
Exemple #2
0
def __send_to_ses(
    sender: str,
    receivers: List[str],
    subject: str,
    txt: str,
    html: str,
    attachments: List[PdfFile],
) -> None:
    if get_setup().is_debug:
        logging.info(
            'Not sending real emails in debug mode (activate with DEBUG=true environment variable)'
        )
        logging.info('sender:       %s', sender)
        logging.info('receivers:    %s', receivers)
        logging.info('subject:      %s', subject)
        logging.info('txt:          %s', txt)
        logging.info('html:         %s', html)
        logging.info('attachments:  %s',
                     "\n".join([str(a) for a in attachments]))
        logging.info('done logging')
        return

    logging.info(
        f'Sending email with subject {subject!r} to {receivers!r} from {sender!r}'
    )
    msg = __build_msg_html(sender, receivers, subject, txt, html, attachments)
    get_ses_client().send_raw_email(
        Source=sender,
        Destinations=
        receivers,  # here it has to be a list, even if it is only one recipient
        RawMessage={
            'Data': msg.as_string(
            )  # this generates all the headers and stuff for a raw mail message
        })
Exemple #3
0
def get_data_for_s3_post(bucket_name=None,
                         bucket_region=None,
                         upload_file_name=None,
                         upload_file_type="",
                         public=False,
                         max_file_size=TEN_MB,
                         post_expire_in=3600):
    """

    @param bucket_name: bucket name
    @param upload_file_name: the file path where to upload, eg: upload_folder/file_name.txt, or file_name.jpg; if no
    file name is provided, it will use by default uuid.uuid4()
    @param upload_file_type: type of the to-be-uploaded file, eg: image/, application/pdf
    @param public: visibility of file on S3
    (more here http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUTacl.html)
    @param max_file_size: the max file size that S3 should accept (in bytes)
    @param post_expire_in: the number of seconds the presigned post is valid for

    @return: a dict containing the information needed for the client to do the POST request
    """
    if upload_file_name is None or upload_file_name == '':
        # raise UploadFileException(EMPTY_FILE_NAME)
        upload_file_name = str(uuid.uuid4())

    bucket_name = __get_bucket_name(bucket_name)
    bucket_region = bucket_region or get_setup().s3_credentials.aws_region
    if not bucket_region:
        raise OwnException(MISSING_BUCKET_REGION)
    key = upload_file_name
    s3 = boto3.client(
        's3',
        region_name=bucket_region,
        config=Config(signature_version='s3v4'),
    )
    # conn = boto.connect_s3()
    # conn.build_post_policy(time() + 1000, {})
    policy = 'public-read' if public else 'private'
    post_data = s3.generate_presigned_post(
        Bucket=bucket_name,
        Key=key,
        Fields={'acl': policy},
        Conditions=[
            {
                'acl': policy
            },
            ['content-length-range', 0, max_file_size],
            ["starts-with", "$Content-Type", upload_file_type or 'image/'],
        ],
        ExpiresIn=post_expire_in)

    fields = post_data.get('fields')
    fields['bucket'] = bucket_name
    fields['Content-Type'] = upload_file_type or 'image/jpeg'

    return fields
    def post(self, received_json):
        try:
            sms.send(
                received_json.get('sender')
                or get_setup().sms_credentials.phone_number,
                received_json['receiver'],
                received_json['body'],
                received_json['data'],
            )
        except OwnException as e:
            return self.send_failed(e.message)

        return self.send_success({})
Exemple #5
0
def send(sender: str, receiver: str, body_template: str, template_data: dict):
    body = build_template(body_template, template_data)
    if get_setup().is_debug:
        logging.info(
            'Not sending real sms in debug mode (activate with DEBUG=true environment variable)'
        )
        logging.info('from:     %s', sender)
        logging.info('receiver: %s', receiver)
        logging.info('body:     %s', body)
        logging.info('done logging')
        return
    if not __twilio_singleton:
        raise OwnException(TWILIO_NOT_SETUP)
    __twilio_singleton.send_message(body, receiver, sender)
    def post(self, received_json):
        try:
            email.send(
                received_json.get('sender')
                or get_setup().email_credentials.email_sender,
                received_json['receivers'],
                received_json['subject'],
                received_json['text'],
                received_json['html'],
                received_json['pdfs'],
                received_json['data'],
            )
        except OwnException as e:
            return self.send_failed(e.message)

        return self.send_success({})
Exemple #7
0
def __get_bucket_name(bucket_name=None):
    bucket_name = bucket_name or get_setup().s3_credentials.default_bucket
    if not bucket_name:
        raise OwnException(MISSING_BUCKET_NAME)
    return bucket_name
Exemple #8
0
debug = os.environ.get('DEBUG') == "true"
production = os.environ.get('PRODUCTION') == "true"
port = os.environ.get('PORT')

if production:
    print('running in production mode')

    if port == "":
        logging.info(
            'Port is missing. Use the "PORT" environment variable to set it')
        sys.exit(-1)
    try:
        port = int(port)
    except:
        logging.info(
            'Port is not int. Use the "PORT" environment variable to set it')
        sys.exit(-1)

    set_setup(False)
    set_sms(get_setup().sms_credentials.sid, get_setup().sms_credentials.token)
    app = configure_app(False)
else:
    print('running in debug mode')
    set_setup(debug)
    if not debug:
        set_sms(get_setup().sms_credentials.sid,
                get_setup().sms_credentials.token)
    app = configure_app(debug)
    app.run(host="0.0.0.0", debug=True, port=8006, threaded=True)
Exemple #9
0
def get_ses_client():
    global SES_CLIENT
    if SES_CLIENT is None:
        SES_CLIENT = boto3.client(
            'ses', region_name=get_setup().email_credentials.aws_region)
    return SES_CLIENT