发送电子邮件后尝试将电子邮件主题和日期时间打印到 txt 文件
Trying to print email subject and datetime to txt file after sending email
我正在尝试创建一个程序,该程序将发送和发送电子邮件,同时还将电子邮件的主题行和发送的 date/time 记录到文本文件中。这是我到目前为止所拥有的。
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()
正如目前所写,程序会发送一封电子邮件(给自己进行测试),并会在当前时间登录一个 txt 文件 (Output.txt)。我要输出的是:
Subject:"Test Subject" Date: 4/12/2019 Time: 13:45:09
*用发送日期和时间替换日期和时间。
虽然我还有一些路要走,但我开始对如何做的格式感到困惑,因为我是 Python 的新手。是否可以完成所有这些并将其打印在一行上?还是必须打印多行?我应该怎么做?
您需要添加额外的写入语句才能输出额外的信息。最好使用 with
块来处理文件打开和关闭,而不是手动进行。 Learn More.
示例:
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()
输出: Subject: Test Subject Date: 12/04/2019 Time: 18:49:13
首先,将写入模式改为a
而不是w
,因为使用w
只会清除文件之前的所有内容。使用 a
,新数据被附加到文件中。只需将您的写入部分替换为:
with open("Output.txt", "a") as f:
另外,要按照你的格式写入数据,先创建字符串,然后一次性写入:
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)
我正在尝试创建一个程序,该程序将发送和发送电子邮件,同时还将电子邮件的主题行和发送的 date/time 记录到文本文件中。这是我到目前为止所拥有的。
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()
正如目前所写,程序会发送一封电子邮件(给自己进行测试),并会在当前时间登录一个 txt 文件 (Output.txt)。我要输出的是:
Subject:"Test Subject" Date: 4/12/2019 Time: 13:45:09
*用发送日期和时间替换日期和时间。
虽然我还有一些路要走,但我开始对如何做的格式感到困惑,因为我是 Python 的新手。是否可以完成所有这些并将其打印在一行上?还是必须打印多行?我应该怎么做?
您需要添加额外的写入语句才能输出额外的信息。最好使用 with
块来处理文件打开和关闭,而不是手动进行。 Learn More.
示例:
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()
输出: Subject: Test Subject Date: 12/04/2019 Time: 18:49:13
首先,将写入模式改为a
而不是w
,因为使用w
只会清除文件之前的所有内容。使用 a
,新数据被附加到文件中。只需将您的写入部分替换为:
with open("Output.txt", "a") as f:
另外,要按照你的格式写入数据,先创建字符串,然后一次性写入:
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)