1

I'm trying to create a program that will send and email while also logging to a text file the email's subject line and date/time it was sent. Here is what I have so far.

def send_email():
    from exchangelib import Credentials, Account, Message, Mailbox, UTC_NOW
    import time

    credentials = Credentials('my@email', 'password')
    account = Account('my@email', credentials=credentials, autodiscover=True)

    m = Message(
                account=account,
                subject='Test Subject',
                body='Test Body',
                to_recipients=[
                    Mailbox(email_address='my@email')
                ])
    text_file = open("Output.txt", "w")
    text_file.write(time.strftime("%H:%M:%S"))
    text_file.close()
    m.send()

send_email()

As it is written currently, the program will send an email (to myself for testing), and will log in a txt file (Output.txt) the current time. What I'm trying to output is this:

Subject:"Test Subject" Date: 4/12/2019 Time: 13:45:09

*Replace date and time with the date and time it was sent.

Granted I still have a little ways to go, I'm starting to get confused on the format of how to do so as I'm new to Python. Is it possible to do all of this and have it print on a single line? Or will it have to print on multiple lines? How should I go about doing this?

JK72
  • 149
  • 1
  • 8
  • Are you getting an undesirable output? If so, can you share so we can see what you're getting vs what you want? – Cohan Apr 12 '19 at 17:49

2 Answers2

1

You would need to add additional write statements in order to output the additional information. It is also better to use with block to handle file opening and closing instead of doing it manually. Learn More.

Example:

def send_email():
    from exchangelib import Credentials, Account, Message, Mailbox, UTC_NOW
    import time

    credentials = Credentials('my@email', 'password')
    account = Account('my@email', credentials=credentials, autodiscover=True)

    subject = 'Test Subject'
    m = Message(
        account=account,
        subject=subject,
        body='Test Body',
        to_recipients=[
            Mailbox(email_address='my@email')
        ])

    with open("Output.txt", "w") as text_file:
        text_file.write("Subject: ")
        text_file.write(subject)
        text_file.write(" ")
        text_file.write("Date: ")
        text_file.write(time.strftime("%d/%m/%Y"))
        text_file.write(" ")
        text_file.write("Time: ")
        text_file.write(time.strftime("%H:%M:%S"))
        text_file.close()
        text_file.close()

    m.send()


send_email()

Output: Subject: Test Subject Date: 12/04/2019 Time: 18:49:13

DobromirM
  • 2,017
  • 2
  • 20
  • 29
  • With this method and if I switched `"w"` to `"a"` to append the file, how would I get the next print to be on a new line? – JK72 Apr 16 '19 at 20:18
  • You just need to write `\n` for a new line. `text_file.write("\n")` whenever you need a line. – DobromirM Apr 16 '19 at 21:42
1

First of all, change the write mode to a instead of w since using w will just clear all previous content of the file. Using a, new data is appended to the file instead. Just replace your write part with this:

with open("Output.txt", "a") as f:

Also, to write the data in your format, create the string first, and write it in one go:

final_log = "Subject: {subject} Date: {date} Time: {time}\n".format(
    subject=subject, date=time.strftime("%d/%m/%Y"), time=time.strftime("%H:%M:%S")
)
f.write(final_log)
Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63
Soumitra Shewale
  • 165
  • 1
  • 1
  • 13