Esempio n. 1
0
def main():
    p = argparse.ArgumentParser(description='Receives a file containing email addresses and sends messages to them')
    p.add_argument('-i', '--input', metavar='input', type=str, help='The file containing all the information')
    p.add_argument('-s', '--subject', metavar='subject', type=str, help='The subject line for the email')

    args = p.parse_args()
    filename = args.input
    subject_line = args.subject
    # Saves the message into a single string
    std_input = ''
    for line in sys.stdin:
        std_input += line

    emails = []
    if reader.file_exists(filename):
        readings = reader.open_csv_file(filename)

        for row in readings:
            emails.append(generate_email_data(row, std_input, subject_line))

        for email in emails:
            print("email", "-s", email.get('subject'), email.get('email'), "<<<", email.get('message'))
            # call(["email", "-s", email.get('subject'), email.get('email'), "<<<", email.get('message')])
    else:
        raise IOError('Please point to an existing file')
Esempio n. 2
0
def main():
    p = argparse.ArgumentParser(description='Receives a file containing email addresses and sends messages to them')
    p.add_argument('-d', '--datafile', metavar='datafile', type=str, help='The file containing all user information')
    p.add_argument('-m', '--message', metavar='msg', dest='msg', type=str, help='The file containing the email message')
    p.add_argument('-s', '--subject', metavar='subject', type=str, help='The subject line for the email')
    p.add_argument('-l', '--login', metavar='login', dest='smtp_login', type=str, help='Login email address')
    p.add_argument('-p', '--pass', metavar='password', dest='smtp_pass', type=str, help='The password for login')
    p.add_argument('--mode', metavar='mode', dest='mode', type=str, help='Operation mode, options: show, send, default: show')
    p.add_argument('--log', metavar='log', dest='log', type=str, help='Log file name')

    args = p.parse_args()
    subject_line = args.subject

    if args.mode:
        mode = args.mode
    else:
        mode = 'show'

    if args.msg:
        msg_file = args.msg
    else:
        msg_file = "message"

    if args.datafile:
        filename = args.datafile
    else:
        filename = "datafile"
    if mode == 'send':
        if not args.smtp_login:
            print("Missing info: ", "Please provide login")
            return
        smtp_login = args.smtp_login

        if not args.smtp_pass:
            print("Missing info: ", "Please provide password")
            return
        smtp_pass = args.smtp_pass

    if args.log:
        logfile = args.log
    else:
        logfile = "emailerlog_" + datetime.datetime.now().strftime("%Y%m%d_%H%M%S")

    with open(msg_file) as mf:
        msg = mf.read()

    emails = []
    if reader.file_exists(filename):
        readings = reader.open_csv_file(filename, ['name', 'email'])
        if readings == None:
            print("Error encountered during reading of csv data file")
            return

        for row in readings:
            email_data = generate_email_data(row, msg, subject_line)
            if email_data == None:
                print("Erorr encountered while generating emails")
                return
            emails.append(email_data)

        if mode == 'show':
            count = 0
            for email in emails:
                print("#", count, "    ", "Email: ", email.get('email'))
                print("Title: ", email.get('subject'))
                print("Message:")
                print(email.get('message'))
                count += 1
                input("Press enter to continue...")
            print("")
            print("End of all emails")
        elif mode == 'send':
            # setup logging
            interrupted_sends = []  # operate like a stack in below code
            confirmed_sends = []
            if os.path.isfile(logfile):
                logfh = open(logfile, 'r')
                logstr = logfh.read()
                logentries = logstr.split('\n')
                for entry in logentries:    # go through to find entries that indicate interrupted sending
                    fields = entry.replace(' ', '').split(',')   # format : line, start/end
                    if fields == ['']:
                        continue
                    if fields[1] == 'start':
                        interrupted_sends.append(int(fields[0]))
                    else:
                        if interrupted_sends[len(interrupted_sends)-1] == int(fields[0]):
                            interrupted_sends.remove(int(fields[0]))
                            confirmed_sends.append(int(fields[0]))
                logfh.close()
                logfh = open(logfile, 'a')
            else:
                logfh = open(logfile, 'w')

            # setup mailing client
            print("Logging in")
            mail_client = setup_mailer(smtp_login, smtp_pass)

            if not mail_client:
                print('Error encoutnered during emailer setup')
                return

            count = 0
            for email in emails:
                if count in confirmed_sends:
                    continue
                print(">Sending out email : ", count)           # print to console
                logfh.write(str(count) + ',' + "start" + "\n")  # write to log

                send_email(mail_client, smtp_login, email)      # send email
                time.sleep(0.5)                                 # delay to avoid overloading network

                logfh.write(str(count) + ',' + "end" + "\n")    # print to console
                print("<Email sent")                            # write to log
                count += 1
            logfh.close()
            print("")
            print("All emails sent")
    else:
        raise IOError('Please point to an existing file')