Read and forward mail in an IMAP inbox

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
davep
Posts: 14
Joined: 01 May 2018, 09:01

Read and forward mail in an IMAP inbox

01 May 2018, 09:25

Been searching the forums, and only find two similar questions, one with no answer, the other with a somewhat different need and no resolution established, so this may be one of those "you can't get there from here" things, but with the breadth of knowledge, I still have hope:

My need is this: I have an IMAP mailbox that needs to be monitored. If any message(s) exist in that mailbox, then they need to be forwarded to a set of other email addresses. This process needs to repeat every so many minutes, as long as the mailbox is not empty. Once the messages are deleted of course, nothing should be found to forward.

I am currently doing this with a Thunderbird client, rules, and an AHK script that runs the rules every so many minutes, but Thunderbird has been less than completely reliable in this regard, and it is cludgy, too many things required to be running, and it's easy for someone to mess it up.

So, the ideal script would:
On continuous loop, every five minutes
Check for the presence of messages
If messages exist
For each message, forward to [email protected], [email protected], [email protected]

I've not yet found a way to retrieve and/or forward natively in AHK, and although that would be the ultimate, have been searching for third party apps that could be made to work. I'm familiar with blat for sending, but the check and forwarding directly or downloading to forward escapes me. I should say that we're a windows shop, so I don't have facility for a 'nix based option.

Any help is appreciated. Thank you.
User avatar
FanaticGuru
Posts: 1907
Joined: 30 Sep 2013, 22:25

Re: Read and forward mail in an IMAP inbox

01 May 2018, 12:11

This is very doable with Outlook. Probably can do with just rules, definitely with VBA macro, definitely with AHK through COM.

I assume Outlook is not an option or you would have already solved this problem but if Outlook is an option, let me know and I will help.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
davep
Posts: 14
Joined: 01 May 2018, 09:01

Re: Read and forward mail in an IMAP inbox

01 May 2018, 15:00

Perhaps so, I do use Outlook, both with rules and VBA, although it is not my forte, and requires a fair amount of "re-learning" each time I pick that challenge up. By nature it seems OL rules are only run on new mail receipt and when ran manually. I have some rules being called from a VBA script tied to a button. This was especially challenging for me, as it was not for rules against my own inbox, but on another, which added a layer of complexity, but it does work. I assume I could do something similar, adding a vba timer functionality. However, because of the relative complexity, it appears similar to the system I have in place now using Thunderbird and AHK. Not difficult to maintain for a techie, but not so much for others. Ideally, I would like this to run on another user's system as a fully "background" function (ideally one I could even set up to auto-run with a service account). Thanks!
User avatar
FanaticGuru
Posts: 1907
Joined: 30 Sep 2013, 22:25

Re: Read and forward mail in an IMAP inbox

01 May 2018, 18:01

If the system has Outlook installed them you can use the COM abilities of AHK to access Outlook and do all kinds of email stuff.

Here is an example of accessing all the emails in an Outlook folder:

Code: Select all

F12::
	olApp := ComObjCreate("Outlook.Application")
	olNameSpace := olApp.GetNamespace("MAPI")
	olAccount := olNameSpace.Folders("[email protected]")
	MsgBox % olAccount.FolderPath
	olSubfolder := olAccount.Folders("Inbox").Folders("FanaticGuru")
	MsgBox % olSubfolder.FolderPath
	for Item in olSubfolder.Items
		MsgBox % Item.Subject
return
This assumes you have Outlook on the system, an email account set up in Outlook named, "[email protected]", and a subfolder in your inbox named "FanaticGuru". All that true this will list the subject of all emails in that folder. All in the background without actually displaying Outlook. The script does create an instance of Outlook but it does not display it.

If this looks like something that would work for you then it can be modified to work on a timer and do more than just display the emails' subjects.

It could also be designed to just work on each email as it comes in instead of emails in a folder but it was not clear if the idea was to have it forward the same emails over and over at a set time as long as they stay in a folder.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
davep
Posts: 14
Joined: 01 May 2018, 09:01

