Net/smtp Multiple Email recipients Issue

I am having an interesting issue. I just recently modified a script from
sending to a single address to multiple. The emails are sent, but when
you look at the To field in the email clients it is all messed up. For
instance when I attempt a reply to all it only thinks there is one
address that is a combination of the two I sent to.

require ‘net/smtp’

toEmail =
toEmail << “1NAME <[email protected]>”
toEmail << “2NAME <[email protected]>”

fromEmail = “[email protected]
fromEmailAlias = “MEMEME”
emailSubject = “Subject”

message = <<MESSAGE_END
From: #{fromEmailAlias} <#{fromEmail}>
To: #{toEmail}
MIME-Version: 1.0
Content-type: text/html
Subject: #{emailSubject}

Email test


Net::SMTP.start(‘’, 25, ‘’, ‘username’,
‘password’, :plain) do |smtp|
smtp.send_message message, fromEmail, toEmail

Here, this is something that I have been working on…You may be able to
modify it to get you in the right direction.
I us FX…nice but you can take it all away and use something else…I
have tried to modularize it as best as I could. ALSO, be careful
because with this one I don’t have server activity checks and if you
send too many Email it clogs your server.

If someone else has something better, please send it up so that I can
learn a little more too.

require ‘fox16’
require ‘fileutils’
require ‘find’
require ‘net/smtp’

include Fox

$emailName = Array::new

#$et =‘text_par.html’)

class My_FileNames
def getFilenames‘TEMPTEST’)
Dir.glob(’*.txt’).each do |f|

def getFileContent
$mailAddtext.text = ‘’
IO.foreach($combobox.text, ‘\n’) do |line|
$mailAddtext.text = “#{line}\n”

def sendEmails
$emailName = $mailAddtext.text

$emailName. each do |name|
  $address = name.scan(/.*?[^\n]$/).flatten

  puts $address

#  Net::SMTP.start('MAIL SERVER HERE', 25, 

‘localhost’,‘USERNAME’,‘PASSWORD’, :plain) do |smtp|
# smtp.open_message_stream(‘FROM EMAIL ADDRESS’, $address) do |f|
# f.puts ‘From: FROM EMAIL ADDRESS’
# f.puts “To: #{$address}”
# f.puts “MIME-Version: 1.0”
# f.puts “Content-type: text/html”
# f.puts ‘Subject: SUBJECT GOES HERE’
# f.puts
# f.puts “#{$eT}”
# end
# end

  #puts message
  #p $address

rescue Exception => e
  puts "Could not send to #{$address} - #{e.message}\n"
  #puts e.backtrace.inspect


theApp =

theMainWindow =, “Email App”)
theMainWindow.position(50,50, 350,200)

groupBxL =, “Left”,
:x=>0, :y=>0, :width=>175, :height=>200)
groupBxR =, “Right”,
:x=>180, :y=>0, :width=>155, :height=>200)

$mailAddtext =, nil, 0,
:height=>175, :width=>150)

$combobox =,20, nil, 0,

theButton =, “&Exit”)
theButton.tipText = “Exit Program”
theButton.connect(SEL_COMMAND) { exit }

sendMailButton =, “&Send
sendMailButton.tipText = “Send To These Emails”
sendMailButton.connect(SEL_COMMAND) {}


I figured it out on my end. Below is what I had to do to get the To:
field to work. You basically put each individual in a sperate To: Line.
For example

To: John [email protected]
To: Rob [email protected]

I still send the array to to net.smtp send_message, but in the message I
scripted the above. I did notice you need to make sure there is no empty
line between the last To: and MIME in the version below.

require ‘net/smtp’

toEmail =
toEmail << “John <[email protected]>”
toEmail << “Rob <[email protected]>”

rcptList = “”
x = 0
while x < toEmail.length
rcptList << “To: #{toEmail[x]}”
x += 1
if x < toEmail.length
rcptList << “\n”

fromEmail = “[email protected]
fromEmailAlias = “Ruby Emailing”
emailSubject = “Tada”

message = <<MESSAGE_END
From: #{fromEmailAlias} <#{fromEmail}>
MIME-Version: 1.0
Content-type: text/html
Subject: #{emailSubject}



Net::SMTP.start(‘’, 25, ‘’,
‘username’, ‘password’, :plain) do |smtp|
smtp.send_message message, fromEmail, toEmail

May I suggest you use Pony?

It is an added dependency but it makes creating emails and emails with
attachments much easier.

I have below a program I’ve made that is very modular in that you pass
- a email server address
- a senders email address

from this you can call one of 2 methods to dispatch emails.
- Emails with no attachments
- Emails with attachments

I know these could be put into 1 but i’ve not got round to doing that.

For what it’s worth the code is below…

require ‘pony’

module Sendemail

# call this 1st to setup the email server and senders
# settings

def initial(server,address)‘setup email settings NOW…’)
@server = server
@address = address

 # this to send just an email

def send_email(from, to, subject, msg)‘sending an email - No Attachment sent !’)

 Pony.mail(:to => "#{to}", :from => "#{from}", :subject => 

“#{subject}”, :body => “#{msg}”, :via => :smtp, :via_options => {
:address => @server,
:port => @address})

 @logbugs.debug("\nserver = #{@server}\nport used = #{@address}\nto 

= #{to}\nfrom = #from}\nsubject = #{subject}\nmsg = #{msg}")
this to send with an attachment
# Know the method and this one could be merged into one
# I just haven’t got round to it.
def email_attach(from, to, subject, msg,attachment,file_path)‘sending an email - Attachment sent !’)

  # these 2 lines are so I can create a PDF via Prawn and add it as
  # the email - disregard these 2 lines
 @table.pdf_setup filename,path,msg

 Pony.mail(:to => "#{to}", :from => "#{from}", :subject => 

“#{subject}”, :body => “#{msg}”, :via => :smtp, :via_options => {
:address => @server,
:port => @address},
:attachments => {"#{attachment}" =>"#{file_path}")})
@logbugs.debug("\nserver = #{@server}\nport used = #{@address}\nto
= #{to}\nfrom = #from}\nsubject = #{subject}\n
msg = #{msg}\nfilename = #{file_path}")