mailgun icon indicating copy to clipboard operation
mailgun copied to clipboard

Expecting type `String` for html parameter, getting `Future<View>`

Open nickfarrant opened this issue 6 years ago • 5 comments

Hi,

Great library, works really well.

The only issue I am having is using a Leaf template. The example code in the readme is generating an error in Xcode for me.

router.post("mail") { (req) -> Future<Response> in
    let content = try req.view().render("Emails/my-email", [
        "name": "Bob"
    ])

    let message = Mailgun.Message(
        from: "[email protected]",
        to: "[email protected]",
        subject: "Newsletter",
        text: "",
        html: content
    )

    let mailgun = try req.make(Mailgun.self)
    return try mailgun.send(message, on: req)
}

content is of type Future<View>, but Mailgun.Message expect a String for the html parameter.

Is there a way around this or am I missing something obvious?

I have tried html: "\(content)", but my Leaf template then generates: NIO.EventLoopFuture in the email.

Thanks in advance, Nick

nickfarrant avatar Feb 22 '19 13:02 nickfarrant

Hello,

Same issue for me. I couldn't find any method to convert the view into a String value. Any news on that?

Thanks, Sylvain

goamigo avatar Mar 25 '19 08:03 goamigo

@joscdk What were you doing to make leaf work?

twof avatar Mar 25 '19 18:03 twof

@nickfarrant @goamigo try the following

let content = try req.view().render("Emails/my-email", [
    "name": "Bob"
])
        
return content.flatMap(to: Response.self) { content in
    let contentString = String(data: content.data, encoding: .utf8)
    let message = Mailgun.Message(
        from: "[email protected]",
        to: "[email protected]",
        subject: "Newsletter",
        text: "",
        html: contentString ?? ""
    )
            
    let mailgun = try req.make(Mailgun.self)
    return try mailgun.send(message, on: req)
}

joscdk avatar Mar 25 '19 18:03 joscdk

Thanks @joscdk ! I've opened a PR that should make life a little easier for folks sending leaf templates. https://github.com/twof/VaporMailgunService/pull/28

Feel free to try that branch out, and if you run into any problems let me know! Code reviews are also always appreciated.

twof avatar Mar 25 '19 22:03 twof

Here's how to do it with Vapor 4 and Mailgun 5.0.0:

let content = req.view.render("Emails/my-email", [
    "name": "Bob"
])
        
return content.flatMapThrowing { content in
    let contentString = String(buffer: content.data)
    let message = MailgunMessage(
        from: "[email protected]",
        to: "[email protected]",
        subject: "Newsletter",
        text: "",
        html: contentString
    )
    return req.mailgun.send(message)
}

adamzarn avatar Oct 08 '20 20:10 adamzarn