Linux mail, mongrel and others

  • Digg this article
  • Sphinn this article
  • Stumble this article
  • Facebook
  • del.icio.us
  • LinkedIn
  • Twit this article
Posted by John
on Sunday, 17 February 2008

Software Install Commands

sudo aptitude install postfix
sudo aptitude install sendmail

sudo aptitude remove postfix
sudo aptitude remove sendmail

install telnet (not installed by default)

sudo aptitude install telnet

Mail Commands

show pending mail in queue

mailq

access mail system

mail

pending mails stored in /var/spool

Sending Mail Manually with Telnet

telnet localhost 25

ehlo localhost
mail from: root@localhost
rcpt to: fmaster@localhost
data
Subject: hello
some content
.
quit

...the dot marks the end of the email

SendMail Commands

sudo /etc/init.d/sendmail start
sudo /etc/init.d/sendmail stop
sudo /etc/init.d/sendmail restart

config files saved in /etc/mail/config.cf

Mongrel Cluster Commands

mongrel_rails cluster::start
mongrel_rails cluster::stop
mongrel_rails cluster::restart

MySQL login

login to server

mysql -u root -p

Shutdown Server Gracefully

shutdown now and reboot

shutdown -r now

Login as Another

su anotheruser

Show Pending Jobs

jobs

Mongrel Cluster Restart

mongrel_rails cluster::restart -C /var/www/apps/[myapp]/config/mongrel_cluster.yml

replace [myapp] with your apps dir

CRONTAB + Mongrel - fire on restart

add above line to /home/deploy/start.sh

crontab -e

then add...

@reboot (sh /home/deploy/start.sh) > /dev/null 2>&1

save and should fire + restart on reboot

Kill Processes

find if firefox is running...

ps aux | grep firefox

kill firefox...

killall firefox

or specifically...

kill 19919  <- processid

NGINX

sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx restart

Sending mail with Rails

  • Digg this article
  • Sphinn this article
  • Stumble this article
  • Facebook
  • del.icio.us
  • LinkedIn
  • Twit this article
Posted by John
on Monday, 05 November 2007

Finally managed to work this out, what follows is something that does 100% work!

Install your Mail Client

Choose which one you're gonna use, SendMail or the more powerful Postfix;

sudo aptitude install sendmail
..or
sudo aptitude install postfix

...however, for this example I'm going to be using the easier Sendmail.

Create your Mailer Model

First things first, your gonna need a mailer model, so to create one go to the root of your rails app and type...

script/generate mailer UserMailer

This will generate a couple of new directories but more importantly thru the magic of rails by using 'mailer' it will know that your creating a model that will use the ActionMailer library and so things will be handled differently with this one than other models.

Setup Rails to use SendMail

Now we could use SMTP to send emails from our web app, but to be perfectly honest that's like a major headache for the newbie to setup. So as long as your running this on a linux box you can use the more simpler SendMail libaries to send email.

To use these in production, edit your production.rb environment file...

myapp/config/environments/production.rb

and add these lines...

config.action_mailer.raise_delivery_errors = true

ActionMailer::Base.delivery_method = :sendmail

ActionMailer::Base.sendmail_settings = {
:location       => '/usr/sbin/sendmail',
:arguments      => '-i -t'
}

What the first line will do is notify you of any delivery errors, useful in testing, you can turn this off in production by setting it to false.

  • :location tells the actionmailer where to find the sendmail libraries, default location.

  • :arguments tell sendmail to send the email immediately, rather than wait, so your email does get sent.

That's that done, remember you could use SMTP but it can be a nightmare to setup installing postfix, setting your MX records etc. this however is simple & quick.

Back to our UserMailer model

Now that's all sorted, open your UserMailer model...

myapp/app/models/user_mailer.rb

And add...

def test
recipients  "user@destination.com"
from        "admin@source.com"
subject     "Thank you for Registering"
body        "test email"
end

What we've done is created a method that will email user@destination.com when it get's called.

Which can be done by using...

User_Mailer.deliver_test

...anywhere in our app's code.

Notice the deliver_ prefix in front of our method test. This is how we associate our 'magic' ActionMailer methods associated when we generated the model.

Further Reading

You can optionally expand this with the examples below, but hopefully the above has helped you get over one of the major headaches of Ruby on Rails.