SwiftMailgun如何向bodyHTML添加变量

时间:2018-09-01 15:27:21

标签: swift

我正在使用SwiftMailgun库在正在使用的应用程序中以及在bodyHTML中向其添加变量的方式发送电子邮件。我想用从数据库中获取的resetToken替换00432,但这样做遇到了挑战

            //getting the resetToken from response
            let resetToken = jsonData.value(forKey: "token") as! String?
            print(resetToken!)

            let mailgun = MailgunAPI(apiKey: "c0c5741e8ef55e9b2307e137ae5b3e92-c1fe131e-add76801", clientDomain: "sandbox732ceb9922894a27beeabb106146127b.mailgun.org")

            mailgun.sendEmail(to: self.emailAddress.text!, from: "HellooFood Account Password Reset <HellooFood@HellooFood.com>", subject: "HellooFood Account Password Reset", bodyHTML: "Hi there <br> We heard that you lost your password, but dont worry! You can use the code below to reset your password within the next day.<br> <b>00432</b><br>Enter the code in the reset password field to reset your password. <br><br>If this mail is a mistake. just ignore it and nothing will happen.<br>If you are not the one who requested the Password Reset Contact us as soon as possible. <br><br>Thanks  HellooFood") { mailgunResult in

                if mailgunResult.success{

                    //switching the screen
                    let goToResetVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Reset")
                    self.navigationController?.pushViewController(goToResetVC, animated: false)
                    SVProgressHUD.dismiss(withDelay: 1.6)

                }else{

                    SVProgressHUD.dismiss()
                    let alert = UIAlertController(title: "No internet connection", message: "Check your data or wifi and try again", preferredStyle: .alert)

                    alert.addAction(UIAlertAction(title: "Ok", style: .destructive, handler: nil))
                    //alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))

                    self.present(alert, animated: true)
                }

            }

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

模板文字 这就是答案,您一开始不应该像这样长字符串。

要创建模板文字,请使用反引号引起来: 使用新引号,您只需在js中按Enter键即可。 在添加变量时,请使用以下语法$ {}

E.G。

let token = "123432";
let bodyHTML = `This is text for the mail sent with the ${token} id`

这将打印:
This is text for the mail sent with the 123432 id