Link to home
Start Free TrialLog in
Avatar of rakkad
rakkadFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Powershell script that will send an email with a specific attachment file

Hello

I have a partly powershell script that sends an email from a csv file that contains email addresses

However, i need to extend this powershell script that will do the following:-

I have a folder that contains the following files:-

nbs-royal.pdf
nbs-coral.pdf
nbs-corporate.pdf

So what should happen is:-

a) I need to search the folders for files called *.royal.pdf, *.coral-pdf, *.corporate.pdf
b) once these files are found, the files should be sent indvidually to three seperate emails as attachment, as follows:-

nbs-royal.pdf file should be sent to royal@co.uk
nbs-coral.pdf should be sent to coral@co.uk
nbs-corporate.pdf should be sent to corporate@co.uk

Thanks
Avatar of Nagendra Pratap Singh
Nagendra Pratap Singh
Flag of Australia image

A batch file will not work for you? I used a batch file based system for years.
There is an example here

Send an email with an attachment to two users and BCC another:

C:\PS> send-mailmessage -from "me@example.com" -to "user01@example.com", "user02@example.com" -bcc helpdesk@example.com -subject "Hello World" -body "See attachment below." -Attachment "data.csv" -smtpServer smtp.example.com

https://ss64.com/ps/send-mailmessage.html
Easy enough.. something like this should do it for you..
$RoyalSMTPTo = 'royal@co.uk'
$RoyalSMTPSubject = 'Located NBS-Royal.pdf'

$CoralSMTPTo = 'coral@co.uk'
$CoralSMTPSubject = 'Located NBS-Coral.pdf'

$CorpSMTPTo = 'corporate@co.uk'
$CorpSMTPSubject = 'Located NBS-Corporate.pdf'

$SMTPServer = 'smtp.co.uk'

$Royal = get-childitem -path c:\* -include "*royal.pdf"
$Coral = get-childitem -path c:\* -include "*coral.pdf"
$Corp = get-childitem -path c:\* -include "*corporate.pdf"

$RoyalBody = "Attached is/are the file(s):`r`n"
foreach ($file in $royal)
{
     $RoyalBody += "$($file.FullName)`r`n"
}
send-mailmessage -To $RoyalSMTPTo -From 'NBS-PDFScript@co.uk' -SMTPServer -Attachment $Royal -Body $RoyalBody  -Subject $RoyalSMTPSubject

$CoralBody = "Attached is/are the file(s):`r`n"
foreach ($file in $Coral)
{
     $CoralBody += "$($file.FullName)`r`n"
}
send-mailmessage -To $CoralSMTPTo -From 'NBS-PDFScript@co.uk' -SMTPServer -Attachment $Coral -Body $CoralBody  -Subject $CoralSMTPSubject

$CorpBody = "Attached is/are the file(s):`r`n"
foreach ($file in $Corp)
{
     $CorpBody += "$($file.FullName)`r`n"
}
send-mailmessage -To $CorpSMTPTo -From 'NBS-PDFScript@co.uk' -SMTPServer -Attachment $Corp -Body $CorpBody  -Subject $CorpSMTPSubject

Open in new window

Avatar of rakkad

ASKER

Can you explain line by line how this script works please
ASKER CERTIFIED SOLUTION
Avatar of Coralon
Coralon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rakkad

ASKER

This solved my problem