Sunday, September 17, 2017

Sparkpost and Alpha Five - Fantastic

September 17th, 2017

Sparkpost and AlphaFive - Fantastic

part 3:

Okay, now that we have some working examples, we are going to send email to multiple recipients.

First things first. Email etiquette dictates that you do not give out others' email addresses without their consent.  So in order to do that NEVER send an email with multiple mail addresses in the 'to' or 'cc' address locations, they are visible to everyone. Always send the email to yourself and 'bcc' to all the recipients.  In this way the email addresses are hidden from others.

In AlphaFive when you send an email via any one of the email send functions the to, cc and bcc will take comma separated list. That is what we are going to use to send the emails via sparkpost and smtp_send function.

Secondly, you really do not need to write out a separate function and then use it to send an email.  Since it takes only about 10 lines of code you can easily add it to the email code and done with it.  So any modification you do will immediately be propagated to the server, instead of republishing all the files to upload a small change you did in the UDF.

So what we need to do today is to build a brand new dialog that will have the following controls:
Subject, a textbox, this is the subject.
Message, a textarea, this is the message we are sending.
a button labelled 'send email, to send email by calling the xbasic function and send out alert to the browser.

we are going to select all the members of the table and send out simple email using the function we created earlier.
Okay here is the code to do that:
function xb as c (e as p)
'debug(1)
dim subject as c
dim message as c
subject = e.dataSubmitted.subject
message = e.dataSubmitted.message
dim cn as sql::Connection
dim result as l
dim sqlSelect as c
dim rs as sql::ResultSet
dim list as c = ""
dim flag as l = .f.
dim msg as c
result = cn.Open("::Name::local_MySql")
if result then
sqlSelect = "SELECT * FROM customer2"
result = cn.Execute(sqlSelect)
if result then
rs = cn.ResultSet
flag = rs.nextRow()
while flag
list = list + rs.data("email")+","
flag = rs.nextRow()
end while
if substr(list,len(list)) = "," then
list = substr(list,1,len(list)-1)
end if
result = email_sparkpost_send(list,subject,message)
if result then
msg = "alert('email sent to recipents in the list');"
else
msg = "alert('there was a problem sending email, try again later');"
end if
end if
cn.close()
end if
xb = msg
end function

This will send emails to all from customer2 table with exposed email address in the 'to' column.
so we will redo the code to send with hidden email address and incorporating the email send function in the xbasic function itself.
So here is the modified code to do that:
function xb as c (e as p)
'debug(1)
dim subject as c
dim message as c
subject = e.dataSubmitted.subject
message = e.dataSubmitted.message
dim cn as sql::Connection
dim result as l
dim sqlSelect as c
dim rs as sql::ResultSet
dim list as c = ""
dim flag as l = .f.
dim msg as c
result = cn.Open("::Name::local_MySql")
if result then
sqlSelect = "SELECT * FROM customer2"
result = cn.Execute(sqlSelect)
if result then
rs = cn.ResultSet
flag = rs.nextRow()
while flag
list = list + rs.data("email")+","
flag = rs.nextRow()
end while
if substr(list,len(list)) = "," then
list = substr(list,1,len(list)-1)
end if
       dim ps as p dim pm as p result = email_smtp_open(ps,"smtp.sparkpostmail.com",587,"SMTP_Injection",sql_lookup("::Name::local_MySql","sparkpost_api","ID=1","api_key"),"STARTTLS") if result then pm.from = "administrator@niniswinecellar.com" pm.from_alias = "Govindan Gandhi" pm.to = "ggandhi344@gmail.com" p.bcc = list pm.subject = subject pm.message = message result = email_smtp_send(pm,ps) end if         email_smtp_close(ps)
                if result then
msg = "alert('email sent to recipents in the list');"
else
msg = "alert('there was a problem sending email, try again later');"
end if                
end if
cn.close()
end if
xb = msg
end function

See if this works well before we go into sending email with html markup.

The next week article will finalize with HTML markup and images on the body of the email.

No comments:

Post a Comment