Python 运行 邮件接收脚本
Python run script upon mail reception
我有一个 .py 脚本,我想 运行 在收到来自特定主题的发件人的邮件时自动执行该脚本。实现此目标的最佳方法是什么?
您可以使用模块 exchangelib 来访问您的电子邮件,但不能只让它收听。你需要每隔一段时间刮一次。您还可以使用此处讨论的 VBA 宏:
https://docs.microsoft.com/en-us/office/vba/outlook/concepts/getting-started/writing-an-outlook-macro
一个例子:
Sub OffensiveWords(Item As MailItem)
Dim arrSpam As Variant
Dim strBody As String
Dim i As Long
strBody = Item.Subject & " " & Item.Body
' Set up the array - use lower case
arrSpam = Array("funded", "ppp", "loan", "funding", "word5")
' Go through the array and look for a match, then do something
For i = LBound(arrSpam) To UBound(arrSpam)
If InStr(LCase(strBody), arrSpam(i)) Then
' do whatever here
Item.Categories = "Spammy"
Item.Save
Exit Sub
Next i
End Sub
' use this to test the rules script - select a message and run this macro
Sub RunAndRules()
Dim objApp As Outlook.Application
Dim objItem As Object ' MailItem
Set objApp = Application
Set objItem = objApp.ActiveExplorer.Selection.Item(1)
'macro name you want to run goes here
OffensiveWords objItem
End Sub
https://www.slipstick.com/outlook/filter-messages-offensive-words/
https://pypi.org/project/exchangelib/
from exchangelib import Credentials, Account
credentials = Credentials('john@example.com', 'topsecret')
account = Account('john@example.com', credentials=credentials, autodiscover=True)
for item in account.inbox.all().order_by('-datetime_received')[:100]:
print(item.subject, item.sender, item.datetime_received)
我有一个 .py 脚本,我想 运行 在收到来自特定主题的发件人的邮件时自动执行该脚本。实现此目标的最佳方法是什么?
您可以使用模块 exchangelib 来访问您的电子邮件,但不能只让它收听。你需要每隔一段时间刮一次。您还可以使用此处讨论的 VBA 宏: https://docs.microsoft.com/en-us/office/vba/outlook/concepts/getting-started/writing-an-outlook-macro 一个例子:
Sub OffensiveWords(Item As MailItem)
Dim arrSpam As Variant
Dim strBody As String
Dim i As Long
strBody = Item.Subject & " " & Item.Body
' Set up the array - use lower case
arrSpam = Array("funded", "ppp", "loan", "funding", "word5")
' Go through the array and look for a match, then do something
For i = LBound(arrSpam) To UBound(arrSpam)
If InStr(LCase(strBody), arrSpam(i)) Then
' do whatever here
Item.Categories = "Spammy"
Item.Save
Exit Sub
Next i
End Sub
' use this to test the rules script - select a message and run this macro
Sub RunAndRules()
Dim objApp As Outlook.Application
Dim objItem As Object ' MailItem
Set objApp = Application
Set objItem = objApp.ActiveExplorer.Selection.Item(1)
'macro name you want to run goes here
OffensiveWords objItem
End Sub
https://www.slipstick.com/outlook/filter-messages-offensive-words/
https://pypi.org/project/exchangelib/
from exchangelib import Credentials, Account
credentials = Credentials('john@example.com', 'topsecret')
account = Account('john@example.com', credentials=credentials, autodiscover=True)
for item in account.inbox.all().order_by('-datetime_received')[:100]:
print(item.subject, item.sender, item.datetime_received)