Pony Mail Gem Throws Broken Pipe Error - ruby

So I'm using sinatra, and I'm trying to send emails from the app for basic verification reasons. I was poking around the docs, and I found the gem pony, which seems to be right up my alley. In my app.rb file I have
require 'rubygems'
require 'sinatra'
require 'pony'
require 'mail'
post '/signup' do
Pony.mail :to => "myself#me.com", :body => "User Sign Up!", :subject => "score"
end
but I end up with a broken pipe error. See below.
I went in to the sendmail.rb, but any change to that and I just got different errors. I think some other people have experienced a similar problem.

Can you send mail with Pony manually via IRB?
$ irb
>> require 'rubygems'
>> require 'pony'
>> mail = Pony.mail :to => "myself#me.com", :body => "User sign up!", :subject => "score"
You may need to add more options (such as SMTP servers). See https://github.com/benprew/pony for configuration.

If you find yourself having the same problem as me, it's very easy to fix. Info here
Basically you just have to run these three lines in the terminal
sudo mkdir -p /Library/Server/Mail/Data/spool
sudo /usr/sbin/postfix set-permissions
sudo /usr/sbin/postfix start
This answer was given to me at https://apple.stackexchange.com/questions/54051/sendmail-error-on-os-x-mountain-lion

Related

Why am I getting this error when I use 'rackup' but not when I use 'ruby'

I am new to Ruby and Sinatra, and I am attempting to finish this tutorial: http://www.sitepoint.com/just-do-it-learn-sinatra-iv/
At the end of the tutorial, before deployment, the tutorial says "Before we move on to deploying the app, we should just check that everything is running fine on our local machine. This is done in a different way now that we have a rackup file." It then instructs the student to enter "rackup" at the command line. When I do so, then go to the 9292 local port specified in my browser I get the error:
DataObjects::ConnectionError at / file is encrypted or is not a database
But when I run my application by typing "ruby main.rb" in the same directory, the app works fine in my browser. Is the tutorial wrong? Am I fine to just deploy my app to Heroku as is? Or do I need to get it working using the rackup command?
I am using sqlite3 1.3.9 and ruby 2.0.0
here is my 'main.rb' file:
require 'sinatra'
require 'data_mapper'
require 'slim'
require 'sass'
DataMapper.setup(:default, 'sqlite3::memory:')
class Task
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true
property :completed_at, DateTime
belongs_to :list
end
class List
include DataMapper::Resource
property :id, Serial
property :name, String, :required => true
has n, :tasks, :constraint => :destroy
end
Task.auto_upgrade!
List.auto_upgrade!
#checks integrty of models
DataMapper.finalize
#For using SASS
get('/styles.css'){ content_type 'text/css', :charset => 'utf-8' ; scss :styles }
get '/' do
#lists = List.all(:order => [:name])
slim :index
end
post '/:id' do
List.get(params[:id]).tasks.create params['task']
redirect to('/')
end
delete '/task/:id' do
Task.get(params[:id]).destroy
redirect to('/')
end
#this adds a button that toggles a checkmark on and off
#so you can check something off your list
put '/task/:id' do
task = Task.get params[:id]
task.completed_at = task.completed_at.nil? ? Time.now : nil
task.save
redirect to('/')
end
post '/new/list' do
List.create params['list']
redirect to('/')
end
delete '/list/:id' do
List.get(params[:id]).destroy
redirect to('/')
end
my config.ru file:
require 'bundler'
Bundler.require
require './main'
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/development.db")
run Sinatra::Application
and my Gemfile
source :rubygems
gem "sinatra"
gem "datamapper"
gem "slim"
gem "sass"
gem "dm-postgres-adapter", :group => :production
gem "dm-sqlite-adapter", :group => :development
gem 'tilt', '~> 1.4.1'
I also have a config file in a .bundle folder that says
---
BUNDLE_WITHOUT: production
I'm not sure where to start.
My code is already different in a few ways than the final code posted at:
https://github.com/daz4126/Just-Do-It
because I have already had some errors that I had to make workarounds for. Copy pasting the final code doesn't make it work.
Thanks in advance. I have other files I can post, just figured the relevant files were these.

Sinatra app with multiple controllers: how do I tell rspec what the routes are?

I'm working on an existing sinatra app that spans a number of controllers. These are currently routed in the config.ru file like so:
require 'bundler'
Bundler.require(:default)
=begin
# The gem file includes these:
gem 'rack' , '1.5.2', :groups => [:default, :test]
gem 'rack-accept' , '0.4.5'
gem 'rack-mount' , '0.8.3'
gem 'rack-protection' , '1.5.1'
gem 'rack-test' , '0.6.2', :groups => [:test]
=end
use Rack::ContentType
maps = {
'/' => RootController,
'/users' => UsersController,
'/animals' => AnimalsController,
}
maps.each do |path, controller|
map(path){ run controller}
end
This file is launched via config.ru (under Thin). But test files don't run under rackup (or Thin).
How do I load the controllers under rspec?
The problem is that when I put a 'get "/PATH"' or 'post' in my tests, Ruby complains about an
argument mismatch (2 for 0). If the 'get' has no argument, Ruby gives a different argument
mismatch (0 for 1). They're both sort of wrong -- get takes a path, an optional hash, and an optional block.
So something is clearly not wired up correctly.
Some of the code is here:
config.ru: http://pastie.org/8673836
spec_helper.rb at http://pastie.org/8673835
Error message at http://pastie.org/8673793
Simple controller at http://pastie.org/8673785
The names are slightly different in the pasties, but the gist is the same.
How do you wire up the controllers when you don't have the environment
config.ru gives you?
Thanks for any help.
The problem is somewhere in the code base, but rack doesn't make it easy to determine where.
I filed a bug on this at
https://github.com/rack/rack/issues/652

Rack/Sinatra Method Not Allowed

I am developing a simple web app using Sinatra and using rack as the middleware and hence have a config.ru.
To run the application I use shotgun config.ru.
I have no problem when the application does a GET request. But my app has a couple of POST requests, and when I submit a form via POST method, I get this strange error:
Method Not Allowed
Following is the content of my config.ru:
require "rack"
require 'rack/contrib/try_static'
require File.expand_path("app", File.dirname(__FILE__))
use Rack::TryStatic, :root => File.join(App::SETTINGS.source, App::SETTINGS.site.config['destination']), :urls => %w[/]
run App
Any idea what could resolve the issue?
Thank You
The following will not respond to posts:
get '/hi' do
"Hello World!"
end
It is quite possible that you will need to do something like this:
post '/hi' do
# do post stuff
end
I solved the issue.
It was a problem with rack.
I replaced
use Rack::TryStatic,
:root => File.join(App::SETTINGS.source, App::SETTINGS.site.config['destination']),
:urls => %w[/]
with:
use Rack::Static,
:urls => ["/#{App::SETTINGS.site.config['destination']}"],
:root => File.join(App::SETTINGS.source, App::SETTINGS.site.config['destination'])

Sending mails automatically through action mailer Rails 3.1

