def _send_mail(mail_to, mail_from, subject, body, logger, mail_cc=None, debug_enabled=False): if debug_enabled: logger.debug("Sending mail to %s. Subject: %s", mail_to, subject) # logger.debug("Body: %s" % body) return True try: Utils.sendmail( toaddr=mail_to, fromaddr=mail_from, subject=subject, body=body, cc=mail_cc, debug=debug_enabled) except smtplib.SMTPRecipientsRefused as e: failed_recipients = e.recipients for mail, condition in failed_recipients.iteritems(): logger.exception("Failed when notifying %s (%s): %s", mail_to, mail, condition) return False except Exception as e: logger.error("Error when notifying %s: %s" % (mail_to, e)) return False return True
def _send_mail(mail_to, mail_from, subject, body, logger, mail_cc=None, debug_enabled=False): if debug_enabled: logger.debug("Sending mail to %s. Subject: %s", mail_to, subject) # logger.debug("Body: %s" % body) return True try: Utils.sendmail(toaddr=mail_to, fromaddr=mail_from, subject=subject, body=body, cc=mail_cc, debug=debug_enabled) except smtplib.SMTPRecipientsRefused as e: failed_recipients = e.recipients for mail, condition in failed_recipients.iteritems(): logger.exception("Failed when notifying %s (%s): %s", mail_to, mail, condition) return False except Exception as e: logger.error("Error when notifying %s: %s" % (mail_to, e)) return False return True
def email_report(to_address, from_address, report): """Send the report by email.""" import smtplib try: Utils.sendmail(to_address, from_address, 'ePhorte role report', report, cc=None) except smtplib.SMTPRecipientsRefused, e: failed_recipients = e.recipients logger.info("Failed to notify <%d> users", len(failed_recipients)) for email, condition in failed_recipients.iteritems(): logger.info("Failed to notify: %s", condition)
def main(): try: opts, args = getopt.getopt(sys.argv[1:], 'hsdf:', ['help', 'summary', 'detail', 'min=', 'max=', 'mail-to=', 'mail-from=', 'file=']) except getopt.GetoptError: usage("Argument error!", 1) detail = False minimum = 1 maxmum = None report_type = 'screen' outputstream = sys.stdout mail_to = None mail_from = None persons = 'person_id' for opt, val in opts: if opt in ('-h', '--help'): usage() elif opt in ('-s', '--summary'): pass elif opt in ('-d', '--detail'): detail = True elif opt in ('--min'): minimum = int(val) if minimum < 0: usage("Error: the value of parameter --min should be at least 1", 2) elif opt in ('--max'): maxmum = int(val) if maxmum < 0: usage("Error: the value of parameter --max should be at least 1", 3) elif opt in ('-f', '--file'): report_type = 'file' outputstream = open(val, 'w') outputstream.write("==== Results of checking active accounts in Cerebrum ====\n") outputstream.write("person_id\tNr_accounts\taccount_names\n") elif opt in ('--mail-to'): report_type = 'mail' mail_to = val elif opt in ('--mail-from'): mail_from = val else: usage("Error: Unknown parameter '%s'" % opt, 4) persons += checkACaccount(minimum,maxmum,detail,report_type,outputstream) if mail_to: count = persons.count("\n") subject = "Warning: these following %s persons have more than %s active accounts." % (count,minimum) Utils.sendmail(mail_to, mail_from, subject, persons)
def generate_mail_notification(quar_info, event_info, debug=False): if event_info['change_type'] == 'quarantine:mod': event_type = 'altered' elif event_info['change_type'] == 'quarantine:add': event_type = 'added' elif event_info['change_type'] == 'quarantine:del': event_type = 'deleted' else: raise Errors.CerebrumError('Unknown quarantine action: %s' % event_info['change_type']) subject = 'Quarantine %s %s on account %s' % \ (quar_info['name'], event_type, event_info['subject']) body = ('Quarantine %s %s on %s.\n\n' 'When: %s\n' 'By: %s\n' 'Change-ID: %s') % (quar_info['name'], event_type, event_info['subject'], event_info['time_stamp'], event_info['change_by'], event_info['change_id']) return Utils.sendmail(quar_info['mail_to'], quar_info['mail_from'], subject, body, debug=debug)
def send_mail(mail_to, mail_from, subject, body, mail_cc=None): """Function for sending mail to users. Will respect dryrun, as that is given and handled by Utils.sendmail, which is then not sending the e-mail. @type mail_to: string @param mail_to: The recipient of the Email. @type mail_from: string @param mail_from: The senders address. @type subject: string @param subject: The messages subject. @type body: string @param body: The message body. @type mail_cc: string @param mail_cc: An optional address that the mail will be CCed to. @rtype: bool @return: A boolean that tells if the email was sent sucessfully or not. """ try: ret = Utils.sendmail(mail_to, mail_from, subject, body, cc=mail_cc, debug=dryrun) if debug_verbose: print "---- Mail: ---- \n" + ret except smtplib.SMTPRecipientsRefused, e: failed_recipients = e.recipients logger.info("Failed to notify <%d> users", len(failed_recipients)) for _, condition in failed_recipients.iteritems(): logger.info("Failed to notify: %s", condition)
def main(): try: opts, args = getopt.getopt( sys.argv[1:], "hds:c:t:f:", ["help", "dryrun", "start-date=", "change-program=", "mail-to=", "mail-from="] ) except getopt.GetoptError: usage(1) change_program = None mail_to = None mail_from = None dryrun = False sdate = DateTime.now() - 1 for opt, val in opts: if opt in ("-h", "--help"): usage() if opt in ("-d", "--dryrun"): dryrun = True elif opt in ("-s", "--start-date"): try: sdate = DateTime.ISO.ParseDate(val) except ValueError: logger.error("Incorrect date format") usage(exitcode=2) elif opt in ("-c", "--change-program"): change_program = val elif opt in ("-t", "--mail-to"): mail_to = val elif opt in ("-f", "--mail-from"): mail_from = val new_persons = get_new_persons(sdate, change_program=change_program) if new_persons: msg = report_new_persons(new_persons) if change_program: subject = "New persons from %s since %s" % (change_program, sdate.date) else: subject = "New persons since %s" % sdate.date if mail_to and not dryrun: Utils.sendmail(mail_to, mail_from, subject, msg) else: print msg
def main(): try: opts, args = getopt.getopt(sys.argv[1:], 'ho:', [ 'help', 'min=', 'max=', 'mail-to=', 'mail-from=', 'source_systems=', 'output=' ]) except getopt.GetoptError: usage(1) minimum = 1 maxmum = None outputstream = sys.stdout mail_to = None mail_from = None source_systems = 'SAP,FS' for opt, val in opts: if opt in ('-h', '--help'): usage() elif opt in ('--min'): minimum = int(val) if minimum < 0: usage(2) elif opt in ('--max'): maxmum = int(val) if maxmum < 0 or maxmum < minimum: usage(3) elif opt in ('--source_systems'): source_systems = val elif opt in ('-o', '--output'): outputstream = open(val, 'w') outputstream.write("""<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Active accounts report</title> <style type="text/css"> h1 { margin: 1em .8em 1em .8em; font-size: 1.4em; } h2 { margin: 1.5em 1em 1em 1em; font-size: 1em; } table + h1 { margin-top: 3em; } table { border-collapse: collapse; width: 100%; text-align: left; } table thead { border-bottom: solid gray 1px; } table th, table td { padding: .5em 1em; width: 10%; } .meta { color: gray; text-align: right; } </style> </head> <body> """) elif opt in ('--mail-to'): mail_to = val elif opt in ('--mail-from'): mail_from = val else: usage(4) persons = 'These following persons are found in Cerebrum:\n\nperson_id' persons += checkACaccount(source_systems, minimum, maxmum, outputstream) if not outputstream is sys.stdout: outputstream.close() if mail_to: subject = "Report from check_acctive_account.py" Utils.sendmail(mail_to, mail_from, subject, persons)
def main(): try: opts, args = getopt.getopt(sys.argv[1:], "hds:c:t:f:", [ "help", "dryrun", "start-date=", "change-program=", "mail-to=", "mail-from=" ]) except getopt.GetoptError: usage(1) change_program = None mail_to = None mail_from = None dryrun = False sdate = DateTime.now() - 1 for opt, val in opts: if opt in ( '-h', '--help', ): usage() if opt in ( '-d', '--dryrun', ): dryrun = True elif opt in ( '-s', '--start-date', ): try: sdate = DateTime.ISO.ParseDate(val) except ValueError: logger.error("Incorrect date format") usage(exitcode=2) elif opt in ( '-c', '--change-program', ): change_program = val elif opt in ( '-t', '--mail-to', ): mail_to = val elif opt in ( '-f', '--mail-from', ): mail_from = val new_persons = get_new_persons(sdate, change_program=change_program) if new_persons: msg = report_new_persons(new_persons) if change_program: subject = "New persons from %s since %s" % (change_program, sdate.date) else: subject = "New persons since %s" % sdate.date if mail_to and not dryrun: Utils.sendmail(mail_to, mail_from, subject, msg) else: print msg
# Compare mailbox state between Cerebrum and Exchange new_state, report = sc.compare_mailbox_state(mb_info, cere_mb_info, state, attr_config) # Join the report together try: rep = u'\n'.join(report) except UnicodeDecodeError, e: print(str(e)) tmp = [] for x in report: tmp.append(x.decode('UTF-8')) rep = u'\n'.join(tmp) # Send the report by mail if mail and sender: Utils.sendmail(mail, sender, 'Exchange mailbox state report', rep.encode('utf-8')) # Write the report to file if repfile: f = open(repfile, 'w') f.write(rep.encode('utf-8')) f.close() # TODO: Exceptions? # Overwrite the old state with the new one. f = open(state_file, 'w') pickle.dump(new_state, f) f.close()
ex_group_info = sc.collect_exchange_group_info(group_ou) sc.close() cere_group_info = sc.collect_cerebrum_group_info(conf["mailbox_spread"], conf["ad_spread"]) # Compare group state new_state, report = sc.compare_group_state(ex_group_info, cere_group_info, state, attr_config) try: rep = u"\n".join(report) except UnicodeDecodeError, e: print(str(e)) tmp = [] for x in report: tmp.append(x.decode("UTF-8")) rep = u"\n".join(tmp) # Send a report by mail if mail and sender: Utils.sendmail(mail, sender, "Exchange group state report", rep.encode("utf-8")) # Write report to file if repfile: f = open(repfile, "w") f.write(rep.encode("utf-8")) f.close() # TODO: Exceptions? f = open(state_file, "w") pickle.dump(new_state, f) f.close()
def main(): try: opts, args = getopt.getopt(sys.argv[1:], 'ho:', ['help', 'min=', 'max=', 'mail-to=', 'mail-from=', 'source_systems=', 'output=']) except getopt.GetoptError: usage(1) minimum = 1 maxmum = None outputstream = sys.stdout mail_to = None mail_from = None source_systems = 'SAP,FS' for opt, val in opts: if opt in ('-h', '--help'): usage() elif opt in ('--min'): minimum = int(val) if minimum < 0: usage(2) elif opt in ('--max'): maxmum = int(val) if maxmum < 0 or maxmum < minimum: usage(3) elif opt in ('--source_systems'): source_systems = val elif opt in ('-o', '--output'): outputstream = open(val, 'w') outputstream.write("""<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Active accounts report</title> <style type="text/css"> h1 { margin: 1em .8em 1em .8em; font-size: 1.4em; } h2 { margin: 1.5em 1em 1em 1em; font-size: 1em; } table + h1 { margin-top: 3em; } table { border-collapse: collapse; width: 100%; text-align: left; } table thead { border-bottom: solid gray 1px; } table th, table td { padding: .5em 1em; width: 10%; } .meta { color: gray; text-align: right; } </style> </head> <body> """) elif opt in ('--mail-to'): mail_to = val elif opt in ('--mail-from'): mail_from = val else: usage(4) persons = 'These following persons are found in Cerebrum:\n\nperson_id' persons += checkACaccount(source_systems, minimum, maxmum, outputstream) if not outputstream is sys.stdout: outputstream.close() if mail_to: subject = "Report from check_acctive_account.py" Utils.sendmail(mail_to, mail_from, subject, persons)