Search code examples
rubytelnet

send email via telnet ruby script


I'm new with ruby and need to send an email via telnet using a relay host with no authentication. I can do it with a linux shell but I need to put it in a script so I can "simplify" its use, I know it's not the best way but I can't find other since the server where i'm working on it's severely restricted and limited.

require 'net/telnet.rb'

mail = Net::Telnet::new(
     "Host"       => "domain.ip",  # default: "localhost"
     "Port"       => 25,           # default: 23
     "Output_log" => "output_log", # default: nil (no output)
     "Dump_log"   => "dump_log",   # default: nil (no output)
     "Prompt"     => /[$%#>] \z/n, # default: /[$%#>] \z/n
     "Telnetmode" => true,         # default: true
     "Timeout"    => 10,           # default: 10
     "Waittime"   => 0,            # default: 0
   )

mail.cmd('helo MYDOMAIN'){ |c| print c }
mail.cmd('mail from: test@domain.com')
mail.cmd('rcpt to: test2@domain.com')
mail.cmd('data')
mail.cmd("subject: test cmd \n\n mensaje de prueba\n\n")
mail.cmd(".\n")
mail.close

I found the net/telnet.rb ruby class and this is my try... after mail.cmd('helo MYDOMAIN') I can't keep writing other commands, what I get is:

220 mail.server.com ESMTP
250 mail.server.com

After this I'm suposed to write mail from, etc. to create the mail. But I can't in the ruby script. I have try using:

mail.puts('mail from: test...')
mail.write('mail from: test...')
mail.print('mail from: test...')
mail.cmd('mail from: test...')

As written in documentation

Also I don't get the telnetmode(true|false) command maybe you could explain it to me please.

-- Edit --

Shell code trying to emulate:

telnet domain.ip 25
#=> Trying domain.ip...
#=> Connected to domain.ip.
#=> Escape character is '^]'.
#=> 220 mail.server.com ESMTP
helo MYDOMAIN
#=>250 mail.server.com
mail from:test@mydomain.com
#=> 250 2.1.0 Ok
rcpt to:test2@mydomain.com
#=> 250 2.1.0 Ok
data
#=> 354 End data with <CR><LF>.<CR><LF>
subject: test mail

test mail body

.
#=> 250 2.0.0 =k: queued as B6F08480D12
quit
#=> 221 2.0.0 Bye
#=> Connection closed by foreign host.

Solution

  • Thanks to the help of the user ddubs how suggest the net\smtp gem (One that I didn't know) I was able to create a simple mail sender and using the mailfactory gem

    Is it a strict requirement that you use telnet? Using ruby-doc.org/stdlib-2.0.0/libdoc/net/smtp/rdoc/Net/SMTP.html will turn your "difficult to maintain" script into something that is much easier to maintain. Even for someone who is completely new to Ruby. – ddubs

    Here is the code sample

    require 'net/smtp'
    require 'mailfactory'
    
    mail_body_HTML = '<h1> mail title</h1> your text in <b>HTML</b>'    
    mail_body_PLAIN = 'this is plain text'
    mail_subject = 'test email'     
    mail_from = 'noreply@mydomain.com'
    mail_to = 'user@otherdomain.com'
    # mail_filePath = ''
    
    mail = MailFactory.new()
    mail.to = mail_to
    mail.from = mail_from
    mail.subject = mail_subject
    mail.html = mail_body_HTML
    # mail.text = mail_body_PLAIN
    # mail.attach(mail_filePath)
    
    relay_ip = x.x.x.x
    
    Net::SMTP.start(relay_ip,25) do |smtp|
       smtp.send_message(mail.to_s, mail_from, mail_to)
    end