title: send email via telnet ruby script
body: <p>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.</p>
  
  <pre><code>require 'net/telnet.rb'
  
  mail = Net::Telnet::new(
       "Host"       =&gt; "domain.ip",  # default: "localhost"
       "Port"       =&gt; 25,           # default: 23
       "Output_log" =&gt; "output_log", # default: nil (no output)
       "Dump_log"   =&gt; "dump_log",   # default: nil (no output)
       "Prompt"     =&gt; /[$%#&gt;] \z/n, # default: /[$%#&gt;] \z/n
       "Telnetmode" =&gt; true,         # default: true
       "Timeout"    =&gt; 10,           # default: 10
       "Waittime"   =&gt; 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
  </code></pre>
  
  <p>I found the <code>net/telnet.rb</code> ruby class and this is my try... after <code>mail.cmd('helo MYDOMAIN')</code> I can't keep writing other commands, what I get is:</p>
  
  <pre><code>220 mail.server.com ESMTP
  250 mail.server.com
  </code></pre>
  
  <p>After this I'm suposed to write <code>mail from</code>, etc. to create the mail. But I can't in the ruby script. I have try using:</p>
  
  <pre><code>mail.puts('mail from: test...')
  mail.write('mail from: test...')
  mail.print('mail from: test...')
  mail.cmd('mail from: test...')
  </code></pre>
  
  <p>As written in <a href="https://ruby-doc.org/stdlib-1.9.3/libdoc/net/telnet/rdoc/Net/Telnet.html" rel="nofollow noreferrer">documentation</a></p>
  
  <p>Also I don't get the <code>telnetmode(true|false)</code> command maybe you could explain it to me please.</p>
  
  <p>-- Edit --</p>
  
  <p>Shell code trying to emulate:</p>
  
  <pre><code>telnet domain.ip 25
  #=&gt; Trying domain.ip...
  #=&gt; Connected to domain.ip.
  #=&gt; Escape character is '^]'.
  #=&gt; 220 mail.server.com ESMTP
  helo MYDOMAIN
  #=&gt;250 mail.server.com
  mail from:test@mydomain.com
  #=&gt; 250 2.1.0 Ok
  rcpt to:test2@mydomain.com
  #=&gt; 250 2.1.0 Ok
  data
  #=&gt; 354 End data with &lt;CR&gt;&lt;LF&gt;.&lt;CR&gt;&lt;LF&gt;
  subject: test mail
  
  test mail body
  
  .
  #=&gt; 250 2.0.0 =k: queued as B6F08480D12
  quit
  #=&gt; 221 2.0.0 Bye
  #=&gt; Connection closed by foreign host.
  </code></pre>