首页 > 解决方案 > SwiftMailgun如何将变量添加到bodyHTML

问题描述

我正在使用 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)
                }

            }

感谢您的帮助

标签: swift

解决方案


模板文字 这就是你不应该首先拥有这样一个长字符串的答案。

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

例如

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


推荐阅读