Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 451
  • Last Modified:

Sending Email from VPS Server - Ruby on Rails App

Using these smtp setting in development i can send email fine but now that i have pushed the application onto a server the emails are not being delivered. I guessing it may have to do with the security settings on the server. I don't see any errors.
#config/environment.rb
config.action_mailer.raise_delivery_errors = true
  config.action_mailer.perform_deliveries = true 
  config.action_mailer.delivery_method  :smtp
  config.action_mailer.smtp_settings = {
    :enable_starttls_auto => true,
    :address => "smtp.gmail.com",
    :port => 587,
    :domain => "mydomain.org",
    :authentication => :plain,
    :user_name => "support@mydomain.org",
    :password => "mypassword"
  }

#on the server
sudo iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            
REJECT     all  --  anywhere             127.0.0.0/8         reject-with icmp-port-unreachable 
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED 
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:www 
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:https 
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:8888 
ACCEPT     icmp --  anywhere             anywhere            icmp echo-request 
LOG        all  --  anywhere             anywhere            limit: avg 5/min burst 5 LOG level debug prefix `iptables denied: ' 
REJECT     all  --  anywhere             anywhere            reject-with icmp-port-unreachable 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere            reject-with icmp-port-unreachable 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere

Open in new window

0
depassion
Asked:
depassion
  • 10
  • 7
2 Solutions
 
cminearCommented:
Based on your question, I would assume that the development and production systems are 2 different ones, with the production one hosted by an external hosting service.  If that is not the case, please clarify.

First check that your web hoster is not blocking access to port 587.  (Since you indicate you have a VPS, I'll assume you have shell access.)  Use netcat ('nc') or telnet to try to reach port 587.

   telnet smtp.gmail.com 587

If you are not blocked, you should see a line like "220 mx.google.com ESMTP ...".  (If you do get this, just enter "quit" and the connection should be closed.)  If you don't get this, then something upstream from your VPS is blocking access to GMail port 587.  This would likely be the web hoster and you should check with their support.  (They likely have their own SMTP servers and would be expecting you to use them instead.)

If the connection to port 587 was not blocked, I would try running in production mode on your development system: script/server production.  (You will need to have a production database of some sort created and working on your development system, obviously.)  I wouldn't expect this to be the problem, but it is worth checking that there isn't a problem with the production environment that is blocking this.

Have you double-checked that all the gem versions on the VPS are the same as what you have in your development environment?
0
 
depassionAuthor Commented:
i can connect via telnet ok.

i can send email in production mode on local machine.

devlopment machine:

actionmailer (2.3.5, 2.3.4)
actionpack (2.3.5, 2.3.4)
activerecord (2.3.5, 2.3.4)
activeresource (2.3.5, 2.3.4)
activesupport (2.3.5, 2.3.4)
autotest-growl (0.1.7)
autotest-rails (4.1.0)
aws-s3 (0.6.2)
builder (2.1.2)
capistrano (2.5.10, 2.5.9)
capistrano-ext (1.2.1)
cgi_multipart_eof_fix (2.5.0)
chronic (0.2.3)
coderay (0.8.357)
configuration (1.1.0)
crack (0.1.4)
cucumber (0.4.4, 0.3.104)
daemons (1.0.10)
diff-lcs (1.1.2)
factory_girl (1.2.3)
fastercsv (1.5.0)
fastthread (1.0.7)
friendly_id (2.2.5, 2.2.4)
gem_plugin (0.2.3)
gemcutter (0.2.1)
heroku (1.4, 1.3.0)
highline (1.5.1)
hoe (2.3.3)
httparty (0.5.0)
imagesize (0.1.1)
inherited_resources (0.9.3)
json (1.2.0)
json_pure (1.2.0, 1.1.9)
launchy (0.3.3)
lesstile (0.3.0)
mime-types (1.16)
mislav-will_paginate (2.3.11)
mongrel (1.1.5)
mysql (2.8.1)
net-scp (1.0.2)
net-sftp (2.0.4, 2.0.2)
net-ssh (2.0.16, 2.0.15)
net-ssh-gateway (1.0.1)
newrelic_rpm (2.9.8, 2.9.5)
nokogiri (1.4.0, 1.3.3)
passenger (2.2.7)
polyglot (0.2.9)
rack (1.0.1)
radiant (0.9.0, 0.8.1)
rails (2.3.5, 2.3.4)
rake (0.8.7)
rapt (0.2.2)
RedCloth (4.2.2)
redgreen (1.2.2)
rest-client (1.0.3)
right_aws (1.10.0)
right_http_connection (1.2.4)
rspec (1.2.9)
rspec-rails (1.2.9)
ruby-openid (2.1.7)
rubyforge (2.0.3)
rufus-scheduler (2.0.3)
sanitize (1.1.0)
sqlite3-ruby (1.2.5)
syntax (1.0.0)
term-ansicolor (1.0.4)
treetop (1.4.2)
webrat (0.6.0, 0.5.3)
whenever (0.4.1)
will_paginate (2.3.11)
xml-simple (1.0.12)
ZenTest (4.1.4)

Server:
actionmailer (2.3.5)
actionpack (2.3.5)
activerecord (2.3.5)
activeresource (2.3.5)
activesupport (2.3.5)
autotest-rails (4.1.0)
builder (2.1.2)
cgi_multipart_eof_fix (2.5.0)
chronic (0.2.3)
coderay (0.8.357)
configuration (1.1.0)
cucumber (0.4.4)
daemons (1.0.10)
diff-lcs (1.1.2)
elif (0.1.0)
factory_girl (1.2.3)
fastercsv (1.5.0)
fastthread (1.0.7)
friendly_id (2.2.6)
gem_plugin (0.2.3)
hoe (2.4.0)
json_pure (1.2.0)
lesstile (0.3.0)
mongrel (1.1.5)
mysql (2.8.1)
passenger (2.2.7)
polyglot (0.2.9)
rack (1.0.1)
rails (2.3.5)
rake (0.8.7)
RedCloth (4.2.2)
rspec (1.2.9)
rspec-rails (1.2.9)
ruby-openid (2.1.7)
rubyforge (2.0.3)
rufus-scheduler (2.0.3)
scout (4.0.2)
term-ansicolor (1.0.4)
treetop (1.4.3, 1.4.2)
will_paginate (2.3.11)
ZenTest (4.2.1)


0
 
Andrew DoadesIT TechnicianCommented:
Have you check the production.log after you try to send an email?

Try and sent the email with the rails app then check the production log, it will tell us a lot.
If the mail is actually being sent, if not why, etc etc.

Andrew
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
depassionAuthor Commented:
can't see any errors in the poduction.log
0
 
Andrew DoadesIT TechnicianCommented:
Is the log showing that the email is being sent however?
0
 
Andrew DoadesIT TechnicianCommented:
also if its possible, you need to check the mail log..
0
 
depassionAuthor Commented:
no errors in the mail logs.
0
 
Andrew DoadesIT TechnicianCommented:
but does the mail log show that the email is being sent?
0
 
depassionAuthor Commented:
no, its blank.
0
 
depassionAuthor Commented:
ok i actually have 2 different email processes happening. When a new user signs up they get a welcome email, that is working fine.

But all the actual subscription emails are not working, perhaps the problem has to do with the RufusScheduler.


##Emails working

#users_controller.rb
def create
    @user = User.new(params[:user])
    if @user.save
      UserMailer.deliver_registration_confirmation(@user)
      flash[:notice] = "Successfully registered"
      redirect_to :controller => "lotto", :action => "index"
    else
      render :action => 'new'
    end
  end

#user_mailer.rb
def registration_confirmation(user)
    recipients   user.email
    from         "no-reply@lottomail.net"
    subject      "Thanks for Registering with Lottomail"
    body         :user => user
    content_type "text/html"
  end


##Emails not working

#config/initializers/task_scheduler.rb
#Lotto Wednesday Draw => Every Wednesday at 0600 UTC
 scheduler.cron('0 6 * * wed') do  
  @subscription = Subscription.find(2)
  @users = @subscription.users
  @subject = @subscription.subject
    for user in @users
      @lotto_numbers = (1..49).to_a.sort{rand() - 0.5 } [0..5]
      @recipients = user.email
      UserMailer.deliver_lotto_wednesday_subscription(@recipients, @subject, @lotto_numbers)
      @subscription.update_attribute(:delivered_at, Time.now)
    end
end

#user_mailer.rb
def lotto_wednesday_subscription(to_addresses, subject, lotto_numbers)
  recipients   to_addresses
  from         "no-reply@lottomail.net"
  subject      subject
  body         :lotto_numbers => lotto_numbers
  content_type "text/html"
  
end

Open in new window

0
 
Andrew DoadesIT TechnicianCommented:
If mail IS actually being sent on way or another then mail sending it working.

The next thing is to try and see what rufus is doing!

Can you post the rufus code here? or if you're keeping a log, check the rufus log
0
 
depassionAuthor Commented:

require 'rubygems'
 require 'rufus/scheduler'
 require 'user_mailer'
 
 scheduler = Rufus::Scheduler.start_new   
 
#Lotto Saturday Draw => Every Saturday at 0600 UTC
 scheduler.cron('0 6 * * sat') do  
  @subscription = Subscription.find(1)
  @users = @subscription.users
  @subject = @subscription.subject
    for user in @users
      @lotto_numbers = (1..49).to_a.sort{rand() - 0.5 } [0..5]
      @recipients = user.email
      UserMailer.deliver_lotto_saturday_subscription(@recipients, @subject, @lotto_numbers)
      @subscription.update_attribute(:delivered_at, Time.now)
    end
end

#Lotto Wednesday Draw => Every Wednesday at 0600 UTC
 scheduler.cron('0 6 * * wed') do  
  @subscription = Subscription.find(2)
  @users = @subscription.users
  @subject = @subscription.subject
    for user in @users
      @lotto_numbers = (1..49).to_a.sort{rand() - 0.5 } [0..5]
      @recipients = user.email
      UserMailer.deliver_lotto_wednesday_subscription(@recipients, @subject, @lotto_numbers)
      @subscription.update_attribute(:delivered_at, Time.now)
    end
end

Open in new window

0
 
Andrew DoadesIT TechnicianCommented:
For the rufus code, the file user_mailer.rb is it in the libs folders?

For the controller it will read the model, but if you require it in the rufus code, I believe it'll look in the libs folder
0
 
depassionAuthor Commented:
user_mailer is a model in app/models/user_mailer.rb. but everything is working in development.

 Anyway i move it into lib directory but again nothing is sent.
0
 
depassionAuthor Commented:
ok this is strange, i have the following test working on the server but inside a different app. The same code in the lottery app does nothing. As far as i can tell the environments are the same. what else could be affecting the scheduler code.

 require 'rubygems'
 require 'rufus/scheduler'
 
 
 scheduler = Rufus::Scheduler.start_new  
 
   scheduler.cron('55 13 * * thu') do
     puts "test"
   end
 
0
 
depassionAuthor Commented:
SOLUTION!!

The user_mailer.rb file was moved to lib directory.
require 'rubygems'
 require 'rufus/scheduler'

 
 scheduler = Rufus::Scheduler::PlainScheduler.start_new   
 
 def scheduler.handle_exception (job, exception)
   puts "job #{job.job_id} caught exception '#{exception}'"
 end
 
    #Lotto Wednesday Draw => Every Wednesday at 1400 UTC
     scheduler.cron('0 14 * * wed') do  
      @subscription = Subscription.find(2)
      @users = @subscription.users
      @subject = @subscription.subject
      puts @subject
        for user in @users
          @lotto_numbers = (1..49).to_a.sort{rand() - 0.5 } [0..5]
          @recipients = user.email
          UserMailer.deliver_lotto_wednesday_subscription(@recipients, @subject, @lotto_numbers)
          @subscription.update_attribute(:delivered_at, Time.now)
        end
    end

Open in new window

0
 
Andrew DoadesIT TechnicianCommented:
So my comment above about moving it to the 'libs' folder was the solution ?
0
 
depassionAuthor Commented:
i had to change this:
scheduler = Rufus::Scheduler.start_new  

to this:
scheduler = Rufus::Scheduler::PlainScheduler.start_new

don't know what that did, but it works now. And also moving user_mailer.rb into the libs folder.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 10
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now