Skip to content

Support Tags in MailgunTemplateMessage #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions Sources/Mailgun/Models/TemplateMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ import Vapor

public struct MailgunTemplateMessage: Content {
public static var defaultContentType: HTTPMediaType = .formData

public typealias FullEmail = (email: String, name: String?)

public let from: String
public let to: String
public let replyTo: String?
public let cc: String?
public let bcc: String?
public let subject: String
public let template: String
public let templateData: [String:String]?
public let templateData: [String: String]?
public let templateVersion: String?
public let templateText: Bool?
public let attachment: [File]?
public let inline: [File]?

public let tags: [String]?

private enum CodingKeys: String, CodingKey {
case from
case to
Expand All @@ -31,8 +32,9 @@ public struct MailgunTemplateMessage: Content {
case templateData = "h:X-Mailgun-Variables"
case templateVersion = "t:version"
case templateText = "t:text"
case tags = "o:tag"
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(from, forKey: .from)
Expand All @@ -43,18 +45,19 @@ public struct MailgunTemplateMessage: Content {
try container.encode(template, forKey: .template)
if let templateData = templateData {
guard let jsonData = try? JSONEncoder().encode(templateData),
let jsonString = String(data: jsonData, encoding: .utf8)
else { throw MailgunError.encodingProblem }
let jsonString = String(data: jsonData, encoding: .utf8)
else { throw MailgunError.encodingProblem }
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)
try container.encode(attachment, forKey: .attachment)
try container.encode(inline, forKey: .inline)
try container.encode(tags, forKey: .tags)
}
public init(from: String, to: String, replyTo: String? = nil, cc: String? = nil, bcc: String? = nil, subject: String, template: String, templateData: [String:String]? = nil, templateVersion: String? = nil, templateText: Bool? = nil, attachments: [File]? = nil, inline: [File]? = nil) {

public init(from: String, to: String, replyTo: String? = nil, cc: String? = nil, bcc: String? = nil, subject: String, template: String, templateData: [String: String]? = nil, templateVersion: String? = nil, templateText: Bool? = nil, attachments: [File]? = nil, inline: [File]? = nil, tags: [String]? = nil) {
self.from = from
self.to = to
self.replyTo = replyTo
Expand All @@ -67,9 +70,10 @@ public struct MailgunTemplateMessage: Content {
self.templateText = templateText
self.attachment = attachments
self.inline = inline
self.tags = tags
}
public init(from: String, to: [String], replyTo: String? = nil, cc: [String]? = nil, bcc: [String]? = nil, subject: String, template: String, templateData: [String:String]? = nil, templateVersion: String? = nil, templateText: Bool? = nil, attachments: [File]? = nil, inline: [File]? = nil) {

public init(from: String, to: [String], replyTo: String? = nil, cc: [String]? = nil, bcc: [String]? = nil, subject: String, template: String, templateData: [String: String]? = nil, templateVersion: String? = nil, templateText: Bool? = nil, attachments: [File]? = nil, inline: [File]? = nil, tags: [String]? = nil) {
self.from = from
self.to = to.joined(separator: ",")
self.replyTo = replyTo
Expand All @@ -82,9 +86,10 @@ public struct MailgunTemplateMessage: Content {
self.templateText = templateText
self.attachment = attachments
self.inline = inline
self.tags = tags
}
public init(from: String, to: [FullEmail], replyTo: String? = nil, cc: [FullEmail]? = nil, bcc: [FullEmail]? = nil, subject: String, template: String, templateData: [String:String]? = nil, templateVersion: String? = nil, templateText: Bool? = nil, attachments: [File]? = nil, inline: [File]? = nil) {

public init(from: String, to: [FullEmail], replyTo: String? = nil, cc: [FullEmail]? = nil, bcc: [FullEmail]? = nil, subject: String, template: String, templateData: [String: String]? = nil, templateVersion: String? = nil, templateText: Bool? = nil, attachments: [File]? = nil, inline: [File]? = nil, tags: [String]? = nil) {
self.from = from
self.to = to.stringArray.joined(separator: ",")
self.replyTo = replyTo
Expand All @@ -97,5 +102,6 @@ public struct MailgunTemplateMessage: Content {
self.templateText = templateText
self.attachment = attachments
self.inline = inline
self.tags = tags
}
}