Link to home
Start Free TrialLog in
Avatar of E=mc2
E=mc2Flag for Canada

asked on

Change Powershell script to also send an attachment

I would like to change this Powershell script to also include sending an attachment at
C:\Users\user1\File\body.docx

#Variables to configure to fit your own environment
$Office365Username = "youradminemailaddress@yourdomain.com"
$MailServer = "yourdomain-com.mail.protection.outlook.com"


#Some re-usable functions

function SendEmailWithBodyFromAFile
{ 
    param([string] $pSendTo, [string]$pSubject, [string]$htmlFilename)

    $smtp = new-object system.net.mail.smtpclient $MailServer
    $msg = new-object system.net.mail.mailmessage $office365username, $pSendTo, $pSubject, (get-content $htmlFilename)
    $msg.isbodyhtml = $true
    $smtp.send($msg)
}

function SendEmail
{ 
    param([string] $pSendTo, [string]$pSubject, [string]$htmlBody)

    $smtp = New-Object System.Net.Mail.SmtpClient $MailServer
    $msg = New-Object System.Net.Mail.MailMessage $Office365Username, $pSendTo, $pSubject, $htmlBody
    $msg.isBodyhtml = $true
    $smtp.send($msg)
}


#if you want to test just this script, uncomment the following line
#SendEmail 'yourtestemailaddress@yourdomain.com' 'test subject' '<body><h1>Hello from Office 365</h1></body>'


Open in new window


ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial