some particular parameters required while sending an email

I am trying to make script in which I want that a email will shoot to the recipient if it will get some .xml files in particular folder or if not then at the end of the day it will shoot an email to recipient with the message that “no file found”. The whole script i am pasting below but i am not getting an idea how to adjust the second path in script that is if “files not found”

$EmailFrom = “”
$EmailTo = “”
$EmailSubject = “”
$Emailbody = “”

$SMTPServer = “xx.basware.com

$emailattachment = “C:\Program Files (x86)\TIE Kinetix B.V\TIE Digipoort WUS Service\error*.xml”

function send_email {
$mailmessage = New-Object system.net.mail.mailmessage
$mailmessage.from = ($emailfrom)
$mailmessage.To.add($emailto)
$mailmessage.Subject = $emailsubject
$mailmessage.Body = $emailbody

$attachment = New-Object System.Net.Mail.Attachment($emailattachment, ‘text/plain’)
$mailmessage.Attachments.Add($attachment)

#$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.Send($mailmessage)
}

you could try

If (test-path $emailattatchment)
{
$emailsubject = “Today’s XML File”
$attachment = New-Object System.Net.Mail.Attachment($emailattachment, ‘text/plain’)
$mailmessage.Attachments.Add($attachment)
}
Else
{
$emailsubject = “No File Found”
}

Hi Simon,

Thanks buddy for your reply but can you please adjust your content in my script as i am not able to fit the content in my script and getting error.

Thanks
Ankit

think this should work

$EmailFrom = “”
$EmailTo = “”
$EmailSubject = “”
$Emailbody = “”

$SMTPServer = “xx.basware.com

$emailattachment = “C:\Program Files (x86)\TIE Kinetix B.V\TIE Digipoort WUS Service\error*.xml”

function send_email {
$mailmessage = New-Object system.net.mail.mailmessage
$mailmessage.from = ($emailfrom)
$mailmessage.To.add($emailto)
$mailmessage.Subject = $emailsubject
$mailmessage.Body = $emailbody

If (test-path $emailattatchment)
{
$emailsubject = “Today’s XML File”
$attachment = New-Object System.Net.Mail.Attachment($emailattachment, ‘text/plain’)
$mailmessage.Attachments.Add($attachment)
}
Else
{
$emailsubject = “No File Found”
}
#$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.Send($mailmessage)
}

Hi Simon,

Thanks for your reply but I have tried it and it didn’t work although it is not giving error on execution but neither giving any result. Do you have any idea why it is happening.

Thanks
Ankit

Hi Ankit,

Please check below script if it works for you.

$EmailFrom = “”
$EmailTo = “”
$EmailSubject = “”
$Emailbody = “”

$SMTPServer = “xx.basware.com
function send_email {
$mailmessage = New-Object system.net.mail.mailmessage
$mailmessage.from = ($emailfrom)
$mailmessage.To.add($emailto)
$mailmessage.Subject = $emailsubject
$mailmessage.Body = $emailbody

#Checks if the file exists and returns True or false accordingly
$emailattachment = Test-Path -Path “C:\Program Files (x86)\TIE Kinetix B.V\TIE Digipoort WUS Service\error*.xml”

If ($emailattachment -eq “True”)
{
$emailsubject = “Today’s XML File”
$attachment = New-Object System.Net.Mail.Attachment($emailattachment, ‘text/plain’)
$mailmessage.Attachments.Add($attachment)
}
Else
{
$emailsubject = “No File Found”
}
#$mailmessage.IsBodyHTML = $true
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.Send($mailmessage)
}

Hi,

Thanks for replying but issue remains the same. It is executing but neither sending any email nor giving any error on console. Just don’t understand where I am doing wrong.

Thanks
Ankit

how are you calling the function (function send_email) ?

what I’ve did is just created two different functions for sending email and those are called accordingly on the if loop trigger, I understand this can be done with the a single function by passing parameter to the function dynamically, if this work you can go ahead and create a single function for the sending emails.

$EmailFrom = “”
$EmailTo = “”
$EmailSubject = “”
$Emailbody = “”
$SMTPServer = “xx.basware.com
$emailattachment = “C:\Program Files (x86)\TIE Kinetix B.V\TIE Digipoort WUS Service\error*.xml”

