Exemplo n.º 1
0
def get_notification_setting(login_group, server):
    """Get the notification setting for the login group.
    Return True if the group should get the notification.

    :param login_group: the login group name as a string
    :param server: a TacticServerStub object
    :return: boolean if group should get notification or not
    """
    notification_settings = ctu.get_project_setting('department_work_order_notification', server=server)
    department_setting = notification_settings.get(login_group, 'False')

    return department_setting == 'True'
Exemplo n.º 2
0
def send_email(template=DEFAULT_TEMPLATE, email_data=None, email_file_name='temp_email.html',
               attachments=None, server=None):
    """Formats an email template, saves the file as html, and sends it.
    The data should contain: to_email, from_email, subject, and message

    Also expects ccs to be a semicolon separated string

    :param template: a path to a template file
    :param email_data: dictionary of email data
    :param email_file_name: file name for temp html file
        Note: the file name may contain subdirectories,
        all email html files will be in /var/www/html/formatted_emails/
    :param attachments: a list of file paths to attach to the email
    :param server: a TacticServerStub object used to get any misc data
    :return:
    """
    if not email_data:
        email_data = {}
    if not attachments:
        attachments = []

    if not os.path.exists(template):
        raise Exception('Email template not found: {0}'.format(template))

    # fill in default email data, fix message characters
    email_data['to_email'] = email_data.get('to_email', '*****@*****.**')
    email_data['from_email'] = email_data.get('from_email', '*****@*****.**')
    default_name = email_data['from_email'].split('@')[0].replace('.', ' ').title()
    email_data['from_name'] = email_data.get('from_name', default_name)
    email_data['subject'] = email_data.get('subject', 'No Subject')
    email_data['message'] = ctu.fix_message_characters(email_data.get('message', 'No message'))

    if server:
        # This will be None by default
        override = ctu.get_project_setting('override_to_email', server=server)
        if override == 'True':
            email_data['to_email'] = '*****@*****.**'
            email_data['ccs'] = ''

    with open(template, 'r') as f:
        email_html = f.read()
    formatted_email = safe_format_string(email_html, email_data)
    for attachment in attachments:
        # This attachment keyword has to be something that won't likely be typed by a user
        formatted_email += '\n@ACHM3NT:{0}'.format(attachment)
    email_file_path = write_email_file(formatted_email, email_file_name)

    args = [SEND_EMAIL_COMMAND, email_file_path, email_data['to_email'], email_data['from_email'],
            email_data['from_name'], email_data['subject'], email_data.get('ccs', '').replace(';', '#Xs*')]
    command = "php {0} '''{1}''' '''{2}''' '''{3}''' '''{4}''' '''{5}''' '''{6}'''".format(*args)
    print command
    os.system(command)