-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_processor_v2.0.6.3.ps1
207 lines (176 loc) · 6.72 KB
/
message_processor_v2.0.6.3.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# Email Message Processor v2.0.6.3
# Written by Culpur.net
# Free to use Licensed under GNU GPLv3
# Need Help contact us msgpro [at] culpur.net
# Script last updated on 23.03.2023 at 20:27
param(
$UserSettingsFilePath = "user_settings.json"
)
function Set-ExecutionPolicyUnrestricted {
try {
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted -ErrorAction Stop
} catch {
Write-Error "Failed to set execution policy: $_"
return $false
}
return $true
}
function ScanOutlookFolders {
$global:olFolders = @{
'JVETInbox' = @{
'Folder' = $null;
'Processed' = $null;
'UnProcessed' = $null;
}
}
$outlook = New-Object -ComObject Outlook.Application
$namespace = $outlook.GetNamespace('MAPI')
$inbox = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
$jvetInbox = $inbox.Folders | Where-Object { $_.Name -eq 'JVET Inbox' }
if ($null -eq $jvetInbox) {
$jvetInbox = $inbox.Folders.Add('JVET Inbox')
}
$olFolders.JVETInbox.Folder = $jvetInbox
$processed = $jvetInbox.Folders | Where-Object { $_.Name -eq 'Processed' }
if ($null -eq $processed) {
$processed = $jvetInbox.Folders.Add('Processed')
}
$olFolders.JVETInbox.Processed = $processed
$unprocessed = $jvetInbox.Folders | Where-Object { $_.Name -eq 'UnProcessed' }
if ($null -eq $unprocessed) {
$unprocessed = $jvetInbox.Folders.Add('UnProcessed')
}
$olFolders.JVETInbox.UnProcessed = $unprocessed
}
function Get-UserSettings {
$filePath = $UserSettingsFilePath
if (Test-Path $filePath) {
$json = Get-Content $filePath -Raw | ConvertFrom-Json
$settings = [PSCustomObject]$json
} else {
$settings = @{
SendFrom = ""
SendTo = ""
Smtp = ""
Encryption = "Unencrypted"
Interval = 2
RI = ""
}
}
Save-UserSettings
return $settings
}
function Save-UserSettings {
$filePath = $UserSettingsFilePath
$json = [System.Web.Script.Serialization.JavaScriptSerializer]::new().Serialize($global:userSettings)
Set-Content -Path $filePath -Value $json
}
$date = Get-Date -Format "ddhhmm MMM yy"
$header = @"
ENTER HEADER TEXT HERE
"@
$header2 = @"
ENTER HEADER TEXT HERE
"@
$footer = @"
ENTER FOOTER TEXT HERE
"@
function ProcessEmails {
$folder = $global:olFolders.JVETInbox.Folder
$emailCount = $folder.Items.Count
Write-Host "Total messages found in JVET Inbox: $emailCount"
for ($i = $emailCount; $i -gt 0; $i--) {
$email = $folder.Items($i)
if ($email.Body -match 'ENTER SOMETHING FOUND IN THE EMAIL') {
$newEmail = CreateNewEmail -OriginalEmail $email -BeforeText $header -AfterText $footer
} elseif ($email.Body -match 'ENTER SOMETHING FOUND IN OTHER EMAIL') {
$newEmail = CreateNewEmail -OriginalEmail $email -BeforeText $header2 -AfterText $footer2
}
if ($null -ne $newEmail) {
if (SendEmail -Email $newEmail) {
MoveEmail -Email $email -DestinationFolder $global:olFolders.JVETInbox.Processed
Write-Host "Successfully sent email: $($email.Subject)" -ForegroundColor Green
} else {
MoveEmail -Email $email -DestinationFolder $global:olFolders.JVETInbox.UnProcessed
Write-Host "Failed to send email: $($email.Subject)" -ForegroundColor Red
}
}
}
DeleteOldProcessedEmails
}
function CreateNewEmail {
param(
$OriginalEmail,
$BeforeText,
$AfterText
)
$newEmail = $global:outlook.CreateItem(0)
$newEmail.BodyFormat = [Microsoft.Office.Interop.Outlook.OlBodyFormat]::olFormatPlain
$newEmail.Body = $BeforeText + $OriginalEmail.Body + $AfterText
$newEmail.To = $global:userSettings.SendTo
$newEmail.From = $global:userSettings.SendFrom
return $newEmail
}
function SendEmail {
param(
$Email
)
try {
$Email.Send()
return $true
} catch {
Write-Verbose "Error sending email: $_.Exception.Message" -Verbose
Add-Content -Path "errors.log" -Value "Error sending email: $_.Exception.Message"
return $false
}
}
function MoveEmail {
param(
$Email,
$DestinationFolder
)
try {
$Email.Move($DestinationFolder)
Write-Host "Email moved to $($DestinationFolder.Name)" -ForegroundColor Green
} catch {
Write-Host "Failed to move email to $($DestinationFolder.Name): $_" -ForegroundColor Red
}
}
function DeleteOldProcessedEmails {
$processedFolder = $global:olFolders.JVETInbox.Processed
$cutoffDate = (Get-Date).AddDays(-2)
for ($i = $processedFolder.Items.Count; $i -gt 0; $i--) {
$email = $processedFolder.Items($i)
if ($email.ReceivedTime -lt $cutoffDate) {
try {
$email.Delete()
} catch {
Write-Host "Failed to delete email: $($email.Subject)" -ForegroundColor Red
Write-Verbose "Error deleting email: $_" -Verbose
Add-Content -Path "errors.log" -Value "Error deleting email: $($email.Subject)"
}
}
}
}
function Main {
Set-ExecutionPolicyUnrestricted
ScanOutlookFolders
$global:userSettings = Get-UserSettings -UserSettingsFilePath $UserSettingsFilePath
$global:outlook = New-Object -ComObject Outlook.Application
$global:userSettings.SendFrom = Read-Host -Prompt "Enter the email address you want to send from (Default: $($global:userSettings.SendFrom))" -Default $($global:userSettings.SendFrom)
$global:userSettings.SendTo = Read-Host -Prompt "Enter the email address you want to send to (Default: $($global:userSettings.SendTo))" -Default $($global:userSettings.SendTo)
$global:userSettings.Smtp = Read-Host -Prompt "Enter the SMTP server to use (Default: $($global:userSettings.Smtp))" -Default $($global:userSettings.Smtp)
$global:userSettings.Encryption = Read-Host -Prompt "Select the encryption type: Unencrypted or Encrypted (Default: $($global:userSettings.Encryption))" -Default $($global:userSettings.Encryption)
Save-UserSettings
do {
ProcessEmails
if ($global:userSettings.Interval) {
Start-Sleep -Seconds ($global:userSettings.Interval * 60)
} else {
Write-Warning "Interval not set in user settings. Using default value of 2 minutes."
Start-Sleep -Seconds (2 * 60)
}
# Start-Sleep -Seconds ($global:userSettings.Interval * 60)
} until ($Host.UI.RawUI.KeyAvailable -and ($Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown").VirtualKeyCode -eq 3))
}
Main