Smtp and Threads

I have a method sendEmail that is sending emails correctly and i decided
to call the method inside a Thread so I don’t have to wait until the
email is sent but I don’t know why it is not working, this is the code:

require ‘net/smtp’

def sendEmail(to, subject, message)
to.gsub!(" “,”")
smtp =‘’, 587)
smtp.start(“”,“[email protected]”,“mypwd”,:login)
to.split(",").each {|email|
emailMessage = "From: myemail [email protected]
To: <#{email.to_s()}>
Subject: #{subject.to_s()}
Content-Type: text/html;

smtp.send_message(emailMessage.to_s(),“[email protected]”,


t = do
sendEmail(“[email protected]”,“mysubject”,“mybody”)

Thanks in advance

On Wed, Oct 9, 2013 at 6:29 PM, Mario R. [email protected] wrote:



Your application (process) ends before the threads completes. I tried
program, it seems you have an extra end. (sniper I ran : )

I made it to wait if the thread is alive, after a while it send its
mail. I
am not completely sure of it myself, but i guess the main program must
continue to be active. As the threads seems to die, as soon as the

I would suggest that you use a library for background processing rather
than implementing threads on your own, as such libraries would have a
to alert you incase they fail to complete.


Harisankar P S |

On Wed, 2013-10-09 at 14:59 +0200, Mario R. wrote:



Thanks in advance

look at thread join

If you have received the message in error, please advise the sender by
reply email and please delete the message. This message contains
information which may be confidential or otherwise protected. Unless
you are the addressee (or authorized to receive for the addressee), you
may not use, copy, or disclose to anyone the message or any information
contained in the message.

Thread.join is what you’re missing, although since you’re only spawning
a single Thread, you’re not really gaining anything there. You also set
“t” as the Thread, but you don’t do anything with it.

Given the way you lay out your email body, you might want to look up
Heredocs as well.

yep it seems the problem was the execution finish before finishing the
thread but in production is not going to end since it is a sinatra
script runnning all the time :slight_smile:
thanks again

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs