Skip to main content
Best answer

Dynamic Variable in Python


Hello!

I’m having trouble trying to include an input data variable to my JSON string using Python - Requests.

  • Trigger: Catch’s hook from a web form
  • Action: Consumes API to send a Confirmation email

Input Data

These are the fields the Zap retrieves from the webhook

 

Code (Action that sends de confirmation email):

import requests

url = "https://api.trx.icommarketing.com/email"

payload="{  \r\n    \"From\": \"Icomm Test demo@demo.com\",  \r\n    \"To\": \"lucas.vargas@icommkt.com\",  \r\n    \"Cc\": \"\",  \r\n    \"Bcc\": \"\",  \r\n    \"Subject\": \"Hello :)\",  \r\n    \"Tag\": \"string\",  \r\n    \"HtmlBody\": \"Cuerpo del correo\",  \r\n    \"TextBody\": \"Texto plano\",  \r\n    \"ReplyTo\": \"daniel.alarcon@icommkt.com\",  \r\n    \"Headers\": [],  \r\n    \"TrackOpens\": true,  \r\n    \"TrackLinks\": \"HtmlAndText\",  \r\n    \"Attachments\": [    \r\n    {      \r\n        \"Name\": \"readme.txt\",      \r\n        \"Content\": \"dGVzdCBjb250ZW50\",      \r\n        \"ContentType\": \"text/plain\"    },    \r\n    {      \r\n        \"Name\": \"report.pdf\",      \r\n        \"Content\": \"dGVzdCBjb250ZW50\",      \r\n        \"ContentType\": \"application/octet-stream\"    \r\n    }  \r\n    ]\r\n    }"
headers = {
  'accept': 'application/json',
  'Content-Type': 'application/json',
  'X-Trx-Api-Key': 'PRIVATE KEY'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

When I test the API it works because it sends an email "To\": \"lucas.vargas@icommkt.com\"

However, I want the email to be sent to the person who filled the form using “email” as a dynamic variable.

 

Hope someone can help me with this case

 

Regards

 

Best answer by GetUWiredBest answer by GetUWired

Hi @LucasIcomm 

There may be more elegant ways to do this but the simplest way would just be to include the email variable in the payload you’ve defined

 

payload="{  \r\n    \"From\": \"Icomm Test demo@demo.com\",  \r\n    \"To\": \""+input_data['email']+"\",  \r\n    \"Cc\": \"\",  \r\n    \"Bcc\": \"\",  \r\n    \"Subject\": \"Hello :)\",  \r\n    \"Tag\": \"string\",  \r\n    \"HtmlBody\": \"Cuerpo del correo\",  \r\n    \"TextBody\": \"Texto plano\",  \r\n    \"ReplyTo\": \"daniel.alarcon@icommkt.com\",  \r\n    \"Headers\": [],  \r\n    \"TrackOpens\": true,  \r\n    \"TrackLinks\": \"HtmlAndText\",  \r\n    \"Attachments\": [    \r\n    {      \r\n        \"Name\": \"readme.txt\",      \r\n        \"Content\": \"dGVzdCBjb250ZW50\",      \r\n        \"ContentType\": \"text/plain\"    },    \r\n    {      \r\n        \"Name\": \"report.pdf\",      \r\n        \"Content\": \"dGVzdCBjb250ZW50\",      \r\n        \"ContentType\": \"application/octet-stream\"    \r\n    }  \r\n    ]\r\n    }"

 

View original
Did this topic help you find an answer to your question?
This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

2 replies

GetUWired
Forum|alt.badge.img+12
  • Zapier Expert
  • 1030 replies
  • Answer
  • May 20, 2021

Hi @LucasIcomm 

There may be more elegant ways to do this but the simplest way would just be to include the email variable in the payload you’ve defined

 

payload="{  \r\n    \"From\": \"Icomm Test demo@demo.com\",  \r\n    \"To\": \""+input_data['email']+"\",  \r\n    \"Cc\": \"\",  \r\n    \"Bcc\": \"\",  \r\n    \"Subject\": \"Hello :)\",  \r\n    \"Tag\": \"string\",  \r\n    \"HtmlBody\": \"Cuerpo del correo\",  \r\n    \"TextBody\": \"Texto plano\",  \r\n    \"ReplyTo\": \"daniel.alarcon@icommkt.com\",  \r\n    \"Headers\": [],  \r\n    \"TrackOpens\": true,  \r\n    \"TrackLinks\": \"HtmlAndText\",  \r\n    \"Attachments\": [    \r\n    {      \r\n        \"Name\": \"readme.txt\",      \r\n        \"Content\": \"dGVzdCBjb250ZW50\",      \r\n        \"ContentType\": \"text/plain\"    },    \r\n    {      \r\n        \"Name\": \"report.pdf\",      \r\n        \"Content\": \"dGVzdCBjb250ZW50\",      \r\n        \"ContentType\": \"application/octet-stream\"    \r\n    }  \r\n    ]\r\n    }"

 


  • Author
  • Beginner
  • 1 reply
  • May 20, 2021

Thank you GetUwired. This code worked perfectly.

 

I appreciate your help

 

Have a very nice day