For those wishing to use SparkPost but not at the current pre-release level, here's an example using the SparkPost REST API and cURL.
This XBasic function is called from the Action Javascript "Open a Report, Label or Letter Layout". Although this example utilizes the Action Javascript Open Report code, the SparkPost setup and JSON generation can be used in any XBasic function for sending emails via SparkPost. This example uses the SparkPost sandbox email account. You'll need to go through the painful process of establishing and verifying your Sending Domain. Painful only because we only do this once a year... or a lifetime... and thereby... painful.
This XBasic function is called from the Action Javascript "Open a Report, Label or Letter Layout". Although this example utilizes the Action Javascript Open Report code, the SparkPost setup and JSON generation can be used in any XBasic function for sending emails via SparkPost. This example uses the SparkPost sandbox email account. You'll need to go through the painful process of establishing and verifying your Sending Domain. Painful only because we only do this once a year... or a lifetime... and thereby... painful.
Code:
function emailReportSP as c (e as p) debug(1) 'This function is called after the report has printed 'The e object that is passed in includes: 'e.tmpl - the component definition 'e.dataSubmitted - data from the component ' - the key where the binary data for the report that was printed is stored in session storage. 'dim key as c 'key = e.fileSessionKey 'The function can return Javascript to execute in the browser. 'Using the e.fileSessionKey value you can retrieve the binary data for the report 'For example dim b as b session.GetDataFromFile(b,e.fileSessionKey) dim size as n size = b.size() if size = 0 then emailReportSP = "alert('Report was not sent');" exit function end if dim sp as p sp.content.from = "[email protected]" sp.content.subject = "Attachment Test" sp.content.text = "Attachment Message content" 'recipients is an Array dim sp.recipients[0] as p sp.recipients[].address = "Your recipient email address goes here" 'attachments is an Array within content dim sp.content.attachments[0] as p sp.content.attachments[].type = "application/pdf" sp.content.attachments[1].name = "billing.pdf" sp.content.attachments[1].data = base64encode(b) 'this is an example of adding a 2nd attachment 'sp.content.attachments[].type = "text/plain; charset=UTF-8" 'sp.content.attachments[2].name = "explanation.txt" 'sp.content.attachments[2].data = "TW92ZSBhbG9uZy4gIE5vdGhpbmcgdG8gc2VlIGhlcmUu" dim spJSON as c spJSON = json_generate(sp,.f.,.t.) dim cf_1 as extension::CurlFile dim flag_1 as l dim slist1[0] as c = [ "Authorization: Your SparkPost REST API key goes here" , "Content-Type: application/json" ] dim ce as extension::Curl ce = extension::Curl.Init() ce.setOpt("URL","https://api.sparkpost.com/api/v1/transmissions") ce.setOpt("NOPROGRESS",1) dim posted_fields as c = (spJSON) ce.setOpt("POSTFIELDS",posted_fields) ce.setOpt("POSTFIELDSIZE_LARGE", len(posted_fields) ) ce.setOpt("USERAGENT","curl/7.34.0") ce.setOpt("HTTPHEADER",slist1) ce.setOpt("MAXREDIRS",50) ce.setOpt("CAINFO",a5.Get_Exe_Path()+"\caroot\ca-cert.pem") ce.setOpt("CAPATH",a5.Get_Exe_Path()+"\caroot") ce.setOpt("CUSTOMREQUEST","POST") ce.setOpt("TCP_KEEPALIVE",1) ce.SetOpt("FILE",cf_1) flag_1 = ce.Exec() if flag_1 then dim headers as c dim contents as c 'headers = cf_1.GetHeaders() contents = cf_1.GetContent() dim contObj as p contObj = json_parse(contents) if contObj.results.total_rejected_recipients > 0 then emailReportSP = "alert('we had a problem');" else emailReportSP = "alert('Total emails sent:" + contObj.results.total_accepted_recipients + "');" end if 'BE SURE TO COMMENT OUT THIS CODE IN A WEB APPLICATION AS showVar() CANNOT BE USED IN WEB APPLICATIONS 'dim msg as c 'msg = "Headers: " + crlf() + headers + crlf() + "Content: " + contents ''''showvar( msg) else dim errors as c errors = ce.error() 'BE SURE TO COMMENT OUT THIS CODE IN A WEB APPLICATION AS showVar() CANNOT BE USED IN WEB APPLICATIONS ''''showvar("error: " + errors) end if ce.close() end function
Comment