I have to send weekly emails to all the user about the latest things happening. I am using ActionMailer to accomplish other mailing task however I have no clue how to automate the weekly emails.
Update
I found whenever gem which could be used to schedule cron jobs. I guess this could be used to send weekly emails which I intend to. Still looking how to make it work with ActionMailer will update once I find the solution
Update 2
This is what I have done so far using whenever gem:-
in schedule.rb
every 1.minute do
runner "User.weekly_update", :environment => 'development'
end
in users_mailer.rb
def weekly_mail(email)
mail(:to => email, :subject => "Weekly email from footyaddicts")
end
in users.rb
def self.weekly_update
#user = User.all
#user.each do |u|
UsersMailer.weekly_mail(u.email).deliver
end
end
If i try to run User.weekly_update from the console I am able to get the mails. I am testing in development mode and using rvm. I checked my crontab file and it has got the right stuff.
However I am not getting any mails automatically from the app. Any clue what might be wrong?
Thanks,
OK so it turns out to be a path issue with whenever gem, and the problem was created when I installed another version of ruby.
In my machine the new ruby version is installed in /usr/local/bin/ruby. In my rails app I had to go to the file script/rails and replace #!/usr/bin/env ruby with #!/usr/local/bin/ruby.
I found this out by visiting cron.log file which showed this error message :- /usr/bin/env: ruby: No such file or directory
I made a cron.log file to log the cron error this is what I did in my schedule.rb code written in the question :-
every 2.minutes do
runner "User.weekly_update", :environment => 'development', :output => 'log/cron.log'
end
I am getting periodic mails now.
It seems like you haven't configured ActionMailer settings.
First check out the logs from console, whether the mailing process is working(paste your logs).
If yes then do following steps.
add this in your gemfile.
gem 'tlsmail'
run
bundle install
write these configuration setting in your config/environments/development.rb file
require 'tlsmail'
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:enable_starttls_auto => true,
:authentication => :login,
:user_name => "<address>#gmail.com",
:password => "<password>"
}
config.action_mailer.raise_delivery_errors = true
add your working password/email against user_name and password.
Don't forget to restart server.

Send email from Ruby program with TLS

I am trying to send an email from a Ruby program. The smtp server is an Exchange 2007 server that requires me to login to send the email.
#!/usr/bin/ruby
require 'rubygems'
require 'net/smtp'
msgstr = "Test email."
smtp = Net::SMTP.start('smtp.server.com', 587,
'mail.server.com', 'username', 'password', :login) do |smtp|
smtp.send_message msgstr, 'from#server.com', 'to#server.com'
end
When I run this I get the following error.
/usr/lib/ruby/1.8/net/smtp.rb:948:in `check_auth_continue':
530 5.7.0 Must issue a STARTTLS command first (Net::SMTPAuthenticationError)
from /usr/lib/ruby/1.8/net/smtp.rb:740:in `auth_login'
from /usr/lib/ruby/1.8/net/smtp.rb:921:in `critical'
from /usr/lib/ruby/1.8/net/smtp.rb:739:in `auth_login'
from /usr/lib/ruby/1.8/net/smtp.rb:725:in `send'
from /usr/lib/ruby/1.8/net/smtp.rb:725:in `authenticate'
from /usr/lib/ruby/1.8/net/smtp.rb:566:in `do_start'
from /usr/lib/ruby/1.8/net/smtp.rb:525:in `start'
from /usr/lib/ruby/1.8/net/smtp.rb:463:in `start'
I can't find anything in the Ruby api for Net::SMTP referencing TLS or a STARTTLS command.
EDIT:
Download smtp-tls.rb, the code doesn't change much but here is what I got to work.
#!/usr/bin/ruby
require 'rubygems'
require 'net/smtp'
require 'smtp-tls'
msgstr = <<MESSAGE_END
From: me <me#test.com>
To: you <you#test.com>
Subject: e-mail test
body of email
MESSAGE_END
smtp = Net::SMTP.start('smtp.test.com', 587, 'test.com', 'username', 'passwd', :login) do |smtp|
smtp.send_message msgstr, 'me#test.com', ['you#test.com']
end
EDIT: Works with ruby 1.8.6
No gem required in Ruby 1.9.3
You don't need a gem to do this (at least, not for Gmail and Ruby 1.9.3), the documentation is just terrible.
See https://stackoverflow.com/a/3559598/4376
I couldn't get it working using your updated example. But the following did work for me.
require 'rubygems'
require 'smtp_tls'
require 'net/smtp'
smtp = Net::SMTP.new 'smtp.gmail.com', 587
smtp.enable_starttls
smtp.start(Socket.gethostname,username,password,:login) do |server|
server.send_message msg, from, to
end
Net::SMTP doesn't look like it supports startTLS encryption. I did find a project on github that has code for dealing with this, though.
smtp-tls
This is a solution using the tlsmail gem:
require 'rubygems'
require 'tlsmail'
require 'net/smtp'
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
Net::SMTP.start(server,25,Socket.gethostname,username,password,:login) do |server|
server.send_message msg, from, to
end

Resources