Advertisement
Guest User

Read emailbody and extract attachment

a guest
Jun 29th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #####################################################################################
  2. #            Author: William Reyor
  3. #        opticfiber@gmail.com
  4. #            Date:- 06/29/2015
  5. #            Description:- read emailbody,extract attachment
  6. #            Prerequisites :- Powershell/Outlook
  7. #
  8. #####################################################################################
  9. # Heavily borrowed from code and concepts from:
  10. # https://gallery.technet.microsoft.com/scriptcenter/Outlook-Automation-using-d7584688
  11. # http://blogs.technet.com/b/heyscriptingguy/archive/2011/03/21/use-powershell-to-replace-text-in-strings.aspx
  12. # https://confidentialfiles.wordpress.com/2012/07/01/renaming-in-powershell-underscore-to-space/
  13. # http://blogs.technet.com/b/heyscriptingguy/archive/2011/03/21/use-powershell-to-replace-text-in-strings.aspx
  14. # http://stackoverflow.com/questions/15113413/how-to-concatenate-strings-and-variables-in-powershell
  15. # http://www.computerperformance.co.uk/powershell/powershell_if_and.htm
  16. ###############################Debug Logs##################################################
  17.  $date = get-date -format d
  18.  $date = $date.ToString().Replace(/,-)
  19.  $time = get-date -format t
  20.  $time = $time.ToString().Replace(":", "-")
  21.  $time = $time.ToString().Replace(" ", "")
  22.  
  23.  $log1 = ".\Logs" + "\" + "Processed_" + $date + "_.log"
  24.  
  25.  $logs = ".\Logs" + "\" + "Powershell" + $date + "_" + $time + "_.txt"
  26.  
  27. # Start-Transcript -Path $logs  
  28.  
  29.  $date1 = get-date
  30.  
  31. #############################outlook Call#############################################
  32.  
  33. $olFolderInbox = 6
  34. $outlook = new-object -com outlook.application;
  35. $ns = $outlook.GetNameSpace("MAPI");
  36. $inbox = $ns.GetDefaultFolder($olFolderInbox)
  37. $messages = $inbox.items
  38. write-host $messages.count
  39. $messcount = $messages.count
  40. add-content $log1 $date1
  41. add-content $log1 "Messages Count: $messcount"
  42. $countprocessed = 0
  43. foreach($message in $messages){
  44. $msubject = $message.subject
  45. add-content $log1 "Messages Subject: $msubject"
  46. $mBody = $message.body
  47. #Write-Host $mBody
  48. $mBodySplit = $mBody -split "Customer Email ID:"
  49. $toaddress1=$mBodySplit[1]
  50. $toaddress1
  51. add-content $log1 "Vendor Email: $toaddress1"
  52.  
  53. ###################################Save Invoice#######################################
  54. # Change the subjet to a useable string to save to the file system
  55.  
  56.  
  57.  
  58. $filepath = "c:\data\report\"
  59. $message.attachments|foreach {
  60.     ## input cleanup from subjectlines
  61.     $filename = $msubject -replace " ", "_"
  62.     $filename = $filename -replace ":", "_"
  63.     $attr = $_.filename
  64.     add-content $log1 "Attachment: $attr"
  65.     $a = $_.filename
  66.     If ($a.Contains(".pdf") -and $filename.Contains("Vendor you get reports from or some other qualifying text") ) {
  67.     $filename = $filename + ".pdf"
  68.     Write-Host "filename:" + $filename
  69.     $_.saveasfile((Join-Path $filepath $filename))
  70.                              }
  71.   }
  72. $attachment = "c:\data\report\" + $filename + ".pdf"
  73.  
  74. #added closing brocket -
  75. }
  76.  
  77. ## Note using the rest of this code ##
  78. ########################send email invoice###########################################
  79.  
  80. ##$mail = $outlook.CreateItem(0)  
  81. ##$Mail.Recipients.Add($toaddress1)  
  82. ##$Mail.Subject = "$msubject"  
  83. ##$Mail.Body = ""  
  84. ##$Mail.Attachments.Add($attachment)
  85. ##$Mail.Send()
  86. ##add-content $log1 "Message Sent: $toaddress1"
  87.  
  88. #################################Move Items#########################################
  89.  
  90. ##$MoveTarget = $inbox.Folders.item("Processed")
  91. ##[void]$message.Move($MoveTarget)
  92. ##add-content $log1 "Message Moved to: Processed"
  93. ##$countprocessed = $countprocessed + 1
  94. ##}
  95. ##add-content $log1 "Total Messages Processed: $countprocessed"
  96.  
  97. ###########################Report Errors#############################################
  98.  
  99. ##if ($error -ne $null)
  100. ##{
  101. ##$smtpServer = "SMTP Server"
  102. ##$msg = new-object Net.Mail.MailMessage
  103. ##$smtp = new-object Net.Mail.SmtpClient($smtpServer)
  104. ##$msg.From = "OutlookautomationScript@testlab.com"
  105. ##$msg.To.Add("Vikas.Sukhija@testlab.com")
  106. ##$msg.Subject = "Outlook Script Error"
  107. ##$msg.Body = $error
  108. ##$smtp.Send($msg)
  109. ##}
  110. ##
  111. ##Stop-Transcript
  112. #####################################END################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement