将变量从脚本传递到我的python电子邮件脚本,以便在邮件中以引号输出

2024-05-19 17:04:02 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将脚本中的变量(缺少文件)$file传递到正文消息中的python电子邮件脚本中。 script.sh

#!/bin/bash
file = abc.txt
echo "the following" ${file} "are missing"
pythonEmail.py

pythonemal.py

#!/usr/bin/python
import smtplib
sender = 'me@test.com'
receivers = ['you@test.com']
message = """From: myself<me@test.com>
To: you@test.com'
Subject: missing files
$file
"""
try:
   smtpObj = smtplib.SMTP("localhost")
   smtpObj.sendmail(sender, receivers, message)
   print ("Successfully sent email")
except SMTPException:
   print ("Error: unable to send email")

Tags: pytest脚本comyoumessagebinsender
1条回答
网友
1楼 · 发布于 2024-05-19 17:04:02

这并不比@Haxiel评论的更多:

script.sh:

#!/bin/bash
file=abc.txt
echo "the following" ${file} "are missing"
pythonEmail.py $file

pythonEmail.py:

#!/usr/bin/python
import smtplib
import sys

sender = 'me@test.com'
receivers = ['you@test.com']
message = """\
From: myself<me@test.com>
To: you@test.com
Subject: missing files

%s""" % (sys.argv[1])

try:
   smtpObj = smtplib.SMTP("localhost")
   smtpObj.sendmail(sender, receivers, message)
   print ("Successfully sent email")
except SMTPException:
   print ("Error: unable to send email")

相关问题 更多 >