FoxPro Programming
SMTP2Go and REST API
Gravatar is a globally recognized avatar based on your email address. SMTP2Go and REST API
  Denis Chassé
  All
  Jan 22, 2024 @ 02:03am

Can you tell me how to process emails (Send, check if sent...) in SMTP2GO using REST API? Regards.

So far I tried this but not successfull

loHttp = CREATEOBJECT("wwHttp")

loHttp.AddPostKey("api_key","api-74033xxxxxxxxxxxxxxxxxxxxxxxxxxx")
loHttp.AddPostKey("to", "Moi <info@gestion-beaute.com>")
loHttp.AddPostKey("sender", "Encore moi <testmlm@gestion-beaute.com>")
loHttp.AddPostKey("subject", "Hello à moi")
loHttp.AddPostKey("text_body", "Je peux juste espérer.")

lcJson = loHttp.Post(" https://api.smtp2go.com/v3/email/send")

Gravatar is a globally recognized avatar based on your email address. re: SMTP2Go and REST API
  Rick Strahl
  Denis Chassé
  Jan 22, 2024 @ 10:27am

Uh... aren't you forgetting something?

Specs?

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: SMTP2Go and REST API
  Denis Chassé
  Rick Strahl
  Jan 22, 2024 @ 01:15pm

In the following line I don't show the entire api_key

loHttp.AddPostKey("api_key","api-74033xxxxxxxxxxxxxxxxxxxxxxxxxxx")
loHttp.AddPostKey("to", "Moi <info@gestion-beaute.com>")
loHttp.AddPostKey("sender", "Encore moi <testmlm@gestion-beaute.com>")
loHttp.AddPostKey("subject", "Hello a moi")
loHttp.AddPostKey("text_body", "Je peux juste esperer.")
lcJson = loHttp.Post("https://api.smtp2go.com/v3/email/send")

this is the error message that I received

{"request_id": "870bb7f4-b97b-11ee-b2ae-f23c9216bf47", "data": {"error": "An error occurred processing the json data you sent with the request, please make sure the data conforms to the specification for this call (see the documentation here: https://apidoc.smtp2go.com/documentation/#/README) and try again. Don't forget to set Content-Type to 'application/json'.", "error_code": "E_ApiResponseCodes.NON_VALIDATING_IN_PAYLOAD", "model_validation_errors": "Model Validation failed: Either 'text_body', 'html_body' or 'template_id' must be passed"}}

After that error message I tried to do the following

loHTTP.setRequestHeader("Content-Type", "application/json")

That didn't work. Not sure what to do now.

Do I need to set the content-type? If yes how do I do it? Anything else that would help me?

Gravatar is a globally recognized avatar based on your email address. re: SMTP2Go and REST API
  Rick Strahl
  Denis Chassé
  Jan 22, 2024 @ 06:57pm

You need to send a JSON string to the server... not post variables via AddPostKey().

You can use wwJsonSerializer or hand code the JSON.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: SMTP2Go and REST API
  Denis Chassé
  Rick Strahl
  Jan 23, 2024 @ 05:18am

Here is what I did

loSMTP = CREATEOBJECT("wwJsonSerializer")
ADDPROPERTY(loSMTP,"api_key", "api-74033xxxxxxxxxxxxxxxxxxxxxxxxxxx")
ADDPROPERTY(loSMTP,"to", "Moi <info@gestion-beaute.com>")
ADDPROPERTY(loSMTP,"sender", "Encore moi <testmlm@gestion-beaute.com>")
ADDPROPERTY(loSMTP,"subject", "Hello a moi")
ADDPROPERTY(loSMTP,"text_body", "Je peux juste esperer.")

After I'm done serializing what is the next step. I mean how do I send my email.

lcJson = loHttp.Post("https://api.smtp2go.com/v3/email/send")

But then what do I add to the line. Should I do it this way?

lcJson = loHttp.Post("https://api.smtp2go.com/v3/email/send", loSMTP)
Gravatar is a globally recognized avatar based on your email address. re: SMTP2Go and REST API
  Denis Chassé
  Denis Chassé
  Jan 23, 2024 @ 06:08am

Don't reply Rick. I think I found what I needed. jsonserviceclient. If I don't understand something I'll get back to you.

regardss

Gravatar is a globally recognized avatar based on your email address. re: SMTP2Go and REST API
  Denis Chassé
  Denis Chassé
  Jan 23, 2024 @ 09:53am
loSMTP = CREATEOBJECT("wwJsonSerializer")
ADDPROPERTY(loSMTP,"api_key", "api-74033xxxxxxxxxxxxxxxxxxxxxxxxxxxx")
ADDPROPERTY(loSMTP,"to", "Moi <info@gestion-beaute.com>")
ADDPROPERTY(loSMTP,"sender", "Encore moi <testmlm@gestion-beaute.com>")
ADDPROPERTY(loSMTP,"subject", "Hello a moi")
ADDPROPERTY(loSMTP,"text_body", "Je peux juste esperer.")

loProxy = CREATEOBJECT("wwJsonServiceClient")
loSMTP_Put = loProxy.CallService("https://api.smtp2go.com/v3/email/send",loSMTP,"PUT")

This is not working. What do I need to fix?

With the debugger when I look at what is in loSmtp_Put I have : 405 method not allowed

Gravatar is a globally recognized avatar based on your email address. re: SMTP2Go and REST API
  Rick Strahl
  Denis Chassé
  Jan 23, 2024 @ 10:21am

LOL:

This is not working.

Seriously?

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: SMTP2Go and REST API
  Denis Chassé
  Rick Strahl
  Jan 23, 2024 @ 11:12am

I modified the code to

loData = CREATEOBJECT("Empty")
ADDPROPERTY(loData,"api_key", "api-74033xxxxxxxxxxxxxxxxxxxxxxxxxxx")
ADDPROPERTY(loData,"to", "Moi <info@gestion-beaute.com>")
ADDPROPERTY(loData,"sender", "Encore moi <testmlm@gestion-beaute.com>")
ADDPROPERTY(loData,"subject", "Hello a moi")
ADDPROPERTY(loData,"text_body", "Je peux juste esperer.")

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

loProxy = CREATEOBJECT("wwJsonServiceClient")
lcJson = loProxy.CallService("https://api.smtp2go.com/v3/email/send",lcJson,"PUT")

I guess that this is code is better. But the result of the execution is not better. 405 method not allowed

Can you help?

Gravatar is a globally recognized avatar based on your email address. re: SMTP2Go and REST API
  Rick Strahl
  Denis Chassé
  Jan 23, 2024 @ 11:42am

Look Denis, this isn't about you needing help and me laughing at you for not knowing. This is about you not providing any useful information... You don't bother reporting an error message, or a link to how the API is supposed to work. You just say It doesn't work and expect somebody to give you answer based on no useful context... Nobody can help you with just that information.

IOW, you want anybody who might actually help to figure this all out on their own before they can even think to help you, which is not cool! It's lazy. We're not here to do the most basic work for you, and you've been a developer for long enough and have been on the UT and therefore you should know better to at least do your basic homework before you ask for help!

Now - because I was curious I went to site and searched for the docs, and reviewed the API even though you made no effort whatsoever to provide that info here.

IAC, the answer is that the to field is not a single value but an array in the JSON that's expected. You need to create an array of string values with a single value. Using the object syntax you used you can create a collection and add the single value to it then assign that to the object - or you can create a string ['rick <rickstrahl@nowhere.com>'] for the email (although I don't recommend that).

Also when you use CallService() you can pass the object itself - no need to serialize it first - the method will do the serialization for you.

+++ Rick ---

Gravatar is a globally recognized avatar based on your email address. re: SMTP2Go and REST API
  Denis Chassé
  Rick Strahl
  Jan 23, 2024 @ 12:10pm

Hello Rick,

Thank you.

© 1996-2024