Sunday, September 10, 2017

Sparkpost and Alpha Five - Fantastic

September 10th 2017

Sparkpost and Alphafive - Fantastic

part 2:

Okay, now that you have configured the sparkpost, if you have not read that please go back and take a look at part 1 on this series, now you are ready to configure alpha to work with sparkpost to send emails from alphafive.

First and foremost we are going to create a table and store the api key there, and when needed in our xbasic function we will retrieve that api key via a sql lookup.  This way the key is not exposed to unwanted elements. Create a table in your back end db, in my case it is MySql. The table is going to have only one important field and only one record.  You may, however, add more fields and able to use them in your emails as you please.  In our example the table will hold
id primary key, auto increment, integer, length 2,
api_key varchar, not null, length 50.
and you can set the table name anything you want, for example mine is, sparkpost_api. Once it is done enter the sparkpost api key in to the field and save it. Now let's check if it works okay.
Go to interactive window and type in
?sql_lookup("::Name::local_mysql","sparkpost_api","id=1","api_key")
and it will spit out the api key. For example, if your api key is "they shoot the horses, don't they?" it will print that. If that is working then well and good, you can go to the next step.  If not go back and correct any mistakes then you are set to go.

Next, we are going to test with sparkpost.  Log into sparkpost and take a look at api documents.  They give examples for various languages and SMTP_Relay. we will be using SMTP_Relay since it lot easier to integrate with alpha and takes only few lines of code. Take note of host, port, user_name and password.  we will see if we can sign in to sparkpost using smtp_open method from alpha.
So again in your interactive window type in

dim ps as p
dim pm as p
?email_smtp_open(ps, "smtp.sparkpostmail.com", 587, "SMTP_Injection", sql_lookup("::Name::local_MySql", "sparkpost_api", "ID=1", "api_key"), "STARTTLS")

it should come out .t. If it does you are good to go, if not go back and correct any mistakes and re do till you succeed.
Next, we will create a simple xbasic function and save that function. Go to regular control panel and click on the code tab and select new and function. Name the function anything you like, mine is email_sparkpost_send. The result is logical and the parameters are to: character, subject: character, message: character. When written the function should look like this:

FUNCTION email_sparkpost_send AS L (to AS C, subject AS C, message AS C )
dim result as l
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 = "you@yourdomain.com"
pm.from_alias = "Your Name"
pm.to = to
pm.subject = subject
pm.message = message
result = email_smtp_send(pm,ps)
end if
email_smtp_close(ps)
email_sparkpost_send = result
END FUNCTION

Now we are going to test this with a dialog component and see if all works as expected.
Create a dialog with three controls: 1> to text, 2> subject text 3> message text-area and add a button > send email. On the onClick event call an xbasic function to cal this email_sparkpost_send.
See if the message is sent and an alert appears. It does for me.
If all goes well you have sent an email with sparkpost and alphafive, isn't that wonderful?
Congratulations.

Next we will see how to send multiple emails at one shot. That's for next week.

No comments:

Post a Comment