python - 如何在 python 中通过 sendemail 将字符串传递给消息
问题描述
我是 python 新手。这是我的
msg = MIMEMultipart()
msg['From'] = "email1"
msg['To'] = "email2"
msg['Subject'] = "Item Found!"
password = "pw"
body = "<a href = ""www.ebay.ca"">Item found!</a>"
msg.attach(MIMEText(body, 'html'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(msg['From'], password)
print("Login success")
server.sendmail(msg['From'], msg['To'], msg.as_string())
我有一个清单是
url1 = "www.ebay.ca"
url2 = "www.amazon.ca"
list = [url1, url2]
i = 0
我要换货
body = "<a href = ""www.ebay.ca"">Item found!</a>"
和
body = "<a href = list[i]>Item found!</a>"
这样做的正确方法是什么,因为我想做一个循环并将 i 作为动态变量并将消息中的 url 作为 url 输出,谢谢。
解决方案
您可以使用 . 添加字符串+
。
tmp = 'str2'
str = 'str1' + tmp + 'str3'
str
>>> 'str1str2str3'
因此只写
body = "<a href = "+ list[i] + ">Item found!</a>"
或者,从 python 3.6 开始,您可以使用 f-strings。
body = f"<a href = {list[i]}>Item found!</a>"
完整代码如下所示:
urls = [url1, url2]
for url in urls:
body = body = f"<a href = {url}>Item found!</a>"
# do sth with body
推荐阅读
- elasticsearch - 如何配置filebeat和logstash?3 台虚拟机
- python - 如何编写循环直到(Python Anaconda)
- python - 将 lambda 字符串转换为 lambda 函数签名
- python - Pyspark - 将时间戳传递给 udf
- javascript - setInterval 内的关闭计数器
- mongodb - mongodb查询具有未知属性的第三级
- ios - IOS:从另一个没有 URL 架构的应用程序打开一个应用程序
- ruby-on-rails - Shopify 应用的 Heroku 服务器超时
- unity3d - 输入字段以错误的格式粘贴阿拉伯文本
- python - 哪个是按列值有效过滤 numpy 矩阵的正确方法?