tags:

views:

498

answers:

2

I'm trying to send simple email via Ruby (no rails) on OS X, with XCode (which installs Ruby.) But I'm running into a problem with my smtp server which requires the email client to check mail before sending as a form of authentication.

How can I get Ruby to authenticate with the smtp server in a "POP" fashion before I can send mail? Not download mail; I only want to send html formatted email (eventually via Applescript calling Ruby, because Applescript doesn't support smtp), but the server requires that I check mail before I send.

Edit 4/05/10:

Well, that's embarrasing. Turned out to be simpler; I was trying to make it more complex than it needed to be. Even though my mail server requires pop before smtp, this sends OK:

require 'net/smtp'

message = <<MESSAGE_END
    From: Private Person <[email protected]>
    To: A Test User <[email protected]>
    Subject: SMTP e-mail test

    This is a test e-mail message.
    MESSAGE_END

Net::SMTP.start('mail.mydomain.com', 25) do |smtp|
smtp.send_message message,
            '[email protected]',
            '[email protected]'
end

Edit 4/04/10:

With this I get a 500 unrecognized command error; the pop server is responding, though.

require 'net/smtp'
require 'net/pop'

message = <<MESSAGE_END
From: Private Person <[email protected]>
To: A Test User <[email protected]>
Subject: SMTP e-mail test

This is a test e-mail message.
MESSAGE_END

Net::POP3.start('mail.mydomain.com', 110, '[email protected]', 'password') do |pop|

// If this line is included,
// I get a printout of the number
// of emails on the server
// right before the error:
//
// puts pop.n_mails  end

Net::SMTP.start('mail.markratledge.com', 
                25, 
                'localhost', 
                '[email protected]', 'password', :plain) do |smtp|
  smtp.send_message message, '[email protected]', 
                             '[email protected]'
end
end
+2  A: 

POP before SMTP isn't one of the authentication types supported by Net::SMTP so I think you're going to have to use Net::POP3 to do your POP3 login e.g.

require 'net/pop'
pop = Net::POP3.start(addr, port, account, password)
pop.finish

Net::POP3 is in the Standard Library so should be available anywhere that Net::SMTP is.

mikej
Getting closer. With the code above I get a 200 reply code that the pop server is ready. (Obviously I'm just learning Ruby...)
songdogtech
the `start` should connect to the server and log you in. Try inserting a `puts pop.n_mails` after the `POP3.start` - this should print the number of messages in the mail box as a diagnostic that you're definitely talking to the POP server. Also try doing the `pop.finish` before making your SMTP connection. What happens now if you actually try to **send** a message via SMTP?
mikej
No luck with moving pop.finish; still a 500 error. And still can't send at all via SMTP without first checking for email. But I can login to the pop server with the code snip above.
songdogtech
Updated code above. The pop server is responding, but still a 500 error....
songdogtech
At the moment you're doing your `Net::SMTP.start` from inside the `Net::POP3.start` block. What if you move it outside? Do you get a descriptive message with the 500 error giving any clues?
mikej
Updated code; I was trying to make things too complex. Thanks for your help over the last few days.
songdogtech
A: 

If that doesn't make your server happy then Net::Telnet will let you send the raw commands yourself.

Azeem.Butt
I don't have telnet access to my server; only ssh.
songdogtech
If you open a telnet connection on the appropriate port numbers (110 for POP and 25 for SMTP) then you can send raw commands.
mikej
I checked again and you're both right; I do have telent access. I thought from the past telnet was disabled on this shared host. Eventually, the Ruby script will be called from an Applescript in order to send html formatted email, so telnet is handy to test the pop server, but can telnet recieve html formatted email?
songdogtech
I don't think you understood me at all. I was suggesting that you telnet into port 25 and send your own SMTP commands, not login to a telnet daemon.
Azeem.Butt
I pretty much did; I can telnet to port 25 and HELO and talk to the server that way, but I'm going to keep trying to work with the pop and smtp code above before I try the Ruby telnet include.
songdogtech