2

I've looked at all the SMTP ruby-docs and can't figure out where I'm going wrong:

def send(username, password, data, toAddress, fromAddress)

    smtp = Net::SMTP.new('my.smtp.host', 25)
    smtp.start('thisisunimportant', username, password, "plain") do |sender|                                                                       
        sender.send_message(data, fromAddress, toAddress)
    end

end

send(user, pass, rcpt, "Hey!")

Gives an unexpected kind of error:

/usr/lib/ruby/1.9.1/net/smtp.rb:725:in authenticate': wrong number of arguments (3 for 4) (ArgumentError) from /usr/lib/ruby/1.9.1/net/smtp.rb:566:indo_start' from /usr/lib/ruby/1.9.1/net/smtp.rb:531:in start' from gmx_pop.rb:24:insend' from gmx_pop.rb:30:in `'

I've tried kicking my computer a couple times but the problem persists.

Reed Spool
  • 843
  • 7
  • 15

2 Answers2

7

Here's a description of the Net::SMTP#start call:

http://ruby-doc.org/stdlib-1.9.1/libdoc/net/smtp/rdoc/Net/SMTP.html#method-i-start

the page mentions that you can just do SMTP.start to do everything at once.

Look like you are missing the port parameter. Try port 587 for secure authentication, if that doesn't work, port 25. (check the tutorial mentioned below)

Your call should look like this:

message_body = <<END_OF_EMAIL
From: Your Name <your.name@gmail.com>
To: Other Email <other.email@somewhere.com>
Subject: text message

This is a test message.
END_OF_EMAIL


server = 'smtp.gmail.com'
mail_from_domain = 'gmail.com'
port = 587      # or 25 - double check with your provider
username = 'your.name@gmail.com'
password = 'your_password'

smtp = Net::SMTP.new(server, port)
smtp.enable_starttls_auto
smtp.start(server,username,password, :plain)
smtp.send_message(message_body, fromAddress, toAddress)    # see note below!

Important:

  • Please note that you need to add To: , From: , Subject: headers to your message_body!
  • the Message-Id: and Date: headers will be added by your SMTP server

Check also:


Another way to send emails from Ruby:

You can use the ActionMailer gem from Rails to send emails from Ruby (without Rails). At first this seems like overkill, but it makes it much easier, because you don't have to format the message body with To: , From: , Subject: , Date: , Message-Id: Headers.

# usage:                                                                                                                                                                                               
#   include Email                                                                                                                                                                                       
#                                                                                                                                                                                                                                            
# TEXT EMAIL :   
#   send_text_email( 'sender@somewhere.com', 'sender@somewhere.com,receiver@other.com', 'test subject', 'some body text' )                                                               
# HTML EMAIL :
#   send_html_email( 'sender@somewhere.com', 'sender@somewhere.com,receiver@other.com', 'test subject', '<html><body><h1>some title</h1>some body text</body></html>' )                  


require 'action_mailer'

# ActionMailer::Base.sendmail_settings = {
#   :address => "Localhost",
#   :port => 25, 
#   :domain => "yourdomain.com"
# }

ActionMailer::Base.smtp_settings = {        # if you're using GMail
  :address              => 'smtp.gmail.com',  
  :port                 => 587,  
  :domain               => 'gmail.com',  
  :user_name            => "your-username@gmail.com"
  :password             => "your-password"
  :authentication       => "plain",  
  :enable_starttls_auto => true  
}

class SimpleMailer < ActionMailer::Base

  def simple_email(the_sender, the_recepients, the_subject, the_body , contenttype = nil)
    from the_sender
    recipients the_recepients
    subject the_subject
    content_type     contenttype == 'html' ? 'text/html' : 'text/plain'
    body the_body
  end
end

# see http://guides.rails.info/action_mailer_basics.html                                                                                                                                               
# for explanation of dynamic ActionMailer deliver_* methods.. paragraph 2.2                                                                                                                            

module Email
  # call this with a message body formatted as plain text                                                                                                                                              
  #                                                                                                                                                                                                    
  def send_text_email( sender, recepients, subject, body)
    SimpleMailer.deliver_simple_email( sender , recepients , subject , body)
  end

  # call this with an HTML formatted message body                                                                                                                                                      
  #                                                                                                                                                                                                    
  def send_html_email( sender, recepients, subject, body)
    SimpleMailer.deliver_simple_email( sender , recepients , subject , body, 'html')
  end
endsubject , body, 'html')
  end
end

e.g. the code above works if you want to use Gmail's SMTP server to send email via your Gmail account.. Other SMTP servers may need other values for :port, :authentication and :enable_starttls_auto depending on the SMTP server setup

Community
  • 1
  • 1
Tilo
  • 33,354
  • 5
  • 79
  • 106
  • Hey, thanks for all the info! I tried everything you suggested: switched the port, created a real email string instead of "Hey!", and tried throwing all the auth info in the start() call. I still get the exact same error I reported earlier :-/ – Reed Spool Nov 09 '11 at 17:09
  • I see how the gem could make it simpler, but forgive me, this exception is bugging the crud out of me and I want to solve it! – Reed Spool Nov 09 '11 at 19:38
0

Try this code

Net::SMTP.smtp.start('my.smtp.host', 25, 'mail.from.domain', username, password, :plain) do |smtp|                                                                       
    smtp.send_message data, fromAddress, toAddress
end
WarHog
  • 8,622
  • 2
  • 29
  • 23
  • I tried this before I tried the posted code, and it led to the same result. I split up start() and auth() because it was giving me that same error about auth, and I wanted to isolate it. – Reed Spool Nov 09 '11 at 17:10