Smtp and Threads

Hi,
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=to.to_s()
    to.gsub!(" ","")
    to.gsub!(";",",")
    smtp = Net::SMTP.new('smtp.gmail.com', 587)
    smtp.enable_starttls
    smtp.start("smtp.gmail.com","myemail@gmail.com","mypwd",:login)
    to.split(",").each {|email|
emailMessage = "From: myemail <myemail@gmail.com>
Content-Type: text/html;

#{message.to_s()}
"
    smtp.send_message(emailMessage.to_s(),"myemail@gmail.com",
email.to_s())

  }
end
end

t = Thread.new do
  sendEmail("blabla@hotmail.com","mysubject","mybody")
end

Thanks in advance

···

To: <#{email.to_s()}>
Subject: #{subject.to_s()}

--
Posted via http://www.ruby-forum.com/.

look at thread join

···

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

Hi,
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=to.to_s()
    to.gsub!(" ","")
    to.gsub!(";",",")
    smtp = Net::SMTP.new('smtp.gmail.com', 587)
    smtp.enable_starttls
    smtp.start("smtp.gmail.com","myemail@gmail.com","mypwd",:login)
    to.split(",").each {|email|
emailMessage = "From: myemail <myemail@gmail.com>
To: <#{email.to_s()}>
Subject: #{subject.to_s()}
Content-Type: text/html;

#{message.to_s()}
"
    smtp.send_message(emailMessage.to_s(),"myemail@gmail.com",
email.to_s())

  }
end
end

t = Thread.new do
  sendEmail("blabla@hotmail.com","mysubject","mybody")
end

Thanks in advance

--
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.

···

--
Posted via http://www.ruby-forum.com/.

Thanks
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

···

--
Posted via http://www.ruby-forum.com/.

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

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 program
end.

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

regards,

Harisankar P S
https://twitter.com/coder_hsps | http://tech.hsps.in

···

On Wed, Oct 9, 2013 at 6:29 PM, Mario Ruiz <lists@ruby-forum.com> wrote:

Hi,
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=to.to_s()
    to.gsub!(" ","")
    to.gsub!(";",",")
    smtp = Net::SMTP.new('smtp.gmail.com', 587)
    smtp.enable_starttls
    smtp.start("smtp.gmail.com","myemail@gmail.com","mypwd",:login)
    to.split(",").each {|email|
emailMessage = "From: myemail <myemail@gmail.com>
To: <#{email.to_s()}>
Subject: #{subject.to_s()}
Content-Type: text/html;

#{message.to_s()}
"
    smtp.send_message(emailMessage.to_s(),"myemail@gmail.com",
email.to_s())

  }
end
end

t = Thread.new do
  sendEmail("blabla@hotmail.com","mysubject","mybody")
end