Pickup folder

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
User avatar
jimimaseye
Moderator
Moderator
Posts: 8587
Joined: 2011-09-08 17:48

Pickup folder

Post by jimimaseye » 2018-09-16 23:29

I've been thinking. Here is a challenge for one of our regular scriptors if you have the time.

Subject: a pick up folder.

How difficult can it be.

M.O: script looks in a pickup folder and for each email it finds a new message is created. The headers and body contents are copied from the original (for headers... copy... next ) and the from and recipient taken from the headers. Then submit. (Then original message is moved or deleted).

Sounds simple, right?

Anyone willing to have a bash (i don't really have time unfortunately) as I'm sure it will be handy to some people.

(I don't need it but was thinking about it for others in the future).
5.7 on test.
SpamassassinForWindows 3.4.0 spamd service
AV: Clamwin + Clamd service + sanesecurity defs : https://www.hmailserver.com/forum/viewtopic.php?f=21&t=26829

palinka
Senior user
Senior user
Posts: 1942
Joined: 2017-09-12 17:57

Re: Pickup folder

Post by palinka » 2018-09-17 02:22

Is this something that can be done with blat? I'll have a look at the documentation.

User avatar
jimimaseye
Moderator
Moderator
Posts: 8587
Joined: 2011-09-08 17:48

Re: Pickup folder

Post by jimimaseye » 2018-09-17 08:54

palinka wrote:
2018-09-17 02:22
Is this something that can be done with blat? I'll have a look at the documentation.
Good question. Not thought of that and I dont actually know the answer. Let us know if you have any success.
5.7 on test.
SpamassassinForWindows 3.4.0 spamd service
AV: Clamwin + Clamd service + sanesecurity defs : https://www.hmailserver.com/forum/viewtopic.php?f=21&t=26829

palinka
Senior user
Senior user
Posts: 1942
Joined: 2017-09-12 17:57

Re: Pickup folder

Post by palinka » 2018-09-17 10:13

I know that powershell can look for new files. It's possible for the whole thing to be automated.

palinka
Senior user
Senior user
Posts: 1942
Joined: 2017-09-12 17:57

Re: Pickup folder

Post by palinka » 2018-09-17 11:27

I use this powershell script to send received faxes. It runs from task scheduler at startup. I don't know how to extract to, from and subject line from eml files that would be dropped into the pickup folder.

However, another possible use for the pickup folder is to send dropped files as attachments, which this script does. That's not the original intention, of course, but potentially useful nonetheless.

And I'm glad I had a look at this. I rarely get faxes. I had a pay fax service for a long time but decided not to pay monthly for the 3 or 4 faxes a year I get. Anyway, I'm glad I looked because I have since required authentication on localhost and this guy won't pass muster, so when I have time I'll get it working and repost. By the way, I'm using this on windows 10, for which fax server is not built in.

Code: Select all

$MonitorFolder = "X:\Faxes\PDF" 
$MonitorStopFile = "end.Mon" 
$smtpServer = "localhost" 
$smtpFrom = "fax@mydomain.tld"  
$smtpTo = "me@mydomain.tld" 
$smtpSubject = "A new fax has arrived." 
 
$SourceID = "MonitorFiles" 
$Query = @" 
SELECT * FROM __InstanceCreationEvent WITHIN 10 
WHERE targetInstance ISA 'Cim_DirectoryContainsFile' 
AND targetInstance.GroupComponent = 'Win32_Directory.Name="$($MonitorFolder.Replace("\", "\\\\"))"' 
"@ 
Try { 
    $smtp = New-Object -TypeName "Net.Mail.SmtpClient" -ArgumentList $smtpServer 
    Register-WmiEvent -Query $Query -SourceIdentifier $SourceID 
    Do { 
        "Waiting for a new file to arrive in '$($MonitorFolder)'; to stop, hit <Ctrl-C> or create a file '$MonitorStopFile'." | Write-Host 
        $FileEvent = Wait-Event -SourceIdentifier $SourceID 
        Remove-Event -EventIdentifier $FileEvent.EventIdentifier 
        $FileName = $FileEvent.SourceEventArgs.NewEvent.TargetInstance.PartComponent.Split("=", 2)[1].Trim('"').Replace("\\", "\") 
        If ((Split-Path -Path $FileName -Leaf) -eq $MonitorStopFile) { 
            $smtpBody = "[$(Get-Date -Format HH:mm:ss)]`tStop file arrived: '$($FileName)'; monitor is going down!" 
            Remove-Item -Path (Join-Path -Path $MonitorFolder -ChildPath $MonitorStopFile) 
            $FileEvent = $Null 
        } Else { 
            $smtpBody = "[$(Get-Date -Format HH:mm:ss)]`tA new fax has arrived: '$($FileName)'" 
             
        } 
        $smtpBody | Write-Host -Fore Yellow 
        Send-MailMessage -SmtpServer $smtpServer -To $smtpTo -From $smtpFrom -Subject $smtpSubject -Body $smtpBody -Attachments $FileName 
    } While ($FileEvent) 
} Catch { 
    $_ | Out-String | Write-Error 
} Finally { 
    Remove-Event -SourceIdentifier $SourceID -ErrorAction SilentlyContinue 
    Unregister-Event -SourceIdentifier $SourceID -ErrorAction SilentlyContinue 
}

palinka
Senior user
Senior user
Posts: 1942
Joined: 2017-09-12 17:57

Re: Pickup folder

Post by palinka » 2019-11-09 04:24

I was poking around and saw this was never updated. Version below will authenticate and also use ssl. It can connect to any mail server now.

Looks for NEW (not modified) files and emails them. Delete "-UseSsl" if you don't want encrypted connection.

Powershell:

Code: Select all

### Email Variables ###############################
                                                  #
$smtpSubject        = "File Found: Pickup Folder" #
$smtpFrom           = "notify@mydomain.tld"       #
$smtpTo             = "user@yourdomain.tld"       #
$smtpServer         = "localhost"                 #
$SMTPAuthUser       = "notify@mydomain.tld"       #
$SMTPAuthPass       = "supersecretpassword"       #
                                                  #
### File Variables ################################
                                                  #
$MonitorFolder      = "C:\scripts\pickup\pickup"  #
$MonitorStopFile    = "end.Mon"                   #
                                                  #
###################################################


$SourceID = "MonitorFiles" 
$Query = @" 
SELECT * FROM __InstanceCreationEvent WITHIN 10 
WHERE targetInstance ISA 'Cim_DirectoryContainsFile' 
AND targetInstance.GroupComponent = 'Win32_Directory.Name="$($MonitorFolder.Replace("\", "\\\\"))"' 
"@ 
Try { 
    $smtp = New-Object -TypeName "Net.Mail.SmtpClient" -ArgumentList $smtpServer 
    Register-WmiEvent -Query $Query -SourceIdentifier $SourceID 
    Do { 
        "Waiting for a new file to arrive in '$($MonitorFolder)'; to stop, hit <Ctrl-C> or create a file '$MonitorStopFile'." | Write-Host 
        $FileEvent = Wait-Event -SourceIdentifier $SourceID 
        Remove-Event -EventIdentifier $FileEvent.EventIdentifier 
        $FileName = $FileEvent.SourceEventArgs.NewEvent.TargetInstance.PartComponent.Split("=", 2)[1].Trim('"').Replace("\\", "\") 
        If ((Split-Path -Path $FileName -Leaf) -eq $MonitorStopFile) { 
            $smtpBody = "[$(Get-Date -Format HH:mm:ss)]`tStop file arrived: '$($FileName)'; monitor is going down!" 
            Remove-Item -Path (Join-Path -Path $MonitorFolder -ChildPath $MonitorStopFile) 
            $FileEvent = $Null 
        } Else { 
            $smtpBody = "[$(Get-Date -Format HH:mm:ss)]`tA new fax has arrived: '$($FileName)'" 
             
        } 
        $smtpBody | Write-Host -Fore Yellow 
		$SecPasswd = ConvertTo-SecureString $SMTPAuthPass -AsPlainText -Force
		$Cred = New-Object System.Management.Automation.PSCredential ($SMTPAuthUser, $SecPasswd)
        Send-MailMessage -SmtpServer $smtpServer -To $smtpTo -From $smtpFrom -Credential $Cred -Port 587 -Subject $smtpSubject -Body $smtpBody -Attachments $FileName -UseSsl
    } While ($FileEvent) 
} Catch { 
    $_ | Out-String | Write-Error 
} Finally { 
    Remove-Event -SourceIdentifier $SourceID -ErrorAction SilentlyContinue 
    Unregister-Event -SourceIdentifier $SourceID -ErrorAction SilentlyContinue 
}


This one below satisfies Jimi's OP for a file containing headers. However, I only used To and Subject. The sender is a fixed variable. Create a text file, add the following to the top of the file with your own email address/subject:

Code: Select all

To: recipient@domain.tld
Subject: This is the message subject line
Message: 
The message can be anything. Then drop the file into the pickup folder and it will automatically send.

Powershell:

Code: Select all

### Email Variables ########################################
                                                           #
$smtpFrom           = "notify@mydomain.tld"                #
$smtpServer         = "localhost"                          #
$SMTPAuthUser       = "notify@mydomain.tld"                #
$SMTPAuthPass       = "supersecretpassword"                #
                                                           #
### File Variables #########################################
                                                           #
$MonitorFolder      = "C:\scripts\pickup\pickup"           #
$MonitorStopFile    = "end.Mon"                            #
                                                           #
############################################################


$SourceID = "MonitorFiles" 
$Query = @" 
SELECT * FROM __InstanceCreationEvent WITHIN 10 
WHERE targetInstance ISA 'Cim_DirectoryContainsFile' 
AND targetInstance.GroupComponent = 'Win32_Directory.Name="$($MonitorFolder.Replace("\", "\\\\"))"' 
"@ 
Try { 
    $smtp = New-Object -TypeName "Net.Mail.SmtpClient" -ArgumentList $smtpServer 
    Register-WmiEvent -Query $Query -SourceIdentifier $SourceID 
    Do { 
        "Waiting for a new file to arrive in '$($MonitorFolder)'; to stop, hit <Ctrl-C> or create a file '$MonitorStopFile'." | Write-Host 
        $FileEvent = Wait-Event -SourceIdentifier $SourceID 
        Remove-Event -EventIdentifier $FileEvent.EventIdentifier 
        $FileName = $FileEvent.SourceEventArgs.NewEvent.TargetInstance.PartComponent.Split("=", 2)[1].Trim('"').Replace("\\", "\") 
        If ((Split-Path -Path $FileName -Leaf) -eq $MonitorStopFile) { 
            $smtpBody = "[$(Get-Date -Format HH:mm:ss)]`tStop file arrived: '$($FileName)'; monitor is going down!" 
            Remove-Item -Path (Join-Path -Path $MonitorFolder -ChildPath $MonitorStopFile) 
            $FileEvent = $Null 
        } Else { 
			If ((Get-Content $FileName | ? {$_.startswith('To: ')} | % {$_.substring(4)}) -ne ''){
				$smtpTo = Get-Content $FileName | ? {$_.startswith('To: ')} | % {$_.substring(4)}
				$smtpSubject = Get-Content $FileName | ? {$_.startswith('Subject: ')} | % {$_.substring(9)}
				$smtpBody = Get-Content $FileName | out-string
			}
        } 
		$SecPasswd = ConvertTo-SecureString $SMTPAuthPass -AsPlainText -Force
		$Cred = New-Object System.Management.Automation.PSCredential ($SMTPAuthUser, $SecPasswd)
        Send-MailMessage -SmtpServer $smtpServer -To $smtpTo -From $smtpFrom -Credential $Cred -Subject $smtpSubject -Body $smtpBody -Port 587 -UseSsl
    } While ($FileEvent) 
} Catch { 
    $_ | Out-String | Write-Error 
} Finally { 
    Remove-Event -SourceIdentifier $SourceID -ErrorAction SilentlyContinue 
    Unregister-Event -SourceIdentifier $SourceID -ErrorAction SilentlyContinue 
}


In both scripts, creating a file named "end.mon" will stop the script (end monitoring).

Post Reply