如何使用 ruby​​ 文件中的 linux 'mail' 命令发送邮件? [英] how to use linux 'mail' command from a ruby file to send a mail?

查看:33
本文介绍了如何使用 ruby​​ 文件中的 linux 'mail' 命令发送邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


在下面的代码中,我必须发送一封关于进程状态的电子邮件,无论它是完成、错误还是超时......


In the below code, I have to send an e-mail on the status of the process whether it is completed, error out or timed out...

    def check_for_forecasts
   wait_until_time = Time.now + timeout.minutes
   loop do
       RAILS_DEFAULT_LOGGER.info "Checking if process has finished"
     if find_token != 0
       update_completion_status
       RAILS_DEFAULT_LOGGER.info "Process has finished"
       break
     elsif find_error != 0
       update_timed_out_field
       RAILS_DEFAULT_LOGGER.info "Process has errored"
       break
     elsif DateTime.now > wait_until_time
       update_timed_out_field
       RAILS_DEFAULT_LOGGER.info "Process has timed out"
       break
     else
       RAILS_DEFAULT_LOGGER.info "Waiting for Process to finish"
       sleep(60) # if it hasn't completed then wait 1 min and try again.
     end
    end
  end

一般来说,我们只在 .sh 文件而不是 .rb 文件中使用 linux 'mail' 命令.下面是我们如何编写 'mail' 命令以在 .sh 文件中发送邮件.

In general, we use linux 'mail' command only in .sh files not in the .rb files. Below is how we write the 'mail' command to send mails in .sh files.

 mail -s "the process has been finished" abc@xyz.com<<EOM
      The process has finished successfully.    
EOM

有没有办法在.rb 文件中使用简单的邮件命令?还是我必须为此安装任何 gem?

Is there any way to use the simple mail command in the .rb file? Or do I have to install any gems for the same?

请帮忙.谢谢.

推荐答案

一个非常丑陋但有用的方法,从 ruby​​ 中做同样的事情:

A really ugly but useful way to do the same from ruby:

to = "abc@xyz.com"
subject = "the process has been finished"
content = "The process has finished successfully."
`mail -s "#{subject}" #{to}<<EOM
  #{content}
EOM`

这篇关于如何使用 ruby​​ 文件中的 linux 'mail' 命令发送邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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