Page 1 of 1

How to mail from KCML

Posted: Thu Jun 17, 2010 8:38 pm
by Ruud
As it took me quite a while to find out how to mail decently I enclose a subroutine that does the trick:

Only the first 4 arguments are mandatory.

Subject$
  • This text is used as the subject for the message
Sender$ Message$
  • The full HTML coded text of the message. This may include all valid HTML tags
Recipient
  • The SYM of an array that holds the mailaddresses of all recipients
Cc
  • The SYM of an array that holds the mailaddrsses of everyone that receives a copy of the mail
Bcc
  • The SYM of an array that holds the mailaddrsses of everyone that receives a blind copy of the mail
Attachments
  • the SYM of an array that holds the path and filename of all attachments
Ppicture
  • refers to an array of picturenames (including path) that can be embedded in the message. The messagecontent should hold <img src="cid:image1.gif"> where the number is the sequence in the array and the extension (.gif .bmp etc) should match with the picturename.
Reply$
  • holds the reply to name. A message can be sent from info@yourserver.com but a replay is to be sent to another. In that situation you send reply$ to the routine.
Don't forget to update the statement that holds your mailservername and portnumber (usually 25)

DEFSUB 'sendmail(subject$, sender$, message$, recipient, cc = 0, bcc = 0, attachments = 0, picture = 0, reply$ = " ")
LOCAL DIM mailhost$90, z$50, z, poort, adres$1000
mailhost$ = "servername:port"
REM alter this value to your needs
OBJECT mail = CREATE "ServerCom", "CDO.Message"
mail.configuration.Fields.Item("http://schemas.microsoft.com/cdo/config ... ").Value(2)
z = POS(mailhost$ == ":")
IF (z > 0)
z$ = STR(mailhost$, z + 1)
STR(mailhost$, z) = " "
CONVERT z$ TO poort
ERROR DO
poort = 25
END DO
END IF
mail.configuration.Fields.Item("http://schemas.microsoft.com/cdo/config ... e(mailhost$)
mail.configuration.Fields.Item("http://schemas.microsoft.com/cdo/config ... alue(poort)
IF (picture > 0)
FOR z = 1 TO DIM(SYM(*picture)$(), 1)
adres$ = $PRINTF("<image%d", z)
adres$ = & STR(SYM(*picture)$(z), POS(SYM(*picture)$(z) == "."))
mail.AddRelatedBodyPart(SYM(*picture)$(z), STR(adres$, 2, LEN(adres$) - 1), cdoreferencetypename)
adres$ = & ">"
mail.configuration.Fields.Item("urn:schemas:mailheader:Content-ID").Value(adres$)
NEXT z
END IF
mail.configuration.Fields.update()
mail.From(sender$)
IF (POS(reply$ > HEX(20)) > 0) THEN mail.replyto(reply$)
IF (recipient > 0)
FOR z = 1 TO DIM(SYM(*recipient)$(), 1)
IF (z == 1) THEN adres$ = SYM(*recipient)$(z)
ELSE IF (POS(SYM(*recipient)$(z) > HEX(20)) > 0) THEN adres$ = & ";" & SYM(*recipient)$(z)
NEXT z
mail.To(adres$)
END IF
IF (cc > 0)
FOR z = 1 TO DIM(SYM(*cc)$(), 1)
IF (z == 1) THEN adres$ = SYM(*cc)$(z)
ELSE IF (POS(SYM(*cc)$(z) > HEX(20)) > 0) THEN adres$ = & ";" & SYM(*cc)$(z)
NEXT z
mail.cc(adres$)
END IF
IF (bcc > 0)
FOR z = 1 TO DIM(SYM(*bcc)$(), 1)
IF (z == 1) THEN adres$ = SYM(*bcc)$(z)
ELSE IF (POS(SYM(*bcc)$(z) > HEX(20)) > 0) THEN adres$ = & ";" & SYM(*bcc)$(z)
NEXT z
mail.bcc(adres$)
END IF
mail.Subject$ = subject$
IF (attachments > 0)
FOR z = 1 TO DIM(SYM(*attachments)$(), 1)
adres$ = SYM(*attachments)$(z)
IF (POS(adres$ > HEX(20)) > 0) THEN mail.addattachment(adres$)
NEXT z
END IF
mail.htmlbody$ = LTRIM(message$)
mail.send()
END SUB

Re: How to mail from KCML

Posted: Mon Nov 21, 2011 6:38 am
by vvidkish
This is wonderful. I should try this out.