如何在sendmail函数的消息正文中插入var内容

时间:2019-06-11 09:24:49

标签: python sendmail

我有下一个功能:

hostname = os.uname()[1]

def sendmail(sender, receiver, content, user=None, password=None, hostname='localhost', port=25,ssl=False):
smt_server = 'localhost'
port = '25'
sender = 'jenkins@jenkins.com'
receiver = 'test@test.es'

content = "I need to show hostname here" , hostname , "Done."

msg = MIMEText(content)
msg['From'] = sender
msg['To'] = receiver
msg['Subject'] = 'Simple app script'

try:
   s = smtplib.SMTP('localhost', port )
   s.sendmail('jenkins@jenkins.com', 'test@test.es', content)
   s.quit()
   print "Succesfully sent email"
except SMTPException:
   print "Error: fail to send email"

实际结果:

AttributeError: 'tuple' object has no attribute 'encode'

预期结果:

邮件的正文消息必须为:

I need to show hostname here MyHostname Done.

我不确定我是否使用严格的方式,你能帮我吗?

谢谢

2 个答案:

答案 0 :(得分:1)

您可以添加字符串:

content = "I need to show hostname here" + hostname + "Done."

答案 1 :(得分:1)

尝试此操作使内容成为一个字符串。

content = "I need to show hostname here " + hostname + " Done."