python-2.7 - 如何使用树莓派和 smtplib 通过电子邮件发送变量值
问题描述
我正在做这个项目,我有 3 个字符串变量,我想将它们的值通过电子邮件发送到电子邮件地址。我能够通过电子邮件发送纯文本消息;但是,我无法在其中包含这些变量值。以下是我的代码:
import serial
import smtplib
import time
serialport = serial.Serial('/dev/ttyUSB0', 115200, timeout = 0.5)
user= 'user@gmail.com'
password= 'password'
receiver= 'receiver@gmail.com
subject= 'Solar tracker status'
header = 'To: ' + email + '\n' + 'From: ' + email + '\n' + 'Subject: ' +
subject
while True:
line = serialport.readline()
result = line.find(";")
if result > 0:
str = line.split(";")
volt=str[0]
power=str[1]
temp=str[2]
body = "\n" + + "Voltage: " + volt + "\n" + "Power: " + power + "\n" + "Temp: " + temp
print header + '\n' + body
s=smtplib.SMTP('smtp.gmail.com',587)
s.ehlo()
s.starttls()
s.ehlo()
s.login(email, password)
s.sendmail(email, email, header + '\n\n' + body)
s.quit
运行此脚本时,我收到此错误消息:
File "testserial.py", line 23, in <module>
body = "\n" + + "Voltage: " + volt
TypeError: bad operand type for unary +: 'str'
我尝试使用 str(volt) 将变量转换为字符串,然后收到此错误消息:
File "testserial.py", line 23, in <module>
str(volt)
TypeError: 'list' object is not callable
我无法理解这一点,因为它们最初是字符串格式,因为我能够使用 %s 将它们写入文本文件而无需转换它。
我想我只是不知道如何将变量传递到电子邮件的正文中。请帮忙!
解决方案
推荐阅读
- ubuntu-16.04 - 如何恢复加载的 Kate 插件
- android - 错误的 Android Studio 所有属性面板
- apache-spark - Spark 超时时的 Hive
- python - 英语缩略正则表达式的解决方案不起作用
- corda - 无法将 cordapp-example 导入 IntelliJ 181.4892.42
- c - C 使用 execvp 执行带有另一个命令输出的命令
- excel - 复制所有行,粘贴到另一个工作表交替行与目标工作表上的现有数据)
- google-chrome-extension - 如何仅通过单击图标或按钮来关闭 chrome 扩展
- matlab - 查找向量 b 的唯一索引的最快方法,其中数组 A(i,j) == b
- python - Pyomo:错误:运行模型时出现意外异常:列表索引超出范围