Solved

Send HTML Email and newest file attachment from multiple paths

Posted on 2016-09-16
8
46 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 83

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 83

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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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 83

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 83

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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…
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…

809 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