Created
February 12, 2020 15:38
-
-
Save ClaudioESSilva/dfaf1de2e5da88fca1e59f70edd7f4ae to your computer and use it in GitHub Desktop.
New-OutlookEmail.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function New-OutlookEmail { | |
<# | |
.SYNOPSIS | |
Will open a new Outlook email item and set some properties. | |
.DESCRIPTION | |
Will open a new Outlook email item and set some properties. | |
Email will be sign and encrypted. | |
Will keep the default signature. | |
Email will not be sent! | |
.PARAMETER EmailSMTPServer | |
If you have a different SMTP server set it here. | |
Default: mail.company.com | |
.PARAMETER EmailFrom | |
Email address to use as "from". | |
NOTE: Must exists on your Outlook configuration | |
.PARAMETER EmailTo | |
Email address to use as "to". | |
.PARAMETER EmailCC | |
Email address to use as "cc". | |
.PARAMETER EmailSubject | |
Email Subject. | |
.PARAMETER EmailHtmlBody | |
Email message in HTML format. | |
.EXAMPLE | |
$OutlookEmailParams = @{ | |
EmailFrom = "emailFrom@company.com" | |
EmailTo = "someone@company.com" | |
EmailCC = "someoneInCC@company.com" | |
EmailSubject = "This is an email setup automatically" | |
EmailHTMLBody = "Everything you need to say goes here" | |
} | |
} | |
New-OutlookEmail @OutlookEmailParams | |
It will open a new Outlook email window and set the properties. | |
.NOTES | |
Author: Cláudio Sobral Silva (@ClaudioESSilva) | |
#> | |
[CmdletBinding()] | |
param ( | |
[string]$EmailSMTPServer = "mail.company.com", | |
[Parameter(Mandatory)] | |
[string]$EmailFrom, | |
[Parameter(Mandatory)] | |
[string]$EmailTo, | |
[string]$EmailCC, | |
[string]$EmailSubject, | |
[Parameter(Mandatory, ValueFromPipeline)] | |
[string]$EmailHtmlBody, | |
[switch]$Send | |
) | |
begin { | |
function Invoke-SetProperty { | |
# Auxiliar function to set properties. The SendUsingAccount property wouldn't be set in a different way | |
param( | |
[__ComObject] $Object, | |
[String] $Property, | |
$Value | |
) | |
[Void] $Object.GetType().InvokeMember($Property,"SetProperty",$NULL,$Object,$Value) | |
} | |
try { | |
$Outlook = New-Object -ComObject Outlook.Application -Verbose:$false | |
} | |
catch { | |
Write-Warning "Could not create Outlook object. Please try again.`r`nError message: $($error[0])" | |
return | |
} | |
#Get the MAPI namespace. | |
$namespace = $Outlook.GetNameSpace("MAPI") | |
# Log on by using the default profile or existing session (no dialog box). | |
$namespace.Logon($null, $null, $false, $true); | |
} | |
process { | |
Write-Verbose "Setting email properties regading team: $group" | |
# Create new email | |
$Mail = $Outlook.CreateItem(0) | |
$Mail.GetInspector.Activate() | |
#Get account object from email addres | |
$account = $outlook.Session.Accounts.Item($EmailFrom) | |
#Change Sender mailbox | |
Invoke-SetProperty -Object $mail -Property "SendUsingAccount" -Value $account | |
# Get default signature | |
$signature = $Mail.HtmlBody | |
# Fill other properties | |
$Mail.To = $EmailTo | |
if ($EmailCC) { | |
$Mail.Cc = $EmailCC | |
} | |
$Mail.Subject = $EmailSubject | |
$Mail.HtmlBody = $EmailHtmlBody + $signature | |
#Sign and Encrypt email | |
$Mail.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x6E010003", 0x03) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment