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
- 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.
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
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.
Of course, I did that and you can download it here.
Note about program and Power Shell Code
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.