Sending files from a directory by mail with the Power Shell

Advertisements
0b3ddacb90f9495080e2ea005f9bb1d8 Sending files from a directory by mail with the Power Shell 2

Today there is again a small article according to the motto “If that doesn’t work, then I’ll do it with the Power Shell”. Often there is the request to send individual files from a folder by mail. I have such a case and decided to share it with you.

Why do I need this?

What’s the matter with you? I bought a new scanner because the document feeder no longer worked with the multifunction printer. After 6 years and missing the duplex scan, I thought that a repair would not lead to the desired result. So I bought an Epson WorkForce DS-1660W. The reasons for me were:

  • WLAN (no USB cable and can be used by several devices)
  • duplex scan
  • OCR
  • Feeding with up to 50 sheets

Also, stand the mail dispatch is supported. Unfortunately not from the device, but only from the software. For the Epson network extension which supports this directly, I didn’t want to spend 499€ extra. Well, a small flaw.

Unfortunately, it turned out that only Microsoft Office in 32-bit is supported by the software. Not an option for me. So I had to find another solution.

Screenshot: Epson Document Capture Pro
Screenshot: Epson Document Capture Pro

The program to scan supports jobs, these can have different target folders and scan profiles. So I create different folders for the different mail targets.

What needs a mail dispatch after the scan for times?

I use that for travel expenses, among other things. The mail saves me from uploading the receipts and I only need to select them.

Another application example would be an application that creates a file and needs to be forwarded.

The Power Shell Script

After the explanation of “why” we now come to the “how”.

Next, we have to select the files first

$sources = Get-ChildItem $sourcepath -Filter $filetype -Depth 0

The next step is to simply send the emails and move the file

Advertisements
ForEach ( $source in $sources)
{
$file = $source.FullName
$SubjectM = $Subject + $source.Name
Send-MailMessage -To $To -From $From -Subject $SubjectM -SmtpServer $SmtpServer -Attachments $file -UseSsl
Move-Item -Path $file -Destination $archivepath
}

What is missing now is the possibility of SMTP authentication. No mail server accepts mail to relay without authentication, apart from internal mail servers working with IP whitelists.

So we still have to integrate the credentials, but first the necessary preparations:

$password = ConvertTo-SecureString $smtppw -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($smtpuser, $password)

The next step is to customize the Send-MailMessage command

Send-MailMessage -To $To -From $From -Subject $SubjectM -SmtpServer $SmtpServer -Attachments $file -Credential $cred -UseSsl

Of course, these code snippets can still be properly included in a script.

Screenshot: ISE with sent-files.ps1
Screenshot: ISE with sent-files.ps1

Of course, I did that and you can download it here.

Advertisements

Note about program and Power Shell Code

The code contained here serves as an example. I do not assume any warranty, guarantee or support for the code or its components. Use the code at your own risk.

I always recommend to have a close look at the scripts before using them.

Run the script as a scheduled task

It is best to set up the script as a scheduled task. It is important that the user you are running the script as has permissions on the directories. Also, think about the frequency of execution and time periods. Does the script have to run every 10 minutes, even at night at 3 am?

You can use the GUI or the Schtasks command to create them.

Note: This article contains Amazon advertising links, I receive compensation for articles purchased over it. The prices remain the same for you.

This article first appeared on Infrastrukturhelden.de in German.

This article is a translation of the Infrastrukturhelden.de article “Dateien aus einem Verzeichnis per Mail verschicken mit der Power Shell” (Published – 2019-07-04). Links may refer to other Infrastrukturhelden.de articles, these may also be available in English language.

Also it can be, that I still use screenshots of German systems. However, where it is possible for me with little effort, I insert screenshots of English systems.