I have been working for awhile now to move a lot of my code over to PowerShell. I have found it to be very efficient and easy to read (just took a bit of a mentality change to switch over from VBScript).
In our environment, I have always found it much more reliable to send emails (usually notification emails to end users about expiring passwords or accounts) directly through IBM Lotus Notes instead of just using our SMTP server. Previously, I worked with my boss and we created a project in Visual Studio calling some of the Notes COM objects to send emails. I decided to move all that code over and send the email directly from my PowerShell scripts.
In searching the web, though, I didn’t find too many examples that covered what I wanted to do including sending HTML emails. Today my boss and I finished working up this code that I have created as a PowerShell module (save it as a .psm1 file) that I can now call from my other scripts.
An important note to get this working. First, you must have IBM Notes client installed on the server where you run the script. Second, since it is making a COM call, you must also be running PowerShell in x86. (In newer versions of Windows such as 2012, it does default to x64.)
Here is the code:
Function Send-IBMNotesMail
<#
.Synopsis
Sends Email using IBM Notes.
.Description
This uses a local install of Notes to send emails. It seems that this is a bit more
reliable in our environment than using a regular SMTP server for some reason. It requires
Notes to be installed on the computer that this script is being run on. It calls the
Lotus.NotesSession comobject which requires PowerShell to be running in x86 mode.
.Parameter NotesInstallDir
The directory where Notes is installed. It defaults to C:\Program Files (x86)\IBM\Notes.
.Parameter Password
The password of the ID file to send email.
.Parameter MailFileServer
The server where the mail file resides.
.Parameter MailFile
The mail file to use to send the email.
.Parameter To
The email address you are sending an email to.
.Parameter From
The email address of the sender.
.Parameter Subject
The subject line of the email.
.Parameter Body
The body of the email in HTML format.
.Parameter BCC
(Optional) A BCC recepient.
.Parameter Attachment
(Optional) File to attach.
.Example
# Send an email.
Send-IBMNotesEmail -SenderEmail "joe.bob@gmail.com" -DestinationEmail "joe.bob@outlook.com" -EmailSubject "Hello" -EmailBody "Body of the email" -BCC "joe.smith@outlook.com"
#>
{
[CmdletBinding()]
param(
[Parameter()]
[string]
$NotesInstallDir = "C:\Program Files (x86)\IBM\Notes",
[Parameter()]
[string]
$Password = "password",
[Parameter()]
[string]
[ValidateNotNullOrEmpty()]
$MailFileServer = "mailserver",
[Parameter()]
[string]
[ValidateNotNullOrEmpty()]
$MailFile = "mailfile",
[Parameter(Position=0, Mandatory=$true)]
[string[]]
[ValidateNotNullOrEmpty()]
$To,
[Parameter(Position=1)]
[string]
[ValidateNotNullOrEmpty()]
$From,
[Parameter(Position=2, Mandatory=$true)]
[string]
$Subject,
[Parameter(Position=3, Mandatory=$true)]
[string]
$BodyContent,
[Parameter()]
[string]
$BCC,
[Parameter()]
[string]
$Attachment
)
#For information on the GetDatabase option:
#http://www-12.lotus.com/ldd/doc/domino_notes/rnext/help6_designer.nsf/f4b82fbb75e942a6852566ac0037f284/320b2e0e83e87bca85256c54004d45a1
$Notes = New-Object -ComObject Lotus.NotesSession
$Notes.Initialize("$Password")
$MailDB = $Notes.GetDatabase("$MailFileServer", "$MailFile")
If (-not $MailDB.isopen) {
Write-Host "Couldn't open the Mail Databse...Trying the Cluster Failover Server"
$dbMail = $notes.GetDatabase("ClusterFailoverServer","$MailFile")
}
if($MailDB.isopen)
{
$doc = $MailDB.CreateDocument()
$doc.AppendItemValue("Form","Memo")
$stream = $notes.CreateStream()
$notes.ConvertMime = $false
$body = $doc.CreateMIMEEntity()
$header = $body.CreateHeader("Subject")
$header.SetHeaderVal("$Subject")
$header = $body.CreateHeader("To")
$header.SetHeaderVal("$To")
$header = $body.CreateHeader("From")
$header.SetHeaderVal("$From")
$header = $body.CreateHeader("Principal")
$header.SetHeaderVal("$From")
If ($BCC -ne "") {
$header = $body.CreateHeader("BCC")
$header.SetHeaderVal("$BCC")
}
#This is to get it so it shows up in sent items properly.
$Date = Get-Date
$doc.AppendItemValue("PostedDate",$Date)
$stream.WriteText( $BodyContent)
$body.SetContentFromText($stream,"text/HTML;charset=UTF-8",$ENC_IDENTITY_7BIT)
if ($Attachment -ne "") {
$($doc.CreateRichTextItem("Attachment")).EmbedObject(1454, "", "$Attachment", "Attachment")
}
$doc.Save($true,$False)
$doc.Send($False)
$Notes.ConvertMime=$true
}
}
Export-ModuleMember -Function Send-IBMNotesMail
I hope you find this as a useful way to send emails from your scripts. I’ll come back in a few days with some of my other PowerShell modules that I am working on and how I integrate with this function.