1. LeVerne Johnson
  2. PowerBuilder
  3. Monday, 13 March 2023 15:45 PM UTC

 I am using this code to send out multiple reports to multiple recipients.  The problem is that it's only sending the first report to the multiple recipients.

EXAMPLE:

 

User1@xx.com - Should get Report #1

User2@xx.com - Should get Report #1

 

User1@xxcom - Should get Report #2

User2@xx.com - Should get Report #2

 

What's happening is that User1@xx.com and User2@xx.com will get Report #1 twice.  Report #2 is never picked up and mailed out to recipients.

 

string ls_email, ls_link,ls_freq, ls_rptnbr,ls_message,ls_address, ls_msg, ls_subject, ls_file, ls_text, path, ls_attach,ls_location,ls_reportonly, ls_analyst
string ls_recipient, ls_ccrecipient, ls_bccrecipient, ls_attachment, ls_sender, ls_rec, ls_run, ls_analystabr, ls_sdate,ls_fileattach[]
string ls_filename[],ls_mimetype,ls_boundary,ls_url,ls_json, is_server, is_userid, is_passwd
date ldt_randate, ldt_today
boolean lb_run ;lb_run = FALSE
boolean lb_count ;lb_count = FALSE
Boolean lb_html, lb_Return
blob blb_file[],blb_boundary,blb_terminus,blb_multipart
blob blb_recipient,blb_subject,blb_message
integer ll_filenum,li_count,li_nbr, li_rptnbr, ls_ret, li_seqno
UInt lui_port

HttpClient inv_httpClient
inv_httpClient = CREATE HttpClient
inv_httpClient.SecureProtocol=5 /*TLS 1.2*/

SetPointer(HourGlass!)

lb_html = False
lui_port =25
is_userid = <USER EMAIL>
is_passwd = <PASSWORD>

select sysdate
into :ldt_today
from dual;

ls_sdate = string(date(ldt_today))

DECLARE GetEAddress CURSOR FOR
SELECT tolist(e_address), rpt_nbr, frequency, file_location, Report_Only, analyst, seq_no
FROM <TABLE>
group by rpt_nbr, frequency, file_location, Report_Only, analyst, seq_no;

OPEN GetEAddress;

if sqlca.SQLCODE <> 0 then
messagebox("No","No records")

CLOSE GetEAddress;
return
else
FETCH GetEAddress
INTO :ls_email, :li_rptnbr, :ls_freq, :ls_link, :ls_reportonly, :ls_analyst, :li_seqno;
end if

IF sqlca.SQLCODE = 0 then
DO WHILE ( sqlca.SQLCODE = 0 )

if sqlca.SQLNRows <> 0 then
ls_text = string(li_rptnbr) +" - " + ls_analyst

select subject, message, file_location, run
into :ls_subject, :ls_msg, :ls_file, :ls_run
from <TABLE>
where <>

choose case li_rptnbr
case IS > 1
if ls_run = 'Y' then
lb_run=TRUE
SELECT tolist(e_address)
INTO :ls_email
FROM <TABLE>
where <>
order by e_address asc;

