swift - 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)
}
}
感谢您的帮助
解决方案
模板文字 这就是你不应该首先拥有这样一个长字符串的答案。
要创建模板文字,请使用反引号: `` 使用新引号,您只需在 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
推荐阅读
- matlab - MATLAB中的视频播放器,按住滑块控件
- python - 为什么我不能在 django 中显示更多字段?
- python - 这个python语句是什么意思,%是什么意思?
- ios - UIPopoverControllerView 根视图填充
- rest - 微服务中的 REST API 方法路径命名
- html - 将 HTML 渲染到 React-PDF
- android - 如何在循环中连续从 Firebase 获取数据,如果数据不正确,应退出循环
- amazon-web-services - AWS CloudWatch 触发器在开始时间重复到结束时间
- node.js - Express 中间件问题(与 MongoDB 交互) - POST 方法的 req.body 未擦除空白并返回不可用对象错误
- python-3.x - matplotlib 轴的“rect”参数中的两个第一个坐标是什么?