E=mc2
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
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>'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.