试图通过一个groovy shell脚本发送邮件 [英] Trying to send an email trough a groovy shell script

查看:256
本文介绍了试图通过一个groovy shell脚本发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我试图用groovy脚本替换一个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会分割一个参数这是一个测试到四个参数这个 code> 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