markratledge
markratledge

Reputation: 17561

Ruby send mail with smtp

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

Upvotes: 1

Views: 2852

Answers (2)

Azeem.Butt
Azeem.Butt

Reputation: 5861

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

Upvotes: 1

mikej
mikej

Reputation: 66293

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.

Upvotes: 2

Related Questions