2015-12-02 32 views
0

我適應一些hMailServer代碼,我發現到MS Outlook VBA。源代碼是在https://www.hmailserver.com/forum/viewtopic.php?f=14&t=2960什麼對象是「oMessage」在hMailServer郵件管道?

我在hMailServer和雷鳥測試此代碼,並將它的工作。但是,在部署中,我希望我無法訪問hMailServer,並且郵件客戶端很可能是MS Outlook。

在源代碼中,作者引用了「oMessage」,但是,我不能確定「oMessage」應該是什麼對象,並且在我的改寫中會導致錯誤是錯誤的命令行字符串當然,「需要的對象」。到那時,我的vba腳本工作正常。由於hMailServer的線程已經有幾年了,所以我不希望在我發佈的問題上得到答覆。

原來這裏是源代碼:

Const g_sPHPPath  = "C:\path\to\php.exe" 
Const g_sScriptPath = "C:\path\to\script.php" 
Const g_sPipeAddress = "[email protected]" 

Sub OnDeliverMessage(oMessage) 

If g_sPipeAddress = "" Then 
    bPipeMessage = True 
Else 
    bPipeMessage = False 

    Set obRecipients = oMessage.Recipients 

    For i = 0 to obRecipients.Count - 1 
     Set obRecipient = obRecipients.Item(i) 

     If LCase(obRecipient.Address) = LCase(g_sPipeAddress) Then 
      bPipeMessage = True 
     End If 
    Next 
End If 

If bPipeMessage Then 
    sCommandLine = "cmd /c type " & g_sDQ & oMessage.Filename & g_sDQ & " | " & g_sDQ & g_sPHPPath & g_sDQ & " " & g_sDQ & g_sScriptPath & g_sDQ 
    Set oShell = CreateObject("WScript.Shell") 
    Call oShell.Run(sCommandLine, 0, TRUE) 
End If 

End Sub 

這裏是我的適應:

Const g_sPHPPath = "C:\xampp\php\php.exe" 
Const g_sScriptPath = "C:\xampp\htdocs\Recycler\test.php" 
Const g_sPipeAddress = "[email protected]" 
Const g_sDQ = """" 

Sub OnDeliverMessage(oMessage) 
Dim Explorer As Outlook.Explorer 
Dim CurrentItem As Object 

Set Explorer = Application.ActiveExplorer 
If Explorer.Selection.Count Then 
    Set CurrentItem = Explorer.Selection(1) 
End If 

If CurrentItem.Class = olMail Then 
    Dim sender 
    sender = CurrentItem.SenderEmailAddress 
End If 

If g_sPipeAddress = "" Then 
    bPipeMessage = True 
Else 
    If LCase(sender) = LCase(g_sPipeAddress) Then 
     bPipeMessage = True 
    End If 
End If 

If bPipeMessage Then 
    sCommandLine = "cmd /c type " & g_sDQ & oMessage.FileName & g_sDQ & " | " & g_sDQ & g_sPHPPath & g_sDQ & " " & g_sDQ & g_sScriptPath & g_sDQ 
    Set oShell = CreateObject("WScript.Shell") 
    Call oShell.Run(sCommandLine, 0, True) 
End If 
End Sub 

那麼,誰能告訴我什麼對象oMessage將等同於在Outlook模式?在cmd字符串中,我應該在「oMessage.FileName」中查找什麼?

回答

0

得到了hMailServer的回覆:「它是hmailserver在接收到消息(物理.EML文件)時創建的文件名,然後在客戶端請求時將其發送給客戶端。」

所以,爭論「oMessage」從hMailServer通過,但它不是在這個VBA適應需要。

解決方案只是將電子郵件保存到過程正文中的文本文件「CurrentItem.SaveAs g_FileName,olTXT」,其中將g_FileName聲明爲常量。

因此,電子郵件已被傳送到一個文本文件,它可以用您選擇的語言進行分析。在我的情況下,PHP,其中如「名」,「門店數量」,「電話號碼」等價值觀被檢索並保存到MySQL數據庫。

最後,在Outlook中應用的規則是收到的電子郵件時,它被移動到文件夾,和OnDeliverMessage()腳本調用。然後

修改後的代碼是:

Const g_sPHPPath = "C:\xampp\php\php.exe" 
Const g_sScriptPath = "C:\xampp\htdocs\Recycler\handler.php" 
Const g_sPipeAddress = "[email protected]" 
Const g_FileName = "C:\tmp\output.txt" 
Const g_sDQ = """" 

Sub OnDeliverMessage() 
Dim Explorer As Outlook.Explorer 
Dim CurrentItem As Object 

Set Explorer = Application.ActiveExplorer 
If Explorer.Selection.Count Then 
    Set CurrentItem = Explorer.Selection(1) 
End If 

CurrentItem.SaveAs g_FileName, olTXT 

If CurrentItem.Class = olMail Then 
    Dim sender 
    sender = CurrentItem.SenderEmailAddress 
End If 

If g_sPipeAddress = "" Then 
    bPipeMessage = True 
Else 
    If LCase(sender) = LCase(g_sPipeAddress) Then 
     bPipeMessage = True 
    End If 
End If 

If bPipeMessage Then 
    sCommandLine = "cmd /c type " & g_sDQ & g_FileName & g_sDQ & " | " & g_sDQ & g_sPHPPath & g_sDQ & " " & g_sDQ & g_sScriptPath & g_sDQ 
    Set oShell = CreateObject("WScript.Shell") 
    Call oShell.Run(sCommandLine, 0, True) 
End If 
End Sub