Fowarding attachments in seperate email

I have a email box that with the incoming emails there are attachments. It can be any amount of attachments on the email.  I need to be able to take the email message (text) in the body of the email and forward that automaticallly to another area, and then take each attachment and detach from the orginal email and place that automatically into a new mail memo and send to the same area. So if there were 2 attachments in one email, three new emails would be sent. One with the text of the email and two seperate emails with 1 attachment each.

I have seen some code on how to count the attachments or detach to a network location but that is still not getting me to where i need to be.  I would like this to run on "New Mail" and have it be a automatted process or schedule it to run every 5-10 minutes if needed.

Any ideas would be great or to know if this can even be done! Thanks in advance!
kali958Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sjef BosmanGroupware ConsultantCommented:
Well, it's feasible, and you even do not have to save the attachments on the server. Here's the general idea, for you to code in LotusScript:

open the received mail
make a copy of the original mail
update the copy so that the destination is the right person
remove all attachments, and send it out
for all subsequent mails
      make a copy of the original mail
      update the copy so that the destination is the right person
      remove all but one attachment, and send it out
end for

You could improve performance probably by making a new copy only in memory, so just open the original mail as a NotesDocument, remove the attachments but never save that mail.
0
mbonaciCommented:
This is the code from SearchDomino that copies attachments from one document to another without disk I/O (detaching):

http://searchdomino.techtarget.com/tip/0,289483,sid4_gci1037183_mem1,00.html
Sub CopyAttachments (db As NotesDatabase, docSource As NotesDocument, docTarget As NotesDocument)
 Dim docTemp As NotesDocument 
 
 'Remove all attachments from existing document
 Call docTarget.RemoveItem ("$FILE")
 
 'Create a temporary document and use CopyAllItems from the current document
 'because I know that will include the attachment
 Set docTemp = db.CreateDocument
 Call docSource.CopyAllItems (docTemp)
 
 'Clear all items not named $File from the temp docSource, so that all that's left is the attachment
 Forall item In docTemp.Items
  If Not (Ucase(item.Name) = "$FILE") Then
   docTemp.RemoveItem (item.Name)
  End If  
 End Forall
 
 'Now, docTemp only has the attachments left on it
 'CopyAllItems will copy the attachment, so use it to copy to docTarget
 Call docTemp.CopyAllItems (docTarget)
 Call docTarget.Save (True, False)
End Sub

Open in new window

0
mbonaciCommented:
Here's the code that strips the attachments from a document:

http://www.experts-exchange.com/Q_21083807.html

So, now you have all that, simply create an agent that acts when new mail arrives (no need to schedule it every 5-10 minutes, as you mentioned).
The collection of documents (db.GetUnprocessedDocuments) will then consist only of new mail documents.

Cycle through the collection and for each document and on each of them use Sjef's algorithm to send e-mails.

Only, in that algorithm, replace the line
    "for all subsequent mails"

with
    "for each attachment in current document"
0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

kali958Author Commented:
Thank you so much for the Answers to my question. I am currently out of the office until next Monday but then I can let you all knoww howw this went. Thanks again for all the ideas
0
kali958Author Commented:
I am still waiting for an answer from the team I am helping with this issue. I will keep you up to date as soon as i hear if there are further questions or a solution accepted :)
0
kali958Author Commented:
I have a udpate on the issue.

Currently the agent is being run manually on new mail in test.

 When you send one email with one attachment with one attachment, it is working. But if you send an email with multiple attachments, then the first will detach and is fine but all following attachments are giving an error (see Image) and then when try try to open the attachment it is a mess.

Any ideas?
Main agent
	While Not(doc Is Nothing)	
		Set rtitem = doc.GetFirstItem("body")
		If doc.HasEmbedded Then 'For emails with attachments
			
			Set attachdoc = New NotesDocument( db )
			'This copies all attachments and the body from doc to attachdoc
			Call CopyAttachments(db, doc, attachdoc, attachmentCount)
			Set tmpDoc = New NotesDocument( db )
'Logic is: For each attachment, replace all attachments with the current attachment, using the ReplaceItemValue method.  This is the only way to reduce multiple attachments on a doc to one singular attachment.  Once you have a document with one attachment, blank out the body, and mail it out.  On the next time through, do the same for any other attachments you have.
			For currentCount = 1 To attachmentCount
				tempCount = 1
				Set tmpDoc = New NotesDocument( db )
				Call attachdoc.CopyAllItems(tmpDoc)
				Set maildoc = New NotesDocument( db )
				Forall item In tmpDoc.Items
					If (sendCount = tempCount) Then
						'This replaces all $FILE items with one item of the current item being processed in tmpDoc.Items
						Set tempItem = tmpDoc.ReplaceItemValue( item.Name, item )
					End If
					If item.Name = "Body" Then
						'This creates a blank body within tmpDoc
						Set bodyItem = maildoc.createRichTextItem("body") 
						Set tempBodyItem = tmpDoc.ReplaceItemValue(item.Name,bodyItem)
					End If
					tempCount = tempCount + 1
				End Forall
'This is where I tried to implement IBM's workaround on signing all documents and items.  Unfortunately, the items were already signed, so this workaround does not work.
				Call tmpDoc.CopyAllItems (maildoc)
				Forall i In mailDoc.Items 
					If Ucase(i.name)="$FILE" Then 
						i.issigned=True
					End If
				End Forall
				Forall i In doc.Items 
					If Ucase(i.name)="$FILE" Then 
						i.issigned=True
					End If
				End Forall
				Call doc.sign
				Call doc.save(True,True)
				Call maildoc.Sign
				Call maildoc.save(True, True)
				
				maildoc.Form = "Memo"
				maildoc.From = doc.From
				maildoc.SendTo = "allen.ng@securian.com"
				Call maildoc.Send( False )
				sendCount = sendCount + 1
			Next


Sub CopyAttachments (db As NotesDatabase, docSource As NotesDocument, docTarget As NotesDocument, count As Integer)
	Dim docTemp As NotesDocument 
	count = 0
 'Remove all attachments from existing document
	Call docTarget.RemoveItem ("$FILE")
	
 'Create a temporary document and use CopyAllItems from the current document
 'because I know that will include the attachment
	Set docTemp = db.CreateDocument
	Call docSource.CopyAllItems (docTemp)
	
 'Clear all items not named $File from the temp docSource, so that all that's left is the attachment.  You also need to
 'leave the body unremoved because if you remove the body it will remove the attachments.
	Forall item In docTemp.Items
		If Not (Ucase(item.Name) = "$FILE"  Or Ucase(item.Name = "Body")) Then
			docTemp.RemoveItem (item.Name)
		Else
			If Ucase(item.Name) = "$FILE" Then
				count = count + 1
			End If
		End If  
	End Forall
	
 'Now, docTemp only has the attachments and the body from the original eft on it
 'CopyAllItems will copy the attachment, so use it to copy to docTarget
	Call docTemp.CopyAllItems (docTarget)
	Call docTarget.Save (True, False)
End Sub

Open in new window

04-28-2010-Error.png
0
Sjef BosmanGroupware ConsultantCommented:
I don't think this will ever work. Attachments aren't Items that you can replace.

Here my suggested pseudo-code:

omail = original mail
n= #attachments
for i=1 to n
      nmail= new document
      copy all from omail to nmail
      for j=1 to i-1
            remove attachment[i] in nmail
      next
      for j=1+1 to n
            remove attachment[i] in nmail
      next
      send nmail
next
0
mbonaciCommented:
You can copy attachments using disk as a temp storage (detach from orig and attach to new).
0
Sjef BosmanGroupware ConsultantCommented:
Yes, possible, but you don't really have to. Plus, sometimes, it creates additional problems, e.g. if there are multiple attachments with the same name.
0
kali958Author Commented:
Sjef, you are right, there could be issues and the thing is that the emails will come with multiple attachments.

The area I am working with to help wtih this issue want to be able to take an email from a customer that could include text in the body and for example 3 attachments. They want to be able to create a email just from the text in the body, then a 3 seperate emails with the attachments so that the items can be added into our image system. So if Susie Customer sends in 3 attachments, each one could be detached by the system rather then a person and put into the system that images, but each attachment would have to be seperated out in to 3 different emails.

Right now if it is just text and 1 attachment, they are able to detach it but any following attachments all come up in junk.

Mbonaci - the associate I am working with did see the article from IBM and did try to use the doc.sign in the code above but it was not working.

Do you think it would be possible to have the system open up and detach into a new email and then send each individual attachment to another lotus notes app?
0
mbonaciCommented:
That's exactly what I meant.
I sent you the link so that you can see why potentially the error occurs (wrong compression declaration), which can be avoided by detaching a file to disk and then attaching it to a new mail message:

http://www.e-e.com/Q_24236806.html
0
Sjef BosmanGroupware ConsultantCommented:
Ok, if I have some spare time later today I might write you some code along the lines of my pseudo-code. No time right now, sorry.
0
kali958Author Commented:
okay, one more update.

They did get it working by detaching to a fileshare but were really looking for a solution that did not include that.

Their concern is if the fileshare was to be restarted or unavailable while the agent is running, is there a notification process (code) that could be included to let someone know a document was not processed.

Is it possible to detach the attachments without using a fileshare?

Thanks again!
0
kali958Author Commented:
Just wondering if you guys had anymore thoughts on this issue we are facing. So far, we still have not found a way to do this wthout using the fileshare and that still gives us a few issues. Just thought I would check in.
0
mbonaciCommented:
Why don't you simply use server's local disk drive instead of a shared directory?
0
Sjef BosmanGroupware ConsultantCommented:
I don't yet know how to remove the text from the Body item...

The sub RemoveAttachmentsExcept should work with a NotesRichTextRange and a Remove. How exactly I haven't figured out yet. I thought it would be simpler...
Main agent
	While Not(doc Is Nothing)	
		Set rtitem = doc.GetFirstItem("body")
		If doc.HasEmbedded Then 'For emails with attachments
			' first, send the body
			Set tmpdoc = New NotesDocument( db )
			Call doc.CopyAllItems(tmpdoc)
			Call RemoveAttachmentsExcept(tmpdoc,-1)
			tmpdoc.SendTo "some address"
			Call tmpdoc.Send(False)
			' then, send the attachments one by one
			For currentCount = 1 To attachmentCount
				Set tmpdoc = New NotesDocument( db )
				Call doc.CopyAllItems(tmpdoc)
				Call RemoveAttachmentsExcept(tmpdoc, currentCount)
				tmpdoc.SendTo "some address"
				Call tmpdoc.Send(False)
			Next

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kali958Author Commented:
Well it looks like the team I was working has found a work around.  They used suggestions from both of you - THANK YOU.  It really helped them out. The code they came up with is below.

If it okay with you both, I would like to split the points since they did use mbonaci to reseach the links and Sjef, they did use your layout for the code.
Sub Initialize
	Dim workspace As New NotesUIWorkspace
	Dim view As NotesUIView
	Dim collection As NotesDocumentCollection
	Dim emailDoc As NotesDocument
	Dim docTarget As NotesDocument 
	Dim docTemp As NotesDocument 
	Dim docSource As NotesDocument
	Dim db As NotesDatabase
	Dim session As New NotesSession
	Dim rtitem As Variant
	Dim emailRtitem As Variant
	Dim attachments(1 To 50) As String
	Dim i As Integer
	
	Messagebox "Start Processing"
	
	Set view = workspace.currentview
	Set collection = view.Documents
	Set docSource = collection.GetFirstDocument
	Set db = session.CurrentDatabase	
	Set rtitem = docSource.GetFirstItem( "Body" )
	i = 1
	
	If ( rtitem.Type = RICHTEXT ) Then
		Forall o In rtitem.EmbeddedObjects
			If ( o.Type = EMBED_ATTACHMENT ) Then			
				i = i + 1
				attachments(i) = o.Source
				
				Set docTemp = db.CreateDocument
				Call docSource.CopyAllItems (docTemp, True)
				
				Forall item In docTemp.Items
					
					If (Ucase(item.Name) = "$FILE" Or Ucase(item.Name) = "BODY") Then
						'Null
					Else
						docTemp.RemoveItem (item.Name)
					End If 
				End Forall
				
				Set emailDoc = New NotesDocument( db )
				Call docTemp.CopyAllItems(emailDoc)
				
				Set emailRtitem = emailDoc.GetFirstItem( "Body" )
				
				If ( emailRtitem.Type = RICHTEXT ) Then
					Messagebox "Body RT"
					Forall object In emailRtitem.EmbeddedObjects
						If object.Source = attachments(i) Then
							Messagebox "Keeping " + attachments(i)	
						Else
							Messagebox "Removing " + object.source
							Call object.Remove
						End If
					End Forall
				End If
				
				emailDoc.Form = "Memo"
				emailDoc.SendTo = "William L Watts"
				emailDoc.Subject = "Attachment Test "
				Call emailDoc.Send( False )
				
			End If
		End Forall
	End If
	
End Sub

Open in new window

0
Sjef BosmanGroupware ConsultantCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Lotus IBM

From novice to tech pro — start learning today.