Hi Readers,
I have been given a below task which needs to be done due to some security issue:-
- Critical APP send invoice(pdf) to mailbox
- User than reads the email address of vendor from body
- After that fwd that email to Vendor.
This manual process needs to be automated.
below is the screen shot on how the email gets delivered to mailbox
By researching on internet & thru my own experience I have written the below powershell method
that will read email address of vendor from the body , extract the attachment ,send email to vendor with attachment &
move the existing email to processed folder.
Solution can be downloaded from below Location:-
https://github.com/vikasSukhija/Downloads
Script Code: (Just create logs folder inside the folder from which script runs)
##################################################################################### # Author: Vikas Sukhija # Date:- 09/12/2013 # Description:- read emailbody,extract attachment & send email # with extracted attachment # Prerequisites :- Powershell/Outlook ##################################################################################### ###############################Logs################################################## $date = get-date -format d $date = $date.ToString().Replace(“/”, “-”) $time = get-date -format t $time = $time.ToString().Replace(":", "-") $time = $time.ToString().Replace(" ", "") $log1 = ".\Logs" + "\" + "Processed_" + $date + "_.log" $logs = ".\Logs" + "\" + "Powershell" + $date + "_" + $time + "_.txt" Start-Transcript -Path $logs $date1 = get-date #############################outlook Call############################################# $olFolderInbox = 6 $outlook = new-object -com outlook.application; $ns = $outlook.GetNameSpace("MAPI"); $inbox = $ns.GetDefaultFolder($olFolderInbox) $messages = $inbox.items write-host $messages.count $messcount = $messages.count add-content $log1 $date1 add-content $log1 "Messages Count: $messcount" $countprocessed = 0 foreach($message in $messages){ $msubject = $message.subject add-content $log1 "Messages Subject: $msubject" $mBody = $message.body #Write-Host $mBody $mBodySplit = $mBody -split "Customer Email ID:" $toaddress1=$mBodySplit[1] $toaddress1 add-content $log1 "Vendor Email: $toaddress1" ###################################Save Invoice####################################### $filepath = "c:\temp\" $message.attachments|foreach { Write-Host $_.filename $attr = $_.filename add-content $log1 "Attachment: $attr" $a = $_.filename If ($a.Contains("pdf")) { $_.saveasfile((Join-Path $filepath $a)) } } $attachment = "c:\temp\" + $a ########################send email invoice########################################### $mail = $outlook.CreateItem(0) $Mail.Recipients.Add($toaddress1) $Mail.Subject = "$msubject" $Mail.Body = "" $Mail.Attachments.Add($attachment) $Mail.Send() add-content $log1 "Message Sent: $toaddress1" #################################Move Items######################################### $MoveTarget = $inbox.Folders.item("Processed") [void]$message.Move($MoveTarget) add-content $log1 "Message Moved to: Processed" $countprocessed = $countprocessed + 1 } add-content $log1 "Total Messages Processed: $countprocessed" ###########################Report Errors############################################# if ($error -ne $null) { $smtpServer = "SMTP Server" $msg = new-object Net.Mail.MailMessage $smtp = new-object Net.Mail.SmtpClient($smtpServer) $msg.From = "OutlookautomationScript@testlab.com" $msg.To.Add("Vikas.Sukhija@testlab.com") $msg.Subject = "Outlook Script Error" $msg.Body = $error $smtp.Send($msg) } Stop-Transcript #####################################END################################################
Regards
Sukhija Vikas
Is there a way to make this work for office 365 also? We get alot of invoices on there so it would be helpfull if we could use the script on a server which runs every couple min to download the attachments and then move the mail.
Its outlook script so it should work, it has nothing to do with back-end.
Well, our server does not have outlook. Thats why it would be helpfull if it could connect to office 365 on its own and then download specific invoices.
This script can run from any machine, doesn’t need server just need outlook.