Powershell Emailing Strings as attachments

I was working on a project to send a html attachment in a powershell script.  I have the HTML string so all I want to do is create an attachment from the string.  Here's the code:

 

$SMTPserver = "mail.company.com"
$from = "from@company.com"
$to = "to@company.com"
$subject = "Test PS Attachment"
$emailbody = "test"
$attText = "The text of the attachment"
$attName = "Test.txt"

 
$mailer = new-object Net.Mail.SMTPclient($SMTPserver)
$msg = new-object Net.Mail.MailMessage($from, $to, $subject, $emailbody)
 
$att = [System.Net.Mail.Attachment]::CreateAttachmentFromString($attText,$attName)
 
 $msg.Attachments.Add($att)

$msg.IsBodyHTML = $false
$mailer.send($msg)

That should get you started.

Print | posted on Thursday, January 19, 2012 2:39 PM