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