尝试通过Groovy Shell脚本发送电子邮件 [英] Trying to send an email through a groovy shell script
问题描述
今天,我试图用普通的脚本替换bash脚本。一切运行顺利,我设法使用 execute()
命令调用其他命令。
today I was trying to replace a bash script with a groovy script. Everything runs smooth and I managed to use the execute()
command to invoke other command.
然后我在尝试发送带有主题的电子邮件:
Then I was trying to send an email with a subject:
mail -s "this is a test" my.mail@example.com < mail.tmp
变成了
'mail -s "this is a test" my.mail@example.com < mail.tmp'.execute()
不起作用,因为groovy将拆分一个参数这是一个测试
分为四个参数这
是
a
test
。
does not work since groovy will split up the one argument "this is a test"
into four arguments "this
is
a
test"
.
到目前为止很好。 Google帮我把它变成了
So far so good. Google helped me to turn this into
['mail', '-s', "this is a test", 'my.mail@example.com', '<', 'mail.tmp'].execute()
现在该主题被识别为一个参数,但是<
也被识别为参数,而不是文件重定向。
Now the subject is recognized as one parameter, but the <
is also recognized as parameter and not as the file redirection.
任何想法我该如何解决?
Any idea how I could solve this?
PS:不,我不希望使用Java代码发送邮件,因为我想代码会更复杂。但是,如果您有Java的单行代码,那么欢迎您...
PS: no, I would not like to use java code for sending mail since I guess the code will be more complex. But if you have a java one-liner, you are welcome...
推荐答案
您将不得不处理输出内容从过程到文件...
You'll have to handle writing the output from the process to a file ...
new File('mail.tmp').withWriter { it << """mail -s "this is a test" my.mail@example.com""".execute().getText() }
我仅在上面使用 ls -al作为命令进行了测试,并且它按预期工作,我不确定较长的运行过程是否需要您调整操作方式-如果是这样,则可能需要使用waitForProcessOutput:
I only tested above with "ls -al" as the command and it worked as expected, I'm not sure if a longer running process would require you to tweak the way you go about it-- if so you might need to use waitForProcessOutput:
new File('mail.tmp').withWriter { """mail -s "this is a test" my.mail@example.com""".execute().waitForProcessOutput(it, it) }
这篇关于尝试通过Groovy Shell脚本发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!