Swift3 / Zoho CRM REST APIs / Send Mail from Inventory Module
Back to Collection Items
func chilkatTest() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = CkoHttp()!
var success: Bool
// Use this online tool to generate code from sample JSON: Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "data": [
// {
// "from": {
// "user_name": "Patricia Boyle",
// "email": "p.boyle@abc.com"
// },
// "to": [
// {
// "user_name": "user1",
// "email": "user1@gmail.com"
// }
// ],
// "inventory_details": {
// "inventory_template": {
// "id": "{{inventory_template_id}}",
// "name": "CT Scan Machines.pdf"
// }
// },
// "paper_type": "A4",
// "view_type": "portrait"
// }
// ]
// }
let json = CkoJsonObject()!
json.update("data[0].from.user_name", value: "Patricia Boyle")
json.update("data[0].from.email", value: "p.boyle@abc.com")
json.update("data[0].to[0].user_name", value: "user1")
json.update("data[0].to[0].email", value: "user1@gmail.com")
json.update("data[0].inventory_details.inventory_template.id", value: "{{inventory_template_id}}")
json.update("data[0].inventory_details.inventory_template.name", value: "CT Scan Machines.pdf")
json.update("data[0].paper_type", value: "A4")
json.update("data[0].view_type", value: "portrait")
// Adds the "Authorization: Bearer <access_token>" header.
http.authToken = "<access_token>"
var resp: CkoHttpResponse? = http.postJson3("https://domain.com/crm/v2.1/Purchase_Orders/{{record_id}}/actions/send_mail", contentType: "application/json", json: json)
if http.lastMethodSuccess == false {
print("\(http.lastErrorText!)")
return
}
let sbResponseBody = CkoStringBuilder()!
resp!.getBodySb(sbResponseBody)
let jResp = CkoJsonObject()!
jResp.loadSb(sbResponseBody)
jResp.emitCompact = false
print("Response Body:")
print("\(jResp.emit()!)")
var respStatusCode: Int = resp!.statusCode.intValue
print("Response Status Code = \(respStatusCode)")
if respStatusCode >= 400 {
print("Response Header:")
print("\(resp!.header!)")
print("Failed.")
resp = nil
return
}
resp = nil
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "data": [
// {
// "code": "SUCCESS",
// "details": {
// "message_id": "9943645bd4411e51145e0515bd77e380efceba4d78a87bdb6aeb65e530109d6c"
// },
// "message": "Your mail has been sent successfully.",
// "status": "success"
// }
// ]
// }
// Sample code for parsing the JSON response...
// Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code
var code: String?
var Message_id: String?
var message: String?
var status: String?
var i: Int = 0
var count_i: Int = jResp.size(ofArray: "data").intValue
while i < count_i {
jResp.i = i
code = jResp.string(of: "data[i].code")
Message_id = jResp.string(of: "data[i].details.message_id")
message = jResp.string(of: "data[i].message")
status = jResp.string(of: "data[i].status")
i = i + 1
}
}
Curl Command
curl -X POST
-H "Authorization: Bearer <access_token>"
-d '{
"data": [
{
"from": {
"user_name": "Patricia Boyle",
"email": "p.boyle@abc.com"
},
"to": [
{
"user_name": "user1",
"email": "user1@gmail.com"
}
],
"inventory_details": {
"inventory_template": {
"id": "{{inventory_template_id}}",
"name": "CT Scan Machines.pdf"
}
},
"paper_type": "A4",
"view_type": "portrait"
}
]
}'
https://domain.com/crm/v2.1/Purchase_Orders/{{record_id}}/actions/send_mail"
Postman Collection Item JSON
{
"name": "Send Mail from Inventory Module",
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{access-token}}",
"type": "string"
}
]
},
"method": "POST",
"header": [
],
"body": {
"mode": "raw",
"raw": "{\n \"data\": [\n {\n \"from\": {\n \"user_name\": \"Patricia Boyle\",\n \"email\": \"p.boyle@abc.com\"\n },\n \"to\": [\n {\n \"user_name\": \"user1\",\n \"email\": \"user1@gmail.com\"\n }\n ],\n \"inventory_details\": {\n \"inventory_template\": {\n \"id\": \"{{inventory_template_id}}\",\n \"name\": \"CT Scan Machines.pdf\"\n }\n },\n \"paper_type\": \"A4\",\n \"view_type\": \"portrait\"\n }\n ]\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{api-domain}}/crm/v2.1/Purchase_Orders/{{record_id}}/actions/send_mail\"",
"host": [
"{{api-domain}}"
],
"path": [
"crm",
"v2.1",
"Purchase_Orders",
"{{record_id}}",
"actions",
"send_mail\""
]
},
"description": "To send emails through an API."
},
"response": [
{
"name": "SUCCESS RESPONSE",
"originalRequest": {
"method": "POST",
"header": [
],
"body": {
"mode": "raw",
"raw": "{\n \"data\": [\n {\n \"from\": {\n \"user_name\": \"Patricia Boyle\",\n \"email\": \"patricia@gmail.com\"\n },\n \"to\": [\n {\n \"user_name\": \"User 2\",\n \"email\": \"user2@gmail.com\"\n }\n ],\n \"inventory_details\": {\n \"inventory_template\": {\n \"id\": \"4150868000000227001\",\n \"name\": \"CT Scan Machines.pdf\"\n }\n },\n \"paper_type\": \"A4\",\n \"view_type\": \"portrait\"\n }\n ]\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{api-domain}}/crm/v2.1/Quotes/4150868000004795007/actions/send_mail",
"host": [
"{{api-domain}}"
],
"path": [
"crm",
"v2.1",
"Quotes",
"4150868000004795007",
"actions",
"send_mail"
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Server",
"value": "ZGS"
},
{
"key": "Date",
"value": "Mon, 10 May 2021 12:46:59 GMT"
},
{
"key": "Content-Type",
"value": "application/json;charset=utf-8"
},
{
"key": "Content-Length",
"value": "190"
},
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Referrer-Policy",
"value": "strict-origin"
},
{
"key": "X-Content-Type-Options",
"value": "nosniff"
},
{
"key": "X-XSS-Protection",
"value": "1; mode=block"
},
{
"key": "Pragma",
"value": "no-cache"
},
{
"key": "Cache-Control",
"value": "no-cache"
},
{
"key": "Expires",
"value": "Thu, 01 Jan 1970 00:00:00 GMT"
},
{
"key": "X-Frame-Options",
"value": "SAMEORIGIN"
},
{
"key": "X-ACCESSTOKEN-RESET",
"value": "2021-05-10T19:05:30+05:30"
},
{
"key": "clientVersion",
"value": "4019232"
},
{
"key": "clientsubVersion",
"value": "19804a82a902c9fa618d2b359b97e26a"
},
{
"key": "Content-Disposition",
"value": "attachment; filename=response.json"
},
{
"key": "Content-Language",
"value": "en-US"
},
{
"key": "Strict-Transport-Security",
"value": "max-age=63072000"
}
],
"cookie": [
],
"body": "{\n \"data\": [\n {\n \"code\": \"SUCCESS\",\n \"details\": {\n \"message_id\": \"9943645bd4411e51145e0515bd77e380efceba4d78a87bdb6aeb65e530109d6c\"\n },\n \"message\": \"Your mail has been sent successfully.\",\n \"status\": \"success\"\n }\n ]\n}"
},
{
"name": "MANDATORY_NOT_FOUND",
"originalRequest": {
"method": "POST",
"header": [
],
"body": {
"mode": "raw",
"raw": "{\n \"data\": [\n {\n \"from\": {\n \"user_name\": \"Patricia Boyle\",\n \"email\": \"p.boyle@abc.com\"\n },\n \"to\": [\n {\n \"user_name\": \"user1\",\n \"email\": \"user1@gmail.com\"\n }\n ],\n \"inventory_details\": {\n \"inventory_template\": {\n \"name\": \"CT Scan Machines.pdf\"\n }\n },\n \"paper_type\": \"A4\",\n \"view_type\": \"portrait\"\n }\n ]\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{api-domain}}/crm/v2.1/Quotes/4150868000004795007/actions/send_mail",
"host": [
"{{api-domain}}"
],
"path": [
"crm",
"v2.1",
"Quotes",
"4150868000004795007",
"actions",
"send_mail"
]
}
},
"status": "Bad Request",
"code": 400,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Server",
"value": "ZGS"
},
{
"key": "Date",
"value": "Mon, 10 May 2021 12:48:39 GMT"
},
{
"key": "Content-Type",
"value": "application/json;charset=utf-8"
},
{
"key": "Content-Length",
"value": "187"
},
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Referrer-Policy",
"value": "strict-origin"
},
{
"key": "X-Content-Type-Options",
"value": "nosniff"
},
{
"key": "X-XSS-Protection",
"value": "1; mode=block"
},
{
"key": "Pragma",
"value": "no-cache"
},
{
"key": "Cache-Control",
"value": "no-cache"
},
{
"key": "Expires",
"value": "Thu, 01 Jan 1970 00:00:00 GMT"
},
{
"key": "X-Frame-Options",
"value": "SAMEORIGIN"
},
{
"key": "X-ACCESSTOKEN-RESET",
"value": "2021-05-10T19:05:30+05:30"
},
{
"key": "clientVersion",
"value": "4019232"
},
{
"key": "clientsubVersion",
"value": "19804a82a902c9fa618d2b359b97e26a"
},
{
"key": "Content-Disposition",
"value": "attachment; filename=response.json"
},
{
"key": "Content-Language",
"value": "en-US"
}
],
"cookie": [
],
"body": "{\n \"data\": [\n {\n \"code\": \"MANDATORY_NOT_FOUND\",\n \"details\": {\n \"api_name\": \"id\",\n \"json_path\": \"$.data[0].inventory_details.inventory_template.id\"\n },\n \"message\": \"required field not found\",\n \"status\": \"error\"\n }\n ]\n}"
},
{
"name": "INVALID_DATA",
"originalRequest": {
"method": "POST",
"header": [
],
"body": {
"mode": "raw",
"raw": "{\n \"data\": [\n {\n \"from\": {\n \"user_name\": \"Patricia Boyle\",\n \"email\": \"p.boyle@abc.com\"\n },\n \"to\": [\n {\n \"user_name\": \"user1\",\n \"email\": \"user1@gmail.com\"\n }\n ],\n \"inventory_details\": {\n \"inventory_template\": {\n \"id\": \"415086800000022700\",\n \"name\": \"CT Scan Machines.pdf\"\n }\n },\n \"paper_type\": \"A4\",\n \"view_type\": \"portrait\"\n }\n ]\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{api-domain}}/crm/v2.1/Quotes/4150868000004795007/actions/send_mail",
"host": [
"{{api-domain}}"
],
"path": [
"crm",
"v2.1",
"Quotes",
"4150868000004795007",
"actions",
"send_mail"
]
}
},
"status": "Bad Request",
"code": 400,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Server",
"value": "ZGS"
},
{
"key": "Date",
"value": "Mon, 10 May 2021 12:49:10 GMT"
},
{
"key": "Content-Type",
"value": "application/json;charset=utf-8"
},
{
"key": "Content-Length",
"value": "216"
},
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Referrer-Policy",
"value": "strict-origin"
},
{
"key": "X-Content-Type-Options",
"value": "nosniff"
},
{
"key": "X-XSS-Protection",
"value": "1; mode=block"
},
{
"key": "Pragma",
"value": "no-cache"
},
{
"key": "Cache-Control",
"value": "no-cache"
},
{
"key": "Expires",
"value": "Thu, 01 Jan 1970 00:00:00 GMT"
},
{
"key": "X-Frame-Options",
"value": "SAMEORIGIN"
},
{
"key": "X-ACCESSTOKEN-RESET",
"value": "2021-05-10T19:05:30+05:30"
},
{
"key": "clientVersion",
"value": "4019232"
},
{
"key": "clientsubVersion",
"value": "19804a82a902c9fa618d2b359b97e26a"
},
{
"key": "Content-Disposition",
"value": "attachment; filename=response.json"
},
{
"key": "Content-Language",
"value": "en-US"
}
],
"cookie": [
],
"body": "{\n \"data\": [\n {\n \"code\": \"INVALID_DATA\",\n \"details\": {\n \"api_name\": \"email\",\n \"json_path\": \"$.data[0].from.email\"\n },\n \"message\": \"Email can't be sent using this from address.Kindly check the allowed from address list\",\n \"status\": \"error\"\n }\n ]\n}"
},
{
"name": "INVALID_DATA",
"originalRequest": {
"method": "POST",
"header": [
],
"body": {
"mode": "raw",
"raw": "{\n \"data\": [\n {\n \"from\": {\n \"user_name\": \"Patricia Boyle\",\n \"email\": \"p.boyle@abc.com\"\n },\n \"to\": [\n {\n \"user_name\": \"user1\",\n \"email\": \"user1@gmail.com\"\n }\n ],\n \"inventory_details\": {\n \"inventory_template\": {\n \"id\": \"4150868000000227001\",\n \"name\": \"CT Scan Machines.pdf\"\n }\n },\n \"paper_type\": \"A4\",\n \"view_type\": \"portrait\"\n }\n ]\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{api-domain}}/crm/v2.1/Quotes/4150868000004795007/actions/send_mail",
"host": [
"{{api-domain}}"
],
"path": [
"crm",
"v2.1",
"Quotes",
"4150868000004795007",
"actions",
"send_mail"
]
}
},
"status": "Bad Request",
"code": 400,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Server",
"value": "ZGS"
},
{
"key": "Date",
"value": "Mon, 10 May 2021 12:49:36 GMT"
},
{
"key": "Content-Type",
"value": "application/json;charset=utf-8"
},
{
"key": "Content-Length",
"value": "216"
},
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Referrer-Policy",
"value": "strict-origin"
},
{
"key": "X-Content-Type-Options",
"value": "nosniff"
},
{
"key": "X-XSS-Protection",
"value": "1; mode=block"
},
{
"key": "Pragma",
"value": "no-cache"
},
{
"key": "Cache-Control",
"value": "no-cache"
},
{
"key": "Expires",
"value": "Thu, 01 Jan 1970 00:00:00 GMT"
},
{
"key": "X-Frame-Options",
"value": "SAMEORIGIN"
},
{
"key": "X-ACCESSTOKEN-RESET",
"value": "2021-05-10T19:05:30+05:30"
},
{
"key": "clientVersion",
"value": "4019232"
},
{
"key": "clientsubVersion",
"value": "19804a82a902c9fa618d2b359b97e26a"
},
{
"key": "Content-Disposition",
"value": "attachment; filename=response.json"
},
{
"key": "Content-Language",
"value": "en-US"
}
],
"cookie": [
],
"body": "{\n \"data\": [\n {\n \"code\": \"INVALID_DATA\",\n \"details\": {\n \"api_name\": \"email\",\n \"json_path\": \"$.data[0].from.email\"\n },\n \"message\": \"Email can't be sent using this from address.Kindly check the allowed from address list\",\n \"status\": \"error\"\n }\n ]\n}"
},
{
"name": "INVALID_REQUEST_METHOD",
"originalRequest": {
"method": "COPY",
"header": [
],
"body": {
"mode": "raw",
"raw": "{\n \"data\": [\n {\n \"from\": {\n \"user_name\": \"Patricia Boyle\",\n \"email\": \"p.boyle@abc.com\"\n },\n \"to\": [\n {\n \"user_name\": \"user1\",\n \"email\": \"user1@gmail.com\"\n }\n ],\n \"inventory_details\": {\n \"inventory_template\": {\n \"id\": \"4150868000000227001\",\n \"name\": \"CT Scan Machines.pdf\"\n }\n },\n \"paper_type\": \"A4\",\n \"view_type\": \"portrait\"\n }\n ]\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{api-domain}}/crm/v2.1/Quotes/4150868000004795007/actions/send_mail",
"host": [
"{{api-domain}}"
],
"path": [
"crm",
"v2.1",
"Quotes",
"4150868000004795007",
"actions",
"send_mail"
]
}
},
"status": "Bad Request",
"code": 400,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Server",
"value": "ZGS"
},
{
"key": "Date",
"value": "Mon, 10 May 2021 12:49:55 GMT"
},
{
"key": "Content-Type",
"value": "application/json;charset=utf-8"
},
{
"key": "Content-Length",
"value": "124"
},
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "X-Frame-Options",
"value": "deny"
},
{
"key": "X-Download-Options",
"value": "noopen"
},
{
"key": "X-Content-Type-Options",
"value": "nosniff"
}
],
"cookie": [
],
"body": "{\n \"code\": \"INVALID_REQUEST_METHOD\",\n \"details\": {},\n \"message\": \"The http request method type is not a valid one\",\n \"status\": \"error\"\n}"
},
{
"name": "OAUTH_SCOPE_MISMATCH",
"originalRequest": {
"method": "POST",
"header": [
],
"body": {
"mode": "raw",
"raw": "{\n \"data\": [\n {\n \"from\": {\n \"user_name\": \"Patricia Boyle\",\n \"email\": \"p.boyle@abc.com\"\n },\n \"to\": [\n {\n \"user_name\": \"user1\",\n \"email\": \"user1@gmail.com\"\n }\n ],\n \"inventory_details\": {\n \"inventory_template\": {\n \"id\": \"{{inventory_template_id}}\",\n \"name\": \"CT Scan Machines.pdf\"\n }\n },\n \"paper_type\": \"A4\",\n \"view_type\": \"portrait\"\n }\n ]\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{api-domain}}/crm/v2.1/Purchase_Orders/{{record_id}}/actions/send_mail\"",
"host": [
"{{api-domain}}"
],
"path": [
"crm",
"v2.1",
"Purchase_Orders",
"{{record_id}}",
"actions",
"send_mail\""
]
}
},
"status": "Unauthorized",
"code": 401,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Server",
"value": "ZGS"
},
{
"key": "Date",
"value": "Thu, 13 May 2021 08:21:30 GMT"
},
{
"key": "Content-Type",
"value": "application/json;charset=utf-8"
},
{
"key": "Content-Length",
"value": "113"
},
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Referrer-Policy",
"value": "strict-origin"
},
{
"key": "X-Content-Type-Options",
"value": "nosniff"
},
{
"key": "X-XSS-Protection",
"value": "1; mode=block"
},
{
"key": "Pragma",
"value": "no-cache"
},
{
"key": "Cache-Control",
"value": "no-cache"
},
{
"key": "Expires",
"value": "Thu, 01 Jan 1970 00:00:00 GMT"
},
{
"key": "X-Frame-Options",
"value": "SAMEORIGIN"
},
{
"key": "X-Download-Options",
"value": "noopen"
}
],
"cookie": [
],
"body": "{\n \"code\": \"OAUTH_SCOPE_MISMATCH\",\n \"details\": {},\n \"message\": \"invalid oauth scope to access this URL\",\n \"status\": \"error\"\n}"
}
]
}