From 383c085e32f284bf88b9bc72fb291440faced36c Mon Sep 17 00:00:00 2001 From: Sam Schmeink Date: Sat, 24 Aug 2019 04:38:24 +0200 Subject: [PATCH] fix for sending without template data --- Sources/Mailgun/Models/TemplateMessage.swift | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Sources/Mailgun/Models/TemplateMessage.swift b/Sources/Mailgun/Models/TemplateMessage.swift index 897822a..254c2fd 100644 --- a/Sources/Mailgun/Models/TemplateMessage.swift +++ b/Sources/Mailgun/Models/TemplateMessage.swift @@ -42,10 +42,12 @@ extension Mailgun { try container.encode(bcc, forKey: .bcc) try container.encode(subject, forKey: .subject) try container.encode(template, forKey: .template) - guard let jsonData = try? JSONEncoder().encode(templateData), let jsonString = String(data: jsonData, encoding: .utf8) else { - throw Error.encodingProblem + if let templateData = templateData { + guard let jsonData = try? JSONEncoder().encode(templateData), let jsonString = String(data: jsonData, encoding: .utf8) else { + throw Error.encodingProblem + } + try container.encode(jsonString, forKey: .templateData) } - try container.encode(jsonString, forKey: .templateData) try container.encode(templateVersion, forKey: .templateVersion) let text = templateText == true ? "yes" : nil // need to send yes as string try container.encode(text, forKey: .templateText)