我正在尝试使用 shell 命令在 python 中发送邮件。但是变量不会在脚本中被替换

I am trying to send mail in python using shell command . But variable does not get substituted in script

import subprocess
to ='xxxx@fmr.com'
subject= 'hostname'
cmd= "cat /var/log | mailx -s 'swap is full on'+subject" +to
subprocess.Popen(cmd,shell=True)

如果我在主题前加上双引号,那么 mailx 会将第一个字符作为主题并将其他字符视为收件人并尝试向他们发送邮件。

-------------------- cmd= "cat /var/log | mailx -s 'swap is full on'"+subject +至

你能试试这个吗:

import subprocess
to ='xxxx@fmr.com'
cmd= "cat /var/log | mailx -s 'test' " + to
subprocess.Popen(cmd,shell=True)