Friday, December 21, 2007

Send-SMTPmail update

Version 1.2 of my Send-SMTPmail cmdlet:

# PowerShell 1.0 script to send email through SMTP
# by Jakob Bindslet (, 21. december 2007
# Current Script version 1.1
#    Send-SMTPmail
#    Sends an email by use of an SMTP server
#    Send-SMTPmail -to -from -subject -body
#    [-smtpserver ] [-port ] [-attachment ] [-html]
#    [-cc ] [-bcc ] [-alert] [-timeout ]
#    No specific order of the parameters are required:
#        -to
#        -from
#        -subject
#        -body
#    Optional arguments are:
#    -smtpserver
#        Address of the smtp server to be used. This is only optional if a
#        default    smtpserver has been specified
#    -port
#        port number of the smtp server, used only if it differs from the
#        standard port 25.
#    -attachment
#        Complete path to file to attach, ie. "c:\boot.ini"
#    -cc
#        Recipient(s) to be included as "carbon copy" on the email.
#    -bcc
#        Recipient(s) to be included as "blind carbon copy" on the email.
#    -timeout
#        Sets the timeout in miliseconds, for the send mail operation.
#        This includes the time it take to upload any attachments, so
#        be careful of setting this too low if using attached file.
#    -alert
#        The email will be treatet as an alert (the standard sharepoint
#        alert icon will be used in outlook).
#    -html
#        Indicates that the email will be in HTML format. The HTML
#        content itself should be specified in the -body parameter.
# -------------------------- EXAMPLE 1 --------------------------
# C:\PS>send-SMTPmail -to hans@contoso.msft -from theBoss@contoso.msft
#    -subject "Congratulations!" -smtpserver "smtp.contoso.msft"
#    -body "you'll get one extra week of vacation year"
#    -bcc "theMiddleBoss@contoso.msft"

function Send-SMTPmail($to, $from, $subject, $body, $attachment, $cc, $bcc, $port, $timeout, $smtpserver, [switch] $html, [switch] $alert) {
    if ($smtpserver -eq $null) {$smtpserver = ""}
    $mailer = new-object Net.Mail.SMTPclient($smtpserver)
    if ($port -ne $null) {$mailer.port = $port}
    if ($timeout -ne $null) {$mailer.timeout = $timeout}
    $msg = new-object Net.Mail.MailMessage($from,$to,$subject,$body)
    if ($html) {$msg.IsBodyHTML = $true}
    if ($cc -ne $null) {$$cc)}
    if ($bcc -ne $null) {$msg.bcc.add($bcc)}
    if ($alert) {$msg.Headers.Add("message-id", "<3bd50098e401463aa228377848493927-1>")}
    if ($attachment -ne $null) {
        $attachment = new-object Net.Mail.Attachment($attachment)


Anonymous said...

Thank you SO MUCH for this!!!

Jakob Bindslet said...

You are most welcome Matt.
What are you trying to accomplish using PowerShell?

Boris Gomiunik said...

Dear jakob! This is really looking cool and the usage looks uber-simple :) But forgive my stupid question, because I'm new in using powershell:

How to deploy this to powershell? And can this be used in combination with Csv-import?

Many thanks

Jakob Bindslet said...

In order to "deploy" the function you simply cut-n-paste it into your PowerShell host. The function is now available fot use.
If you need to use this often, I would suggest creating a small library of useful functions, maybe saving this as MyFunctions.ps1, so that you can load all the nice functions easily. Whenever you start PowerShell you simply write the following at the prompt ". .\MyFunctions.ps1" - now the functions will be available.

Boris Gomiunik said...

Jakob, thanks for the help

The function wasn't working, because I forgot to press additional enter after copy-pasting the function into the powershell. (:blush:)

Your script is a lifesaver!

Boris Gomiunik said...

I must say that I like powershell more and more by every minute. By the way: is it possible to modify this script to authenticate against smtp server?

Jakob Bindslet said...

Try using something like this, right before the "$mailer.send($msg)" line:

$myCred = new-object
$myCred.domain = "the domain you want"
$myCred.userName = "username"
$myCred.password = "password"
$mailer.credentials = $myCred

This should work - however I don't have access to a mailserver from my current location, so I haven't really tested it.

Boris Gomiunik said...

Thanks again for help. It doesn't work, but that's probably because I'm trying to use Live ID authentication. Do you think it's possible to set Live ID credentials?

Wayne Hoggett said...

Wow, two years late :P

Anyways, the function provided in the post above is great!

I can send html files as attachments or manually write HTML for the body using you function, but the real power would be in using convertto-html cmdlet to produce the html for the email.

Jakob Bindslet said...

Better late than never Wayne :-D

It should be easy to do something like:

$a = Get-ps | convertto-html

And the use $a as the HTML body of an email. However, I often find that I construct the HTML part by script rather than using convertto-html. Maybe I'm just to picky in my HTML requirements ...

Wayne Hoggett said...

Great suggestions.

I found using convertto-html works ok if you save the output to the file system and use get-content to add it to the body of the email.

Otherwise you're better off creating your own HTML like you said.

The -head parameter for convertto-html was useful in creating well-presented HTML emails using CSS.

Peter J Quinn said...

Just what I had been searching for all day!