no name no name - 2 months ago 6
Ruby Question

Return key sends the email in ruby , how to make it jump to a new line instead of sending the mail

so i have this script which helps me send an email however when i type a message and hit enter it sends the mail , how can i make it possible so it jumps to a new line and only sends the mail if specific word has been entered

require 'mail'
require 'colorize'
options = { :address => "smtp.gmail.com",
:port => 587,
:user_name => 'mymail@gmail.com',
:password => 'mypass',
:authentication => 'plain',
:enable_starttls_auto => true }
ail.defaults do
delivery_method :smtp, options
end

Mail.deliver do
to 'test@gmail.com'

from 'mymail@gmail.com'


subject 'hello'
puts "[!] IMPORTANT type 'SND' in full uppercase then PRESS ENTER to send the Email".yellow
puts"Enter Your Message Below".colorize(:light_blue)
print "Message > ".colorize(:light_blue) ##entering message body here
body gets ##sends the mail as soon as i hit enter

end

Answer

This should do:

Mail.deliver do
  to 'test@gmail.com'
  from 'mymail@gmail.com'
  subject 'hello'

  puts "[!] IMPORTANT type 'SND' in full uppercase then PRESS ENTER to     send the Email".yellow
  puts"Enter Your Message Below".colorize(:light_blue)
  print "Message > ".colorize(:light_blue) ##entering message body here

  the_body = ""
  loop do
    line = gets
    break if line.include? "SND"
    the_body << line
  end

  body the_body
end