FoxPro Programming
Twilio SendGrid for email sending with the West-Wind tools
Gravatar is a globally recognized avatar based on your email address. Twilio SendGrid for email sending with the West-Wind tools
  Russell Campbell
  All
  Sep 10, 2022 @ 06:27pm

I was wondering if anyone is using the West Wind tools to send email via Twilio's SendGrid API. I use Twilio for sending texts with the HTTPGet method in wwHTTP. Works great. Sending mail is a bit more complicated. Suggestions or examples would be appreciated.

Gravatar is a globally recognized avatar based on your email address. re: Twilio SendGrid for email sending with the West-Wind tools
  Russell Campbell
  Russell Campbell
  Sep 19, 2022 @ 07:38am

I'll take that as a "no." 😃

Gravatar is a globally recognized avatar based on your email address. re: Twilio SendGrid for email sending with the West-Wind tools
  Rick Strahl
  Russell Campbell
  Sep 20, 2022 @ 08:51pm

Doesn't mean it doesn't work though - just that nobody's done it and is willing to share. The APIs should be easy to access with wwHttp or possibly wwJsonServiceClient.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Twilio SendGrid for email sending with the West-Wind tools
  Russell Campbell
  Rick Strahl
  Sep 22, 2022 @ 07:00am

Well, sorting through the details is troublesome given the docs from Twilio. I don’t do this type of thing a lot, so it’s not second nature. Another person, can’t think of their name right now, has tools for this for VFP, so I may look into that. Some you have to pay for, some not. Not sure where the tools for SendGrid fall.

Gravatar is a globally recognized avatar based on your email address. re: Twilio SendGrid for email sending with the West-Wind tools
  Russell Campbell
  Rick Strahl
  Sep 24, 2022 @ 07:40pm

I'm trying to convert the examples he gives (see the previous link) and use your tools, but I'm unclear how to accomplish it. I created the appropriate object and used the JSON serializer to get the JSON created, but then posting it is a different challenge.

oEmail = createobject("Empty")
addproperty(oEmail, "personalizations", createobject("Empty"))
addproperty(oEmail.personalizations, "to", createobject("Empty"))
addproperty(oEmail.personalizations.to, "email", "someone@somewhere.com")
addproperty(oEmail.personalizations.to, "name", "Russell Campbell")
addproperty(oEmail, "from", createobject("Empty"))
addproperty(oEmail.from, "email", "someone@somewhere.com")
addproperty(oEmail, "subject", "Hello, world")
addproperty(oEmail, "content", createobject("Empty"))
addproperty(oEmail.content, "type", "text/plain")
addproperty(oEmail.content, "value", "Heya!")
do classes\wwJsonSerializer.prg
loSer = CREATEOBJECT("wwJsonSerializer")
lcJson =  loSer.Serialize(oEmail)

So that works out ok, but then posting it is not so obvious (to me, at least). Any thoughts (or sample code) here would be appreciated.

Gravatar is a globally recognized avatar based on your email address. re: Twilio SendGrid for email sending with the West-Wind tools
  Russell Campbell
  Russell Campbell
  Sep 27, 2022 @ 06:07am

Still was not sure what to use to get the API working, but I did discover that I had overlooked the step to set up my DNS entries. Had to create three CNAME records. Then was able to test using Telnet and that confirmed they could send email on my behalf. And then I noticed something else I'd overlooked which was I could send the email via a standards SMTP interface and that's working. My volume will be low, so that should suffice, but I would eventually like to get this working via the API.

Gravatar is a globally recognized avatar based on your email address. re: Twilio SendGrid for email sending with the West-Wind tools
  Phil Sherwood
  Russell Campbell
  Sep 27, 2022 @ 07:50am

We had to use the Chilkat tools to send and schedule emails with SendGrid. I'd be happy to share our code if you're interested.

Phil

Gravatar is a globally recognized avatar based on your email address. re: Twilio SendGrid for email sending with the West-Wind tools
  Rick Strahl
  Russell Campbell
  Sep 27, 2022 @ 02:22pm

Make sure you have the right case for each of those JSON keys as JSON is case sensitive.

You may need to use wwJsonSerializer::PropertyNameOverrides to force the property names to their proper case.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Twilio SendGrid for email sending with the West-Wind tools
  Russell Campbell
  Phil Sherwood
  Sep 28, 2022 @ 07:32am

Thanks, Phil. I was trying to avoid buying another toolkit if Rick's would do it. And there are a number of VFP examples on the Chilkat site, so I was assuming that if I did buy it they would suffice to get me rolling. But I appreciate the offer.

