EmailFunctions module contains functions for emailing through PowerShell. Functions are
Get-OutlookRecipient, New-Email, New-HtmlBody, Send-Email (more below). The module is supported forPS 2.0, Office 2010 (Otlook, Word).
Functions are pipelined in order:
Get-Outlookrecipient | New-Email -Body (New-HtmlBody) | Send-Email
Download module: EmailFunctions.zip
Download Video: Video_EmailFunctions.rar
New-HtmlBody
Function New-HtmlBody returns string that contains data in HTML syntax. The data are generated by Word document.
The function runs Word application installed on source system (supported Office 2010).
New document ensures to compose a rich text and data via Word.
The data in HTML code are saved by pressing a button in PowerShell window from the document to temporary file.
The file is loaded and data returned to PS window with possibility to save the file on drive.
It does not support all Word functionality (able to generate HTML code only from something).
New-HtmlBody $htmlcode = New-HtmlBody New-HtmlBody -SaveFileTo "c:\temp\BodyTemp01.html"
Get-OutlookRecipient
The function (Get-OutlookRecipients) returns email addresses from Outlook application.
The purpose is simple: Get email adresses (recipients) from Outlook application (supported 2010).
Required data is possible to get from two locations: Inbox and Contacts folder.
Filters for better selection are:
– Domain (domain part of email address) – available for location Inbox and Contacts
– Company (company attribute) – available for location Contacts
– MessagesFromDate – available for location Inbox
– MessagesToDate – available for location Inbox
Get-OutlookRecipients -Location Contacts Get-OutlookRecipients -Location Contacts -Domain "com" Get-OutlookRecipients -Location Contacts -Domain "gmail.com" -Company "MS" -ExportToCsv C:\ExportContacts.Csv Get-OutlookRecipients -Location Inbox Get-OutlookRecipients -Location inbox -MessagesToDate (get-date) Get-OutlookRecipients -Location inbox -MessagesFromDate "2/5/2012 11:00:00" -MessagesToDate "2/20/2012 12:00:00" -ExportToCsv C:\ExportSenders.Csv
New-Email
The fuction New-Email can create a object of email message including also request for SMTP server based on GUI or specified parameters.
The fuction New-Email can create a object of email message. The object can be defined by GUI or parameters. You can for example specified:
– CcFromTxtFile
– BodyIsHtml
– Port
– CredentilPassword
New-Email -Gui New-Email -Recipient "ficility@gmail.com" -Subject "Hello" -body (New-HtmlBody) -BodyIsHtml -Server "smtp.gmail.com" -Port 587 -CredentilName "ficilityTest@gmail.com" -CredentilPassword "password" New-Email -Recipient "ficilityTest@gmail.com" -CcFromCsvFile "C:\EmailAddresesCsv.csv" -Subject "Hello" -Body "It is test!" -Server "smtp.gmail.com" -Port 465 -CredentilName "ficility@gmail.com" -CredentilPassword "password" | Send-Email -WebMail -ReportAsTxtToFile New-Email -Gui | Send-Email -Outlook
Send-Email
The fuction Send-Email can send a object (from New-Email) by three methods: System.Net.Mail, System.Web.Mail, Outlook.application (COM object).The fuction Send-Email can send a object (from New-Email)by following methods/switches:
– AutoDetection (automatic process – 465 means webmail namespace, port 587 means netmail namespace, no SMTP server means Outlook (with configured profile))
– Other switches mean namespaces: System.Net.Mail, System.Web.Mail, Outlook.application.
– Useful could be ReportAsTxtToFolder (automatic naming), ReportAsTxtToFile.
New-Email -Gui | Send-Email -Outlook Send-Email -NetMail -ReportAsTxtToFolder "C:\Temp" New-Email -Gui | Send-Email -AutoDetection -ReportAsTxtToFile C:\Temp\Report1.txt New-Email -Recipient "ficility@gmail.com" -Subject "Hello" -body (New-HtmlBody) -BodyIsHtml -Server "smtp.gmail.com" -Port 587 -CredentilName "ficilityTest@gmail.com" -CredentilPassword "password" | Send-Email -AutoDetection