abcreddy abcreddy - 1 year ago 64
Ruby Question

how to use linux 'mail' command from a ruby file to send a 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 = + timeout.minutes
loop do "Checking if process has finished"
if find_token != 0
update_completion_status "Process has finished"
elsif find_error != 0
update_timed_out_field "Process has errored"
elsif > wait_until_time
update_timed_out_field "Process has timed out"
else "Waiting for Process to finish"
sleep(60) # if it hasn't completed then wait 1 min and try again.

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"<<EOM
The process has finished successfully.

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?

Please Help.
Thank you.

Answer Source

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

to = ""
subject = "the process has been finished"
content = "The process has finished successfully."
`mail -s "#{subject}" #{to}<<EOM
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download