Files
MBS/PROYECTO/TEST/winmail/sendmail.4gl
T

221 lines
6.6 KiB
Plaintext

DEFINE smtplog base.StringBuffer
TYPE cnf_t RECORD
hostname STRING,
smtphost STRING,
smtpport INTEGER,
senderemail STRING,
cnxtimeout INTEGER
END RECORD
DEFINE cnf cnf_t
DEFINE rpc1 DYNAMIC ARRAY OF STRING,
mailbody1 DYNAMIC ARRAY OF STRING
DEFINE logtext STRING
MAIN
DEFINE quepaso BOOLEAN,
chan base.Channel
-- LET chan = base.Channel.create()
-- CALL chan.openServerSocket('smtp.office365.com','587','ub')
LET rpc1[1] ="jsoto@marmotech.com.do"
LET mailbody1[1] ="PRUEBA DE LA CONEXION"
-- CALL sendmail(rpc1, mailbody1) RETURNING quepaso
CALL mail()
END MAIN
FUNCTION mail()
DEFINE result, id INTEGER
DEFINE str STRING
-- first, we initialize the module
CALL ui.Interface.frontCall("WinMail", "Init", [], [id])
-- Set the body of the mail
CALL ui.interface.frontCall("WinMail", "SetBody", [id,
"This is a text mail using WinMail API - MAPI"], [result])
-- Set the subject of the mail
CALL ui.interface.frontCall("WinMail", "SetSubject", [id,
"test mail - ignore it"], [result])
-- Set the mail sender
CALL ui.Interface.frontCall("WinMail", "SetFrom", [id, "Nominas",
"nominas@marmotech.com.do"], [result])
-- Set the SMTP server
CALL ui.Interface.frontCall("WinMail", "SetSmtp", [id, "smtp.office365.com:587"],
[result])
-- Add an Addressee as "TO"
CALL ui.Interface.frontCall("WinMail", "AddTo", [id, "myBoss",
"jsoto@marmotech.com.do"], [result])
-- Add another Addressee as "BCC"
CALL ui.Interface.frontCall("WinMail", "AddBCC", [id, "my friend",
"jsoto@movilsoluciones.com.do"], [result])
-- Add Two attachments
CALL ui.Interface.frontCall("WinMail", "AddAttachment", [id,
"C:\\Users\\jsoto\\Documents\\MARMOTECH\\logo.png"], [result])
# CALL ui.Interface.frontCall("WinMail", "AddAttachment", [id,
# "c:\\mydocs\demo.png"], [result])
-- Send the mail via smtp
CALL ui.Interface.frontCall("WinMail", "SendMailSMTP", [id], [result])
-- Send the mail via the default mailer
-- CALL ui.Interface.frontCall("WinMail", "SendMailMAPI", [id], [result])
IF result == TRUE THEN
DISPLAY "Message sent successfully"
ELSE
CALL ui.Interface.frontCall("WinMail", "GetError", [id], [str])
DISPLAY str
END IF
CALL ui.Interface.frontCall("WinMail", "Close", [id], [result])
END FUNCTION
FUNCTION sendmail(rcpt, mailbody)
DEFINE mailbody DYNAMIC ARRAY OF STRING
DEFINE rcpt DYNAMIC ARRAY OF STRING
DEFINE smtpStatus INTEGER
DEFINE smtpMessage STRING
DEFINE mc base.Channel
DEFINE i INTEGER
LET smtplog = base.StringBuffer.create()
LET mc = base.Channel.create()
TRY
CALL mc.openClientSocket(cnf.smtphost, cnf.smtpport, "ub", cnf.cnxtimeout)
CATCH
DISPLAY "The connection to the SMTP Server failed.\n"
RETURN FALSE
END TRY
CALL readSmtpAnswer(mc) RETURNING smtpStatus, smtpMessage
CALL smtpSend(mc, "HELO " || cnf.hostname || "\r") RETURNING smtpStatus, smtpMessage
IF smtpStatus >= 400 THEN
CALL smtpError(mc, smtpMessage)
RETURN FALSE
END IF
CALL smtpSend(mc, SFMT("MAIL FROM: %1\r", extract_email(cnf.senderemail))) RETURNING smtpStatus, smtpMessage
IF smtpStatus >= 400 THEN
CALL smtpError(mc, smtpMessage)
RETURN FALSE
END IF
FOR i = 1 TO rcpt.getLength()
CALL smtpSend(mc, rcpt[i]) RETURNING smtpStatus, smtpMessage
IF smtpStatus >= 400 THEN
CALL smtpError(mc, smtpMessage)
RETURN FALSE
END IF
END FOR
CALL smtpSend(mc, "DATA\r") RETURNING smtpStatus, smtpMessage
IF smtpStatus >= 400 THEN
CALL smtpError(mc, smtpMessage)
RETURN FALSE
END IF
{ FOR i = 1 TO mailbody.getLength()
CALL clientWrite(mc, mailbody[i])
END FOR}
CALL readSmtpAnswer(mc) RETURNING smtpStatus, smtpMessage
IF smtpStatus >= 400 THEN
CALL smtpError(mc, smtpMessage)
RETURN FALSE
END IF
CALL smtpSend(mc, "QUIT\r") RETURNING smtpStatus, smtpMessage
IF smtpStatus >= 400 THEN
CALL smtpError(mc, smtpMessage)
RETURN FALSE
END IF
CALL mc.close()
RETURN TRUE
END FUNCTION
FUNCTION smtpSend(ch, command)
DEFINE ch base.Channel
DEFINE command STRING
DEFINE smtpStatus INTEGER
DEFINE smtpMessage STRING
-- CALL clientWrite(ch, command)
CALL readSmtpAnswer(ch) RETURNING smtpStatus, smtpMessage
RETURN smtpStatus, smtpMessage
END FUNCTION
FUNCTION default_maildemo_cnf()
LET cnf.smtpPort = 587
LET cnf.smtpHost = "smtp.office365.com"
LET cnf.senderEmail = "nominas@marmotech.com.do"
LET cnf.hostname = ""
LET cnf.cnxtimeout = 10
END FUNCTION
FUNCTION readSmtpAnswer(ch)
DEFINE ch base.Channel
DEFINE line STRING
DEFINE smtpStatus INTEGER
DEFINE smtpMessage STRING
LET smtpMessage = ""
WHILE 1
LET line = serverRead(ch)
IF line IS NULL THEN
RETURN -1, "COULD NOT READ SMTP ANSWER"
END IF
IF line MATCHES "[0-9][0-9][0-9] *" THEN
IF smtpMessage.getLength() != 0 THEN
LET smtpMessage=smtpMessage || "\n"
END IF
LET smtpMessage=smtpMessage.append(line.subString(4, line.getLength()))
LET smtpStatus = line.subString(1,3)
RETURN smtpStatus, smtpMessage
END IF
IF line MATCHES "[0-9][0-9][0-9]-*" THEN
IF smtpMessage.getLength() != 0 THEN
LET smtpMessage=smtpMessage || "\n"
END IF
LET smtpMessage=smtpMessage.append(line.subString(4, line.getLength()))
END IF
END WHILE
END FUNCTION
FUNCTION smtpError(mc, msg)
DEFINE msg STRING
DEFINE mc base.Channel
DEFINE smtpStatus INTEGER
DEFINE smtpMessage STRING
DISPLAY "SMTP Error", msg
CALL smtpSend(mc, "QUIT\r") RETURNING smtpStatus, smtpMessage
CALL mc.close()
END FUNCTION
FUNCTION extract_email(email)
DEFINE email STRING
DEFINE s, e INTEGER
LET s = email.getIndexOf("<", 1)
LET e = email.getIndexOf(">", 1)
IF s == 0 OR e == 0 OR s >= e THEN
RETURN email
END IF
RETURN email.subString(s + 1, e - 1)
END FUNCTION
FUNCTION serverRead(mc)
DEFINE line STRING
DEFINE mc base.Channel
LET line = mc.readLine()
CALL smtplog.append("<FONT COLOR=red><B>S&gt; </B>")
CALL smtplog.append(line)
CALL smtplog.append("<BR></FONT>")
LET logtext=smtplog.toString()
RETURN line
END FUNCTION