Re: Read and forward mail in an IMAP inbox

02 May 2018, 07:35

That is indeed the plan, until someone deletes the messages manually, they remain and are forwarded.

Looks like it could be made to work. I had started down this path with some stuff Sue Mosher had worked with when I created the Thunderbird/AHK solution in place now. Not that it doesn't work, I just haven't found either OL or TB to be a tremendously reliable base for a "set it and forget it" automation solution. AHK on the other hand has provided me just that for myriad things. I was kind of hoping against hope I guess that somebody had done something similar perhaps using simple utility apps. For some reason, although send doesn't seem to be much challenge (Blat is good at that), the check and forward functions don't seem to be available in a windows command line utility. Nature of the beast I guess. Thanks again.
User avatar
FanaticGuru
Posts: 1907
Joined: 30 Sep 2013, 22:25

Re: Read and forward mail in an IMAP inbox

02 May 2018, 14:13

davep wrote:That is indeed the plan, until someone deletes the messages manually, they remain and are forwarded.

Looks like it could be made to work. I had started down this path with some stuff Sue Mosher had worked with when I created the Thunderbird/AHK solution in place now. Not that it doesn't work, I just haven't found either OL or TB to be a tremendously reliable base for a "set it and forget it" automation solution. AHK on the other hand has provided me just that for myriad things. I was kind of hoping against hope I guess that somebody had done something similar perhaps using simple utility apps. For some reason, although send doesn't seem to be much challenge (Blat is good at that), the check and forward functions don't seem to be available in a windows command line utility. Nature of the beast I guess. Thanks again.
Here is example of forwarding all emails in a folder:

Code: Select all

F12::
	olApp := ComObjCreate("Outlook.Application")
	olNameSpace := olApp.GetNamespace("MAPI")
	olAccount := olNameSpace.Folders("[email protected]")
	olSubfolder := olAccount.Folders("Inbox").Folders("FanaticGuru")
	for olMailItem in olSubfolder.Items
	{
		olForward := olMailItem.Forward
		olForward.Display ; Remove to work in background
		olForward.To := "[email protected]"
		MsgBox WAIT ; Remove to work in background
		olForward.Send
	}
return
Use with caution, you could end up forwarding thousands of emails if used on a full folder. Added a MsgBox to slow things down for testing.

This works on pushing a key but you could use SetTimer to work on an interval.

You could add stuff to the body, change subject, whatever before Sending.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
davep
Posts: 14
Joined: 01 May 2018, 09:01

Re: Read and forward mail in an IMAP inbox

03 May 2018, 07:52

Thanks FG, I'll give it a whirl...
davep
Posts: 14
Joined: 01 May 2018, 09:01

Re: Read and forward mail in an IMAP inbox

03 May 2018, 08:25

You know, I've never used SetTimer. I guess I started using Sleep, and never really had enough problems to purse other options. I'm sure I've seen reference to it countless times. Thanks for that too.
davep
Posts: 14
Joined: 01 May 2018, 09:01

Re: Read and forward mail in an IMAP inbox

03 May 2018, 09:20

Have to stop for a while, traveling for a bit, but this looks promising. Right now I'm fighting what I believe is a problem actually recognizing items in the root of the inbox. Worked through errors that told me I didn't have the account referenced correctly, no more errors, so I think it's just not hitting the inbox folder. The script seems to run okay, just never goes inside the "for" loop. I'll pick it up again as soon as I get back. Thanks for the help!
User avatar
FanaticGuru
Posts: 1907
Joined: 30 Sep 2013, 22:25

Re: Read and forward mail in an IMAP inbox

03 May 2018, 13:22

davep wrote:Right now I'm fighting what I believe is a problem actually recognizing items in the root of the inbox. Worked through errors that told me I didn't have the account referenced correctly, no more errors, so I think it's just not hitting the inbox folder. The script seems to run okay, just never goes inside the "for" loop.
This script might help debug.

Code: Select all

F12::
	olApp := ComObjCreate("Outlook.Application")
	olNameSpace := olApp.GetNamespace("MAPI")
	olAccount := olNameSpace.Folders("[email protected]")
	MsgBox % olAccount.FolderPath
	olSubfolder := olAccount.Folders("Inbox").Folders("FanaticGuru")
	MsgBox % olSubfolder.FolderPath
	for Item in olSubfolder.Items
		MsgBox % Item.Subject
return
It will show you the path of account, path of subfolder, and then loop through the emails in the subfolder showing subject.

It is important to realize that all the folder and account stuff is probably case-sensitive. Even though an email address or path normally is not case-sensitive, when used as an account name or folder name like this in a Namespace, it is probably case-sensitive.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
davep
Posts: 14
Joined: 01 May 2018, 09:01

Re: Read and forward mail in an IMAP inbox

03 May 2018, 14:32

Still stuck in the office, so I tried it real quick (I'm NEVER gonna get out of here)

Thanks, that did help! I have it now resolving the inbox path (\\<email_address>\Inbox) appears correct in the message box. Now having trouble with the "for Item", it' gives an error "0x80020006 - Unknown Name - Specifically: _NewEnum". Interestingly, "The Google" only shows one real reference to that error, in another AHK forum. At first glance, I don't see direct correlation, although admittedly I'm rushing a bit.

That's a lot farther. Maybe I can do some remote work while I'm out.

I'm excited about the possibility, if this works, I should be able to set AHK to do a continuous cycle with the code, make sure OL is running, and restart it once in a while.

Again thanks!
davep
Posts: 14
Joined: 01 May 2018, 09:01

Re: Read and forward mail in an IMAP inbox

03 May 2018, 15:14

So a little remote work, changed "for Item in olSubfolder.Items" to "Loop, % olSubfolder.Items.Count", and I'm seeing the "WAIT" MsgBox, so we're in the loop now.
(I also did a MsgBox for that variable and am seeing a proper item count).

Messages aren't forwarding yet, but progress!
davep
Posts: 14
Joined: 01 May 2018, 09:01

Re: Read and forward mail in an IMAP inbox

03 May 2018, 15:33

Last one I can do from here, and I'll pick it back up when I get back, it appears the IMAP server I'm polling from may not support outbound SMTP from a remote client. This is why the existing AHK/Thunderbird works, as it is configured to send from a gmail account.

I really want to make this work (and I think the script WILL work, if I can figure out a way to get around the SMTP restrictions using the same server, or can instead pump it out with blat or something else).

Anyway I wanted to post a final update of the day's progress, and will keep you informed, again FG, thanks for the help!
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Read and forward mail in an IMAP inbox

09 May 2018, 19:13

I don't know anything about the software involved but you could try:

Code: Select all

for _, Item in olSubfolder.Items
;instead of:
for Item in olSubfolder.Items
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
davep
Posts: 14
Joined: 01 May 2018, 09:01

Re: Read and forward mail in an IMAP inbox

17 May 2018, 13:28

I believe I am past that part, showing the items successfully via msgbox, just need to get the forward working. I've gotten pulled away, but all the windows are still open (lousy task management system, I know). When I can get three brain cells together, I figured on trying a blat send. Thanks for the suggestion.
User avatar
FanaticGuru
Posts: 1907
Joined: 30 Sep 2013, 22:25

Re: Read and forward mail in an IMAP inbox

17 May 2018, 15:53

jeeswg wrote:I don't know anything about the software involved but you could try:

Code: Select all

for _, Item in olSubfolder.Items
;instead of:
for Item in olSubfolder.Items
Just for the record for Item in olSubfolder.Items is correct. This is the way for works with most COM objects which are collections where the key is the data.

You could probably do for Item, Type in olSubfolder.Items where Item contains the data that you want and the Type contains the data type but the data type is probably always going to be 9 for a COM object. This is pretty much just theory. Have not played with it much of unraveling the structure.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
davep
Posts: 14
Joined: 01 May 2018, 09:01

Re: Read and forward mail in an IMAP inbox

24 May 2018, 11:37

catching up, I do have the items being found in the relevant folder, I can see each item via a msgbox that I have in the for loop. The problem is that the IMAP server I'm checking doesn't support SMTP, so I can't forward the message. I have been trying to use SendUsingAccount to send it out from a different mailbox, but thus far have not been successful. Seems like I'm getting just a couple of minutes here or there to finish this, but if I can get the package to work, I'll post the script for posterity.
User avatar
FanaticGuru
Posts: 1907
Joined: 30 Sep 2013, 22:25

Re: Read and forward mail in an IMAP inbox

24 May 2018, 13:45

davep wrote:catching up, I do have the items being found in the relevant folder, I can see each item via a msgbox that I have in the for loop. The problem is that the IMAP server I'm checking doesn't support SMTP, so I can't forward the message. I have been trying to use SendUsingAccount to send it out from a different mailbox, but thus far have not been successful. Seems like I'm getting just a couple of minutes here or there to finish this, but if I can get the package to work, I'll post the script for posterity.
SendUsingAccount is more tricky to use than some might think. It requires an Account object, not just the name of an account.

Here is an example:

Code: Select all

F12::
	olApp := ComObjCreate("Outlook.Application")
	olNameSpace := olApp.GetNamespace("MAPI")
	olAccount := olNameSpace.Folders("[email protected]")
	olSubfolder := olAccount.Folders("Inbox").Folders("FanaticGuru")
	for olMailItem in olSubfolder.Items
	{
		olForward := olMailItem.Forward
		olForward.Display ; Remove to work in background
		olForward.SendUsingAccount := olNameSpace.Accounts.Item("[email protected]") ; Account Display Name, case sensitive
		olForward.To := "[email protected]"
		MsgBox WAIT ; Remove to work in background
		olForward.Send
	}
return
It is important to realize that "[email protected]" is an account name which may or may not be an email address. Outlook defaults to using the email address for the account name but the account name can be changed by the user. So this has to be exactly as displayed (typically in the left tree in Outlook, this left tree is a display of the Namespace structure).

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
davep
Posts: 14
Joined: 01 May 2018, 09:01

Re: Read and forward mail in an IMAP inbox

24 May 2018, 17:13

That example is pretty much dead on what I'm using, I must have copied it from another thread. But if I read it right, it's using the same user account for both the folder check and the send function. In my effort, it's the send function that's biting me, as I'm doing the folder contents check on an IMAP mailbox on an appliance. So I can read from it, but not send. So I was trying to use SendUsingAccount to send the forwards from a different account in Exchange (i.e. olForward.SendUsingAccount := olNameSpace.Accounts.Item("davep")), which I have control of via Outlook. Perhaps I need to define that account separately? Sorry for the confusion, and thanks again.
User avatar
FanaticGuru
Posts: 1907
Joined: 30 Sep 2013, 22:25

Re: Read and forward mail in an IMAP inbox

24 May 2018, 19:29

davep wrote:That example is pretty much dead on what I'm using, I must have copied it from another thread. But if I read it right, it's using the same user account for both the folder check and the send function. In my effort, it's the send function that's biting me, as I'm doing the folder contents check on an IMAP mailbox on an appliance. So I can read from it, but not send. So I was trying to use SendUsingAccount to send the forwards from a different account in Exchange (i.e. olForward.SendUsingAccount := olNameSpace.Accounts.Item("davep")), which I have control of via Outlook. Perhaps I need to define that account separately? Sorry for the confusion, and thanks again.
olForward.SendUsingAccount := olNameSpace.Accounts.Item("davep")) this should send the email through account "davep" regardless of what account the email was received through.

Need to make sure "davep" is a valid account name. Maybe this code will help with that.

Code: Select all

olApp := ComObjCreate("Outlook.Application")
olNameSpace := olApp.GetNamespace("MAPI")
x := 0
Loop
{
	x++
	try
		MsgBox % x "`t" olNameSpace.Accounts.Item(x).DisplayName
	catch
		break
}

Esc::ExitApp
This code will list all the accounts setup in Outlook.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: olyria18 and 322 guests