ls_attach = mid(ls_file, lastpos(ls_file,'\')+1,120)
path = left(ls_file, lastpos(ls_file,'\') )
blb_recipient = blob('Content-Disposition: form-data; name="Recipient"' + '~r~n~r~n' + ls_email, EncodingUTF8!)
blb_subject = blob('Content-Disposition: form-data; name="Subject"' + '~r~n~r~n' + ls_subject, EncodingUTF8!)
blb_message = blob('Content-Disposition: form-data; name="Message"' + '~r~n~r~n' + ls_msg, EncodingUTF8!)

if isnull(ls_msg) then
ls_msg = ""
ls_message = ls_msg
end if

ls_attachment = path + ls_attach

/* Get attachments and read into a blob */
li_count = 1
For li_nbr = 1 To li_count
ls_fileattach[li_nbr] = ls_attachment
ll_FileNum = FileOpen(ls_fileattach[li_nbr], StreamMode!)
FileReadEx(ll_FileNum, blb_file[li_nbr])
FileClose(ll_FileNum)
ls_FileName[li_nbr] = ls_fileattach[li_nbr]
Next
ls_MimeType = 'application/pdf'

/* Create a boundary marker for the multipart blob */
ls_BOUNDARY = "$$$Boundary$$$"

/* Create component blobs */
blb_boundary = blob('~r~n--' + ls_Boundary + '~r~n', EncodingUTF8!)
blb_terminus = blob('~r~n--' + ls_Boundary + '--~r~n', EncodingUTF8!)

For li_nbr = 1 To li_count
blb_file[li_nbr] = blob( 'Content-Disposition: form-data; name="file"; filename="' + ls_FileName[li_nbr] + '"' + '~r~n' + &
"Content-Type: " + ls_MimeType + "~r~n~r~n", EncodingUTF8!) + &
blb_file[li_nbr]
Next

/* Concatenate blobs into a single multipart blob */
For li_nbr = 1 To li_count
blb_multipart = blb_multipart + blb_boundary + &
blb_file[li_nbr]
Next
blb_multipart = blb_multipart + blb_boundary + &
blb_recipient + &
blb_boundary + &
blb_subject + &
blb_boundary + &
blb_message + &
blb_terminus

/* set email properties */
ls_location = "TEXT"

if li_rptnbr = 999 then
blb_subject = blob('Content-Disposition: form-data; name="Subject"' + '~r~n~r~n' + 'Updated Reports', EncodingUTF8!)
blb_message = blob('Content-Disposition: form-data; name="Message"' + '~r~n~r~n' + ls_location, EncodingUTF8!)
end if


if Not IsNull(ls_subject) and li_rptnbr <> 300 then
integer li_rtn

/* HTTPClient */
li_rtn = inv_httpClient.clearrequestheaders()
li_rtn = inv_httpClient.SetRequestHeader("Content-Type", "multipart/form-data; boundary=" + ls_BOUNDARY)

if isnull(li_rtn) or li_rtn <> 1 then messagebox('SetRequestHeader Failed',string(li_rtn))
ls_url = 'https://mailapi.dob.texas.gov/api/mail/sendmailattachment'
li_rtn = inv_httpClient.SendRequest('POST', ls_url, blb_multipart)
li_rtn = inv_httpClient.GetResponseBody(ls_json)
end if
end if
end choose
end if
FETCH NEXT GetEAddress
INTO :ls_email, :li_rptnbr, :ls_freq, :ls_link, :ls_reportonly, :ls_analyst, :li_seqno;
Loop;
END IF
CLOSE GetEAddress;

 

 

LeVerne Johnson Accepted Answer Pending Moderation
  1. Monday, 13 March 2023 19:32 PM UTC
  2. PowerBuilder
  3. # 1

Just curious: Why use a CHOOSE CASE if you only have one case?

Using CHOOSE CASE because there are several reports.

Have you debugged your code?

Yes, we have debugged the code and it appeared that it should do what's expected, but does not.

 

What happens if you only do the SendRequest('POST' with a single attachment, trying to send the second one)?

If we choose one report to go to several recipients, it works as expected.  It will send that one report to several recipients.

The problem come in when there's more than one report to email out.  For example, if two reports needs to go to two recipients, the code will send the first report to both recipients twice.  It never sends out the second report.

 

Comment
  1. Miguel Leeuwe
  2. Monday, 13 March 2023 20:23 PM UTC
Hi,

this is why I think it has something to do with the way you concatenate the attachments. As I've said already, I don't know anything about this way of sending email. We either use smtp, not using an http client or by using ole.

Sorry, I can't be of more help on this one.

regards.
  1. Helpful
  1. Sivaprakash BKR
  2. Tuesday, 14 March 2023 07:19 AM UTC
2nd is not sent or first one doesn't get cleared. May be the code is sending only the first attachment and leave out the other attachments. Have you checked whether the first attachment is cleared in the loop, if any.

  1. Helpful
  1. John Fauss
  2. Tuesday, 14 March 2023 13:19 PM UTC
Unless you already have documentation that clearly states that what you are wanting to do is supported by the web API or you have working code from a non-PB implementation, I suggest you contact the publisher to verify that what you are attempting is valid. It might not be possible.
  1. Helpful
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Monday, 13 March 2023 18:22 PM UTC
  2. PowerBuilder
  3. # 2

1. Please read the first post on this Q&A.

2. Just curious: Why use a CHOOSE CASE if you only have one case?

3. Have you debugged your code?

regards.

string ls_email, ls_link,ls_freq, ls_rptnbr,ls_message,ls_address, ls_msg, ls_subject, ls_file, ls_text, path, ls_attach,ls_location,ls_reportonly, ls_analyst
string ls_recipient, ls_ccrecipient, ls_bccrecipient, ls_attachment, ls_sender, ls_rec, ls_run, ls_analystabr, ls_sdate,ls_fileattach[]
string ls_filename[],ls_mimetype,ls_boundary,ls_url,ls_json, is_server, is_userid, is_passwd
date ldt_randate, ldt_today
boolean lb_run ;lb_run = FALSE
boolean lb_count ;lb_count = FALSE
Boolean lb_html, lb_Return
blob blb_file[],blb_boundary,blb_terminus,blb_multipart
blob blb_recipient,blb_subject,blb_message
integer ll_filenum,li_count,li_nbr, li_rptnbr, ls_ret, li_seqno
UInt lui_port

HttpClient inv_httpClient
inv_httpClient = CREATE HttpClient
inv_httpClient.SecureProtocol=5 /*TLS 1.2*/

SetPointer(HourGlass!)

lb_html = False
lui_port =25
is_userid = <USER EMAIL>
is_passwd = <PASSWORD>

select sysdate
into :ldt_today
from dual;

ls_sdate = string(date(ldt_today))

DECLARE GetEAddress CURSOR FOR
SELECT tolist(e_address), rpt_nbr, frequency, file_location, Report_Only, analyst, seq_no
FROM <TABLE>
group by rpt_nbr, frequency, file_location, Report_Only, analyst, seq_no;

OPEN GetEAddress;

if sqlca.SQLCODE <> 0 then
	messagebox("No","No records")
	CLOSE GetEAddress;
	return
else
	FETCH GetEAddress
	INTO :ls_email, :li_rptnbr, :ls_freq, :ls_link, :ls_reportonly, :ls_analyst, :li_seqno;
end if

IF sqlca.SQLCODE = 0 then
	DO WHILE ( sqlca.SQLCODE = 0 )
		if sqlca.SQLNRows <> 0 then
			ls_text = string(li_rptnbr) +" - " + ls_analyst
			select subject, message, file_location, run
			into :ls_subject, :ls_msg, :ls_file, :ls_run
			from <TABLE>
			where <>

			choose case li_rptnbr
				case IS > 1
					if ls_run = 'Y' then
						lb_run=TRUE
						SELECT tolist(e_address)
						INTO :ls_email
						FROM <TABLE>
						where <>
						order by e_address asc;

						ls_attach = mid(ls_file, lastpos(ls_file,'\')+1,120)
						path = left(ls_file, lastpos(ls_file,'\') )
						blb_recipient = blob('Content-Disposition: form-data; name="Recipient"' + '~r~n~r~n' + ls_email, EncodingUTF8!)
						blb_subject = blob('Content-Disposition: form-data; name="Subject"' + '~r~n~r~n' + ls_subject, EncodingUTF8!)
						blb_message = blob('Content-Disposition: form-data; name="Message"' + '~r~n~r~n' + ls_msg, EncodingUTF8!)

						if isnull(ls_msg) then
							ls_msg = ""
							ls_message = ls_msg
						end if

						ls_attachment = path + ls_attach

						/* Get attachments and read into a blob */
						li_count = 1
						For li_nbr = 1 To li_count
							ls_fileattach[li_nbr] = ls_attachment
							ll_FileNum = FileOpen(ls_fileattach[li_nbr], StreamMode!)
							FileReadEx(ll_FileNum, blb_file[li_nbr])
							FileClose(ll_FileNum)
							ls_FileName[li_nbr] = ls_fileattach[li_nbr]
						Next
						ls_MimeType = 'application/pdf'

						/* Create a boundary marker for the multipart blob */
						ls_BOUNDARY = "$$$Boundary$$$"

						/* Create component blobs */
						blb_boundary = blob('~r~n--' + ls_Boundary + '~r~n', EncodingUTF8!)
						blb_terminus = blob('~r~n--' + ls_Boundary + '--~r~n', EncodingUTF8!)

						For li_nbr = 1 To li_count
							blb_file[li_nbr] = blob( 'Content-Disposition: form-data; name="file"; filename="' + ls_FileName[li_nbr] + '"' + '~r~n' + &
							"Content-Type: " + ls_MimeType + "~r~n~r~n", EncodingUTF8!) + &
							blb_file[li_nbr]
						Next

						/* Concatenate blobs into a single multipart blob */
						For li_nbr = 1 To li_count
							blb_multipart = blb_multipart + blb_boundary + &
							blb_file[li_nbr]
						Next
						blb_multipart = blb_multipart + blb_boundary + &
						blb_recipient + &
						blb_boundary + &
						blb_subject + &
						blb_boundary + &
						blb_message + &
						blb_terminus

						/* set email properties */
						ls_location = "TEXT"

						if li_rptnbr = 999 then
							blb_subject = blob('Content-Disposition: form-data; name="Subject"' + '~r~n~r~n' + 'Updated Reports', EncodingUTF8!)
							blb_message = blob('Content-Disposition: form-data; name="Message"' + '~r~n~r~n' + ls_location, EncodingUTF8!)
						end if


						if Not IsNull(ls_subject) and li_rptnbr <> 300 then
							integer li_rtn

							/* HTTPClient */
							li_rtn = inv_httpClient.clearrequestheaders()
							li_rtn = inv_httpClient.SetRequestHeader("Content-Type", "multipart/form-data; boundary=" + ls_BOUNDARY)

							if isnull(li_rtn) or li_rtn <> 1 then messagebox('SetRequestHeader Failed',string(li_rtn))
							ls_url = 'https://mailapi.dob.texas.gov/api/mail/sendmailattachment'
							li_rtn = inv_httpClient.SendRequest('POST', ls_url, blb_multipart)
							li_rtn = inv_httpClient.GetResponseBody(ls_json)
						end if
					end if
			end choose
		end if
		FETCH NEXT GetEAddress
		INTO :ls_email, :li_rptnbr, :ls_freq, :ls_link, :ls_reportonly, :ls_analyst, :li_seqno;
	Loop
END IF
CLOSE GetEAddress;
Comment
  1. Miguel Leeuwe
  2. Monday, 13 March 2023 20:27 PM UTC
Do you have any information about the api https://mailapi.dob.texas.gov/api/mail/sendmailattachment on how to send multiple attachements in one go?
  1. Helpful
  1. LeVerne Johnson
  2. Monday, 13 March 2023 20:35 PM UTC
No, I do not. But, I will ask the developer who created that API.
  1. Helpful 1
  1. Miguel Leeuwe
  2. Monday, 13 March 2023 20:38 PM UTC
Yes, that's a very good start! Maybe it just needs some special separator / who knows!
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.