Solved

Send HTML Email and newest file attachment from multiple paths

Posted on 2016-09-16
8
39 Views
Last Modified: 2016-09-19
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)

#################################################################################
0
Comment
Question by:eperez0968
  • 4
  • 4
8 Comments
 
LVL 82

Expert Comment

by:oBdA
ID: 41802223
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

0
 

Author Comment

by:eperez0968
ID: 41802260
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
0
 
LVL 82

Expert Comment

by:oBdA
ID: 41802284
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

0
 

Author Comment

by:eperez0968
ID: 41802319
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
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 82

Expert Comment

by:oBdA
ID: 41802343
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

0
 

Author Comment

by:eperez0968
ID: 41804849
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)

#################################################################################
0
 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
ID: 41805374
You're using the same variable for both results, so the later command overwrites what the earlier did.
You need something like
$LatestTest = Get-ChildItem  E:\Test\logs ...
$LatestMain = Get-ChildItem  E:\MainLogs ...

Open in new window

and then
$attach = new-object Net.Mail.Attachment($LatestTest)
$message.Attachments.Add($attach)
$attach = new-object Net.Mail.Attachment($LatestMain)
$message.Attachments.Add($attach)

Open in new window

0
 

Author Closing Comment

by:eperez0968
ID: 41805481
Awesome, that did it
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
This article will help you understand what HashTables are and how to use them in PowerShell.
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now