function Send-email_File {
$mailmessage = New-Object system.net.mail.mailmessage
$mailmessage.from = ($emailfrom)
$mailmessage.To.add($emailto)
$mailmessage.Subject = “Today’s XML File details - Found”
$mailmessage.Body = "Attached are the files which are in the Queue "
$attachment = New-Object System.Net.Mail.Attachment($emailattachment, ‘text/plain’)
$mailmessage.Attachments.Add($attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.Send($mailmessage)
}

function Send-email_NOfile {
$mailmessage = New-Object system.net.mail.mailmessage
$mailmessage.from = ($emailfrom)
$mailmessage.To.add($emailto)
$mailmessage.Subject = “Today’s XML File details- Not Found”
$mailmessage.Body = “No file is in the Queue”
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.Send($mailmessage)
}

#Checks if the file exists and returns True or false accordingly
$emailattachment = Test-Path -Path “C:\Program Files (x86)\TIE Kinetix B.V\TIE Digipoort WUS Service\error*.xml”

If ($emailattachment -eq “True”)
{
Send-email_File
}
Else
{
Send-email_NOfile
}

Hey…

thanks a ton for your help and reply. Finally script is running and I got emails in my mailbox but the problem is attachments are missing in the email and giving some error which I pasted below. Whereas files are available in the folder which i have mentioned in attachment path in script. Second part is running fine i.e. when I remove those files from the folder then script is running without giving any error and got the right email with the right content i.e. “No file is in the Queue”


Error

New-object : Cannot convert argument “0”, with value: “True”, for “Attachment” to type “System.IO.Stream”: “Cannot convert
value “True” to type “System.IO.Stream”. Error: “Invalid cast from ‘System.Boolean’ to ‘System.IO.Stream’.””
At C:\Users\ankitpar\Downloads\Untitled8.ps1:12 char:15

  • … ttachment = New-object System.Net.Mail.Attachment($emailattachment, ’ …
  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (:slight_smile: [New-Object], MethodException
    • FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

Exception calling “Add” with “1” argument(s): “Value cannot be null.
Parameter name: item”
At C:\Users\ankitpar\Downloads\Untitled8.ps1:13 char:1

  • $mailmessage.Attachments.Add($attachment)
  •   + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : ArgumentNullException
    
    
    

Thanks
Ankit

You can try by changing the variable name to $files as below.

  1. $files = “C:\Program Files (x86)\TIE Kinetix B.V\TIE Digipoort WUS Service\error*.xml”

  2. $attachment = New-Object System.Net.Mail.Attachment($file, ‘text/plain’)

Nopes… still issue remain the same.

You can try below Script.

Note:- I have used Sendgrid for sending out emails, this is working fine for me.

function Send-email_File{
Process{
$File = “C:\Program Files (x86)\TIE Kinetix B.V\TIE Digipoort WUS Service\error*.xml”
$sub=“Today’s XML File details – Found”
$body=“Attached are the files which are in the Queue”
$To=“”
$pass = ConvertTo-SecureString “” -AsPlainText -Force
$smtpServer = “”
$user = “”
$credential = New-Object System.Management.Automation.PsCredential($user, $pass)

    Send-MailMessage -From "test@mydomain.com" -To $To -Subject $sub -Body $body -Attachments $file -SmtpServer $smtpServer -Credential $credential -UseSsl -Port 587

}
}

function Send-email_NOfile {

    $sub="Today's XML File details – Not Found"
    $body="No file in the Queue"
    $To=""
    $pass = ConvertTo-SecureString "" -AsPlainText -Force
    $smtpServer = ""
    $user = ""
    $credential = New-Object System.Management.Automation.PsCredential($user, $pass)

    Send-MailMessage -From "test@mydomain.com" -To $To -Subject $sub -Body $body -SmtpServer $smtpServer -Credential $credential -UseSsl -Port 587

}

#Checks if the file exists and returns True or false accordingly
$emailattachment = Test-Path -Path “C:\Program Files (x86)\TIE Kinetix B.V\TIE Digipoort WUS Service\error*.xml”

If ($emailattachment -eq “True”)
{
Send-email_File
}
Else
{
Send-email_NOfile
}

If you have SMTP server in place please refer below SMTP script and change the script accordingly

function Send-email{
Process{
$File = “C:*txt”
$sub=“”
$body=“”
$To=“”
$smtpServer = “”
Send-MailMessage -From “test@mydomain.com” -To $To -Subject $sub -Body $body -Attachments $file -SmtpServer $smtpServer
}
}
Send-email

hope this would help you in some way. :slight_smile:

Good morning,

Thanks for your extensive help but few questions here -

In below mentioned script lines, in first point do I need to put my local LAN user id in parameter or email id? In second point do I need to put my LAN password or email password?

  1. $user = “”
  2. $credential = New-Object System.Management.Automation.PsCredential($user, $pass)

Thanks
Ankit

As I mentioned I checked sending out email via sendgrind (https://sendgrid.com/)

$user = “”

But if you have SMTP server in place you can use below script, just replace the function with the required changes.

function Send-email{
Process{
$File = “C:*txt”
$sub=“”
$body=“”
$To=“”
$smtpServer = “”
Send-MailMessage -From “test@mydomain.com” -To $To -Subject $sub -Body $body -Attachments $file -SmtpServer $smtpServer
}
}
Send-email

Well as you suggested I have applied the second function in my script as we have smtp server. The script is given below and again it is running fine without giving any error but the issue is again the attachment is missing in email and the second function is working and I got the email “No file in the Queue” whereas xml files are placed ini folder. Any insight would be appreciated. Thanks in advance

function Send-email_File{
Process{
$File = “C:\Users\ankitpar\Desktop\Digipoort*.xml”
$sub=“Today’s XML File details – Found”
$body=“Attached are the files which are in the Queue”
$To="ankit.parmar@basware.com"
$smtpServer = “mail.basware.com
Send-MailMessage -From “ankit.parmar@basware.com” -To $To -Subject $sub -Body $body -Attachments $File -SmtpServer $smtpServer
}
}

function Send-email_NOfile {
Process{
$File = “C:\Users\ankitpar\Desktop\Digipoort*.xml”
$sub=“Today’s XML File details – Not Found”
$body=“No file in the Queue”
$To="ankit.parmar@basware.com"
$smtpServer = “mail.basware.com
Send-MailMessage -From “ankit.parmar@basware.com” -To $To -Subject $sub -Body $body -SmtpServer $smtpServer
}
}

If ($File -eq “True”)
{
Send-email_File
}
Else
{
Send-email_NOfile
}

Thanks
Ankit

I have tried it in other way. Please find the below script but this time getting error of “Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response
was: 5.7.1 Client was not authenticated” . Any suggestions … Thanks

$File = Test-Path C:\Users\ankitpar\Desktop\Digipoort*.xml
$fpath=“C:\Users\ankitpar\Desktop\Digipoort*.xml”
$secpasswd = ConvertTo-SecureString “xxxx” -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential (“abc@domain.com”, $secpasswd)
$smtpServer = “xyz.domain.com
$To="abc@domain.com"

function Send-email_File{
Process{

$sub=“Today’s XML File details – Found”
$body=“Attached are the files which are in the Queue”

Send-MailMessage -From “abc@domain.com” -To $To -Subject $sub -Body $body -Attachments $fpath -SmtpServer $smtpServer -Credential $mycreds -UseSsl -Port “587”

}
}

function Send-email_NOfile {
Process{

$sub=“Today’s XML File details – Not Found”
$body=“No file in the Queue”

Send-MailMessage -From “abc@domain.com” -To $To -Subject $sub -Body $body -UseSsl -SmtpServer $smtpServer -Credential $mycreds -Port “587”
}
}

If ($File)
{
Send-email_File
}
Else
{
Send-email_NOfile
}

try this

function Send-email_File{
Process{
$Files = get-ChildItem “C:\Users\ankitpar\Desktop\Digipoort*.xml”
$attachments = @()

Foreach($file in $files){
		$filename =$file.FullName			
		$attachments += $filename 
	}

$sub=“Today’s XML File details – Found”
$body=“Attached are the files which are in the Queue”
$To="ankit.parmar@basware.com"
$smtpServer = “mail.basware.com
Send-MailMessage -From “ankit.parmar@basware.com” -To $To -Subject $sub -Body $body -Attachments $attachments -SmtpServer $smtpServer
}
}

Thanks Simon for replying but by execution of script which you have provided I didn’t get any email in my inbox though it’s executed without any error.

what happens when you run :-

function Send-email_File{
Process{
$Files = get-ChildItem “C:\Users\ankitpar\Desktop\Digipoort*.xml”
$attachments = @()

Foreach($file in $files){
		$filename =$file.FullName			
		$attachments += $filename 
		Write-host "Adding file  " $attachments  #  for testing only
	}

$sub=“Today’s XML File details – Found”
$body=“Attached are the files which are in the Queue”
$To="ankit.parmar@basware.com"
$smtpServer = “mail.basware.com
Send-MailMessage -From “ankit.parmar@basware.com” -To $To -Subject $sub -Body $body -Attachments $attachments -SmtpServer $smtpServer
}
}
function Send-email_NOfile {
Process{

$sub="Today's XML File details – Not Found"
$body="No file in the Queue"
$To="ankit.parmar@basware.com"
$smtpServer = "mail.basware.com"
Send-MailMessage -From "ankit.parmar@basware.com" -To $To -Subject $sub -Body $body -SmtpServer $smtpServer

}
}

$path = Test-Path -path “C:\Users\ankitpar\Desktop\Digipoort*.xml”

If ($path -eq “True”)
{
Send-email_File
}
Else
{
Send-email_NOfile
}