Avatar of E=mc2
E=mc2Flag for Canada

asked on 

Modify Powershell script to send .html attachments

I would like to modify the script so that any html files it finds at this path:
"C:\Users\User1\Desktop\Path
that start with "Desktop-123-Files.... "...
Are sent as attachments with the email.

If there are 2 or 3 html files that all start with the designation above, they should be sent as attachments.
Is this possible?

#Send Email 
$EncryptedCredential = "C:\creds\creds.txt" 
$EmailUsername = "email1@email.com" 
$EncryptedPW = Get-Content $EncryptedCredential | ConvertTo-SecureString 
$Credential = New-Object System.Management.Automation.PsCredential($EmailUsername, $EncryptedPW) 
$EmailFrom = "email1@email.com" 
$EmailTo = "email1@email.com"
$SMTPServer = "smtp.smtp.com" 
$SMTPPort = 123
$subject = "Files Processed"
 
$SMTPSsl = $true 
 
$param = @{ 
    SmtpServer = $SMTPServer 
    Port = $SMTPPort 
    UseSsl = $SMTPSsl      
    Credential = $Credential     
    From = $EmailFrom      
    To = $EmailTo   
    Subject = $subject

 
Send-MailMessage @param 

Open in new window

PowershellEmail ClientsScripting Languages

Avatar of undefined
Last Comment
oBdA
Avatar of Robert
Robert
Flag of United States of America image

Send-mailmessage accepts an array of stings for the file attachments so you would need to pass it an array.
for example
$File1 = "C:\Users\User1\Desktop\Path\test.html"
$File2 = "C:\Users\User1\Desktop\Path\test2.html"
[array]$Attachment=$File1,$File2

Open in new window

So first you would search the directory for the name pattern your looking for and pass the returned values into an array that you use for attachments .
Then add the attachments parameter to your script then set attachments to the array.
$param = @{ 
    SmtpServer = $SMTPServer 
    Port = $SMTPPort 
    UseSsl = $SMTPSsl      
    Credential = $Credential     
    From = $EmailFrom      
    To = $EmailTo   
    Subject = $subject
    Attachments = $Attachment 

Open in new window


ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Scripting Languages
Scripting Languages

A scripting language is a programming language that supports scripts, programs written for a special run-time environment that automate the execution of tasks that could alternatively be executed one-by-one by a human operator. Scripting languages are often interpreted (rather than compiled). Primitives are usually the elementary tasks or API calls, and the language allows them to be combined into more complex programs. Environments that can be automated through scripting include software applications, web pages within a web browser, the shells of operating systems (OS), embedded systems, as well as numerous games. A scripting language can be viewed as a domain-specific language for a particular environment; in the case of scripting an application, this is also known as an extension language.

30K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo