Lotusscript send mail

I’m trying to write some lotusscript that will send an email to a bunch of users.I have several fields on the form that contain the email addresses seperated by “;”.

However, when the script runs, it only sends the mail to the first email address in each field.

The script is as follows:

Sub Postopen(Source As Notesuidocument)

Dim workspace As New NotesUIWorkspace

Dim uidoc As NotesUIDocument, doc As NotesDocument



Set uidoc = workspace.CurrentDocument

Set doc=uidoc.Document



If uidoc.IsNewDoc Then

	Dim subj As String

	subj=("Company ISM Form Change Notification : Section ")&doc.ESection(0)&(", Policy No ")&doc.ENumber(0)

	

	Dim shipman, md, marinesuper, masters, ships As Variant

	shipman = doc.ESM

	md=doc.EMD

	marinesuper=doc.EMS

	masters=doc.EM

	ships=doc.ES

	

	Dim recipients(0  To 5 ) As String

	recipients( 0 ) = "d.l@hxxxx.com"

	recipients( 1 ) = md(0)

	recipients( 2 ) = marinesuper(0)

	recipients( 3 ) = shipman(0)

	recipients( 4 ) = masters(0)

	recipients( 5 ) = ships(0)

	

	doc.Form = "MailCISM"

	doc.Subject = subj

	Call doc.Send(True,recipients)

	Call uidoc.Close(True)

End If	

End Sub

It returns all the recipients fields, but just the first address from each field, some of which contain up to 7 email addresses. Any idea how to get it to return all the email addresses and send the mail? I tried variants, but I can’t get it to work!

Thanks in advance, Darren.

Subject: Answer

You are assigning the first value in each of those fields to the recipient array. Loop thru each one of those variables and assign all of the elements to the recipients array if they i guess are not blank

Subject: Barry is right

you could also look at at ArrayAppend (documented in designer help) to append the arrays of names together, that will save you looping through each field.

Subject: Thanks for the pointers, but…

I have looked at the help but I can’t make any sense of it. It seems to be giving examples of fixed lists of text (or numbers).I need it to use a field to get the values. The field may have 1 or 2 or 3 up to potentially 10 values.

My brain hurts.

Subject: Example

dim def (0 to 1) as stringdef(0) = “darren.lambe@hanson.com

recipients = ArrayAppend(def,md, marinesuper,shipman,masters,ships)

Subject: Arrays

From your code:Dim shipman, md, marinesuper, masters, ships As Variant

shipman = doc.ESM

md=doc.EMD

marinesuper=doc.EMS

masters=doc.EM

ships=doc.ES

You now have an array in each variable.

For example, shipman contains the addresses “foo@example.com” and “bar@example.com” in two elements.

Dim recipients(0 To 5 ) As String

recipients( 0 ) = “darren.lambe@hanson.com

recipients( 1 ) = md(0)

recipients( 2 ) = marinesuper(0)

recipients( 3 ) = shipman(0)

recipients( 4 ) = masters(0)

recipients( 5 ) = ships(0)

Here you only read the first value of each array (e.g. shipman(0) to get “foo@example.com” and assign that into element 3 of the recipients array.

What you need to do is to copy all the values in each array into a larger array, like Barry and Carl explains.

In addition, if I may give you some pointers:

  • If you declare several variables on one line, like you do, unless you specify each one with their data type, they will become variants by default. In your example that does not matter, as you want them to be variants (to later become arrays).

But if you do this:

Dim FirstName, LastName, City As String

Then only “City” will be a string, FirstName and LastName will be variants…

It is also recommended that you don’t use extended notation to read values from a document the way you do:

shipman = doc.ESM

md=doc.EMD

Instead use the GetItemValue() method of the NotesDocument class. There are several reasons for this, both from a performance and from a compatibility point.

So do this:

shipman = doc.GetItemValue(“EMS”)

to get all values as an array or

shipman = doc.GetItemValue(“EMS”)(0)

to get the first value as a string.

I also prefer to have all declarations at the top of each function (and in aproximate the order the variables are used), that makes the code easier to read, IMHO.

Subject: Thankyou for the pointers friends…

Code now works…had to do some fiddling with joining all the arrays up, but it works as follows:

Dim workspace As New NotesUIWorkspace

Dim uidoc As NotesUIDocument, doc As NotesDocument



Set uidoc = workspace.CurrentDocument

Set doc=uidoc.Document



If uidoc.IsNewDoc Then

	Dim subj As String

	subj=("Company ISM Form Change Notification : Section ")&doc.ESection(0)&(", Policy No ")&doc.ENumber(0)

	

	Dim shipman, md, marinesuper, masters, ships As Variant

	shipman = doc.GetItemValue("ESM")

	md=doc.getitemvalue("EMD")

	marinesuper=doc.getitemvalue("EMS")

	masters=doc.getitemvalue("EM")

	ships=doc.getitemvalue("ES")

	

	Dim def (0 To 1) As String

	def(0) = "XXX.XXX@XXX.com"

	

	Dim rec1 As Variant

	rec1 = Arrayappend(def,md)

	Dim rec2 As Variant

	rec2=Arrayappend(marinesuper,shipman)

	Dim rec3 As Variant

	rec3=Arrayappend(masters,ships)

	Dim rec4 As Variant

	rec4=Arrayappend(rec1, rec2)

	Dim recipients As Variant

	recipients=Arrayappend(rec3, rec4)

	

	doc.Form = "MailCISM"

	doc.Subject = subj

	Call doc.Send(True,recipients)

	Call uidoc.Close(True)

End If

Subject: My code…

I have been cleaning up your code a little bit more, trying to make it easier to read.

This is how I would write your code. Note that I have not changed anything in your logic/functionality.

'--- Domino classes

Dim ws As New NotesUIWorkspace

Dim uidoc As NotesUIDocument

Dim doc As NotesDocument

'--- Other variables

Dim subj As String

Dim def(0) As String

Dim recipients As Variant



Set uidoc = ws.CurrentDocument

Set doc=uidoc.Document



If uidoc.IsNewDoc Then

	subj = "Company ISM Form Change Notification : Section "

	subj = subj + doc.GetItemValue("ESection")(0) + ", Policy No " + doc.GetItemValue("ENumber")(0)



	def(0) = "XXX.XXX@XXX.com"

	recipients = ArrayAppend(def,doc.GetItemValue("EMD"))	' Get MD values

	recipients = ArrayAppend(recipients,doc.GetItemValue("ESM"))	' Get shipman values

	recipients = ArrayAppend(recipients,doc.GetItemValue("EMS"))	' Get marinesuper values

	recipients = ArrayAppend(recipients,doc.GetItemValue("EM"))	' Get masters values

	recipients = ArrayAppend(recipients,doc.GetItemValue("ES"))	' Get ships values



	doc.Form = "MailCISM"

	doc.Subject = subj

	Call doc.Send(True,FullTrim(recipients))

	Call uidoc.Close(True)

End If 

I think that should work.

Subject: That looks better…

Thanks for that, never done append array before…your code makes much more sense, thankyou I’ll give it a go!