Sending a batch of emails from within a sB program.

Post Reply
Henko
Posts: 882
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Sending a batch of emails from within a sB program.

Post by Henko » Sun Apr 07, 2019 6:54 pm

Some applications require that a batch of emails be sent to selected users about a specific subject, for instance payements that are due. In a club application there might be other aspects that should be communicated to groups of members of varying composition.
I asked my son Michiel for ideas to enable such functionality within a sB program and he came up with a nice solution.

We took an free account with one of the mail providers, wich offers a (limited) free mail service: sendgrid.com. They offer an HTTP API, whith which one can define an email to be sent to a number of email adresses. This is sent from the sB program using the HTTP POST command and they will then send the email to the adresses.
When making an account with them one enters a userid (we took my email adress) and one asks for a API usage code which must be copied and saved. The userid and API code are needed to autenticate each request on their service.
The free service includes a maximum of 100 emails per day.

In the code segment hereafter some fields like the user id and API code are replaced by descriptions between <>. You have to substitute your own data.
The program is in the form of a function. A little "main" program prepares the parameters to be passed to the function.

While testing the program, the HTTP RESPONSE$() command was usefull. It is outcommented at the end of the function.

Code: Select all

' main program, where the selection of email adresses is made and
'   the subject text and the body text for the emails are defined
'
dim adresses$(20)            ' enlarge if needed
self$ = <your email adress>  ' sendgrid account user name
subject$ = "mailto test"
message$ = "We did not receive your contribution yet. Best regards, the Treasurer"

' simulate the result of the selection process,
adresses$(0) = <1st email adress>
adresses$(1) = <2nd email adress>
n_emails=2           '  2 emails should be sent

mailto(self$,adresses$,n_emails,subject$,message$)
end


' send a batch of emails to selected adresses
' using mail provider sendgrid.com (free account and scheme)
' sender$      = your own email adress (the sender)
' recipient$() = string array with recipient email adresses
' n_emails     = amount of adresses in recipient$() array
' subject$     = text for email subject
' message$     = text for the body of the email
' info between <...> to be replaced by specific user data
'
'
def mailto(sender$,recipient$(),n_emails,subject$,message$)

' Prepare the email variables
ob=option_base ! option base 1
url$ = "https://api.sendgrid.com/v3/mail/send"

header$(1) = "Authorization: Bearer <API code as recieved from the sendgrid provider>"
header$(2) = "Content-Type: application/json"

' Prepare a JSON for the HTTP request

payload$ = ""
payload$ &= "{ "
payload$ &= "   ""personalizations"":[ "
payload$ &= "      { "
payload$ &= "         ""to"":[ "
for i=1 to n_emails-1
  payload$ &= "            {""email"":""" & recipient$(i) & """}, "
  next i
payload$ &= "            {""email"":""" & recipient$(n_emails) & """} "
payload$ &= "         ] "
payload$ &= "      } "
payload$ &= "   ], "
payload$ &= "   ""from"":{ "
payload$ &= "      ""email"":""" & sender$ & """ "
payload$ &= "   }, "
payload$ &= "   ""subject"":""" & subject$ & """, "
payload$ &= "   ""content"":[ "
payload$ &= "      { "
payload$ &= "         ""type"":""text/plain"", "
payload$ &= "         ""value"":""" & message$ & """ "
payload$ &= "      } "
payload$ &= "   ] "
payload$ &= "}"

header$(3) = "content-length: " & len(payload$)

' send via HTTP POST

HTTP url$ HEADER header$ POST payload$

' print HTTP_RESPONSE$ ()    in case of problems

option base ob
end def

User avatar
rbytes
Posts: 1963
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone X
iPad 4
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Sending a batch of emails from within a sB program.

Post by rbytes » Tue Apr 09, 2019 5:35 pm

Maybe this will be a better way to send Christmas letters and cards next December. Think of the trees we will save.
Zzzzz

Henko
Posts: 882
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Sending a batch of emails from within a sB program.

Post by Henko » Wed Apr 10, 2019 8:59 am

I am not sure if files like .wav, jpg, etc. can be specified in the JSON message, but one of the fields is named: "content type", so maybe it is possible. I will have a look at it.

Henko
Posts: 882
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Sending a batch of emails from within a sB program.

Post by Henko » Wed Apr 10, 2019 3:38 pm

Yes,
Attaching files like .wav and .jpg is possible (says my informant😁).

User avatar
rbytes
Posts: 1963
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone X
iPad 4
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Sending a batch of emails from within a sB program.

Post by rbytes » Thu Apr 11, 2019 12:37 am

I will set up my free account this week and give it a try. Thanks, Henk!
Zzzzz

Post Reply