Avatar of Eric Perez
Eric Perez
Flag for United States of America asked on

Send HTML Email and newest file attachment from multiple paths

i got this script from Microsoft technet..

I need it to email and attach the NEWEST FIle or recent modified file in the folder path...

###############################################################################

###########Define Variables########

$fromaddress = "donotreply@labtest.com"
$toaddress = "Aishwarya.Rawat@labtest.com"
$bccaddress = "Vikas.sukhija@labtest.com"
$CCaddress = "Mahesh.Sharma@labtest.com"
$Subject = "ACtion Required"
$body = get-content .\content.htm
$attachment = "C:\sendemail\test.txt"
$smtpserver = "smtp.labtest.com"

####################################

$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
$message.CC.Add($CCaddress)
$message.Bcc.Add($bccaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)

#################################################################################
PowershellEmail ClientsHTML

Avatar of undefined
Last Comment
Eric Perez

8/22/2022 - Mon
oBdA

To get the latest file from a folder into $attachment, you can use this:
$attachment = Get-ChildItem C:\sendemail -Filter *.txt | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName

Open in new window

Eric Perez

ASKER
i am getting this error

this is the change i did with you suggestion... attached is the error i get....

###############################################################################

###########Define Variables########

$fromaddress = "donotreply@labtest.com"
$toaddress = "Aishwarya.Rawat@labtest.com"
$bccaddress = "Vikas.sukhija@labtest.com"
$CCaddress = "Mahesh.Sharma@labtest.com"
$Subject = "ACtion Required"
$body = get-content .\content.htm
$attachment = Get-ChildItem E:\Extranet\batchProcess\logs -Filter *.txt | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName
$smtpserver = "smtp.labtest.com"

####################################

$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
$message.CC.Add($CCaddress)
$message.Bcc.Add($bccaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)

#################################################################################
Error.png
oBdA

Any specific reason you're not using Send-MailMessage?
###########Define Variables########

$fromaddress = "donotreply@labtest.com"
$toaddress = "Aishwarya.Rawat@labtest.com"
$bccaddress = "Vikas.sukhija@labtest.com"
$CCaddress = "Mahesh.Sharma@labtest.com"
$Subject = "ACtion Required"
$body = get-content .\content.htm
$attachment = Get-ChildItem C:\sendemail -Filter *.txt | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName
$smtpserver = "smtp.labtest.com"

Send-MailMessage -From $fromaddress -To $toaddress -Bcc $bccaddress -Cc $CCaddress -Body $body -Attachments $attachment -SmtpServer $smtpserver
####################################

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Eric Perez

ASKER
i tried your new solution and this is what comes up.. attached is the error..

###########Define Variables########

$fromaddress = "TestEmail@dycominc.com"
$toaddress = "Eric.Perez@dycominc.com"
$bccaddress = "Eric.Perez@dycominc.com"
$CCaddress = "Eric.Perez@dycominc.com"
$Subject = "TEST-REPORT"
$body = get-content .\content.htm
$attachment = Get-ChildItem "E:\Extranet\batchProcess\logs" -Filter *.txt | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName
$smtpserver = "smtp.dynutil.com"

Send-MailMessage -From $fromaddress -To $toaddress -Bcc $bccaddress -Cc $CCaddress -Body $body -Attachments $attachment -SmtpServer $smtpserver
####################################
Error2.png
oBdA

Replace the $body line with this: (that is, add -Raw to the Get-Content arguments.
$body = get-content .\content.htm -Raw

Open in new window

Eric Perez

ASKER
ok got everything to work.. just one thing, I cant sent multiple attachment it only sends the second attachment...  here is the code.

###########Define Variables########

$fromaddress = "Server@dycominc.com"
$toaddress = "User@dycominc.com"
#$bccaddress = "example@test.com"
#$CCaddress = "example2@test.com"
$Subject = "Server-Logs"
$body = get-content .\content.htm
$attachment = Get-ChildItem E:\Test\logs -Filter *.html | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName
$attachment = Get-ChildItem E:\MainLogs -Filter *.html | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName
$smtpserver = "smtp.Company.com"

####################################

$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
#$message.CC.Add($CCaddress)
#$message.Bcc.Add($bccaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
$attach = new-object Net.Mail.Attachment($attachment)
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)

#################################################################################
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION 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
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Eric Perez

ASKER
Awesome, that did it