Solved

Send HTML Email and newest file attachment from multiple paths

Posted on 2016-09-16
8
56 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 85

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 85

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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

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
 
LVL 85

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 85

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

717 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