Solved

Send HTML Email and newest file attachment from multiple paths

Posted on 2016-09-16
8
53 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 84

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 84

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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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 84

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 84

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

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Are you irritated by repeating emails issue in Microsoft Outlook 2016 after recent update ?  Lets’ see how to resolve and prevent duplicate emails in the Outlook 2016 using some simple techniques.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

697 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