Gravatar is a globally recognized avatar based on your email address. re: Twilio SendGrid for email sending with the West-Wind tools
  Russell Campbell
  Rick Strahl
  Sep 28, 2022 @ 07:35am

Rick, I did wonder if I should be using the JSONService tools and not just wwHTTP. But it's unclear to me as to what SendGrid is really expecting, so I'm unclear what and how to use your tools to supply it. But, as I mentioned, I got it working via a standard SMTP interface, so that will work for now.

Gravatar is a globally recognized avatar based on your email address. re: Twilio SendGrid for email sending with the West-Wind tools
  Rick Strahl
  Russell Campbell
  Sep 28, 2022 @ 10:32am

The code you are using to generate the JSON is not correct - you're not accounting for the collections (arrays in JSON)...

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Twilio SendGrid for email sending with the West-Wind tools
  Rick Strahl
  Russell Campbell
  Sep 28, 2022 @ 10:36am

To generate the JSON that matches the Twilio template you showed, you want code like this:

CLEAR
DO wwJsonSerializer


lcEmailTo = "test@test.com"
lcEmailName = "Recipient Name"
lcEmailFrom = "test@test.com"
lcContent = "Message Content"
lcSubject = "Message Title"
lcBody = "Hello cruel world"

loData = CREATEOBJECT("Empty")

ADDPROPERTY(loData,"personalizations", CREATEOBJECT("Collection"))

loTo = CREATEOBJECT("EMPTY")
ADDPROPERTY(loTo,"email",lcEmailTo)
ADDPROPERTY(loTo,"name", lcEmailName)

loToParent = CREATEOBJECT("collection")
loToParent.Add(loTo)

loData.personalizations.Add(loToParent)


ADDPROPERTY(loData,"from", CREATEOBJECT("EMPTY"))
ADDPROPERTY(loData.from,"email",lcEmailFrom)

ADDPROPERTY(loData,"subject",lcSubject)

ADDPROPERTY(loData,"content", CREATEOBJECT("Collection"))

loContent = CREATEOBJECT("EMPTY")
ADDPROPERTY(loContent,"type","text/plain")
ADDPROPERTY(loContent,"value",lcBody)

loData.Content.Add(loContent)

loSer = CREATEOBJECT("wwJsonSerializer")
lcJson = loSer.Serialize(loData, .t.)

? lcJson

This produces:

{
  "content": [
    {
      "type": "text/plain",
      "value": "Hello cruel world"
    }
  ],
  "from": {
    "email": "test@test.com"
  },
  "personalizations": [
    [
      {
        "email": "test@test.com",
        "name": "Recipient Name"
      }
    ]
  ],
  "subject": "Message Title"
}

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Twilio SendGrid for email sending with the West-Wind tools
  Russell Campbell
  Rick Strahl
  Sep 28, 2022 @ 12:23pm

Ah, yes, the square brackets. I missed that. Thanks. I don't use Json a lot. But I didn't take it farther to likely discover that later because I was unsure what class from your tools would be used to post it.

Gravatar is a globally recognized avatar based on your email address. re: Twilio SendGrid for email sending with the West-Wind tools
  Rick Strahl
  Russell Campbell
  Sep 28, 2022 @ 04:07pm

Geez, Russell. I think you might have to actually read the documentation. 😄

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: Twilio SendGrid for email sending with the West-Wind tools
  Russell Campbell
  Rick Strahl
  Sep 28, 2022 @ 05:34pm

I've certainly looked over the SendGrid docs. They have different examples, but I can't always tell how the tools they use equate to what you're offering. And I don't do a lot of web-based API stuff. I'm sure you think it's a cake walk, but then it's been your bread and butter for quite awhile. One thing about tech docs is that they all too often are written by the programmers that created the system. Often, they really can't divorce themselves from the knowledge of the system and remember what it was like when they didn't have that knowledge. So all kinds of assumptions get made because they know it like the back of their hand. Of course, quality varies, but it is a difficult thing for any programmer to do - remember what you didn't know and therefore know how to explain it to someone who's a programmer, but is not well-versed in some technology you now know intimately. I've also found the Twilio docs to be outdated at times or just plain wrong. I was working with their docs for a program to receive replies from texts I sent out. Followed it to the T. Didn't work. Their TelNet example didn't work at first until I noticed that the details didn't match their summary of commands they put at the end. Just two lines that were reversed, no code changes. Did it that way and it worked, yet it was just about assigning TO and FROM and I wouldn't have expected it to be sensitive to the order. Seems it was. Frankly, I look for tools with lots of examples. I have lots on my plate and reading docs is less preferable than working with examples that work. Not that I never read docs. I've read yours a fair amount.

© 1996-2024