我试图在 Python 电子邮件脚本的主题行中包含一个变量,以将消息发送到我的手机。当我运行程序时,消息显示“温度是:CTemp”,但我想要“温度是:12.34”(或者当时设置的变量)。如何将变量 CTemp 插入主题行?
import smtplib
from email.mime.text import MIMEText
username = "****@****.com"
password = "****"
vtext = "**********@****.com"
CTemp = 12.34 #set for testing
msg = MIMEText
msg = MIMEText("""The Temp is: CTemp""")
server = smtplib.SMTP('****.****.net',25)
server.login(username,password)
server.sendmail(username, vtext, msg.as_string())
server.quit()
最佳答案
您应该将“文本”字符串连接到变量
CTemp = str(12.34) #set for testing
text = "The temp is: "+CTemp
msg = MIMEText(text)
http://www.pythonforbeginners.com/concatenation/string-concatenation-and-formatting-in-python
关于python - 使用 Python 发送电子邮件并在主题行中包含变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33219363/