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 = Array.new
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

MESSAGE_END

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

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 = File.read(‘text_par.html’)

class My_FileNames
def getFilenames
FileUtils.cd(‘TEMPTEST’)
Dir.glob(’*.txt’).each do |f|
$combobox.appendItem(f)
end
end

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

def sendEmails
$emailName = $mailAddtext.text

$emailName. each do |name|
begin
  $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
end
end

end
end

theApp = FXApp.new

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

groupBxL = FXGroupBox.new(theMainWindow, “Left”,
FRAME_RIDGE|LAYOUT_FIX_X|LAYOUT_FIX_Y|LAYOUT_FIX_WIDTH|LAYOUT_FIX_HEIGHT,
:x=>0, :y=>0, :width=>175, :height=>200)
groupBxR = FXGroupBox.new(theMainWindow, “Right”,
FRAME_RIDGE|LAYOUT_FIX_X|LAYOUT_FIX_Y|LAYOUT_FIX_WIDTH|LAYOUT_FIX_HEIGHT,
:x=>180, :y=>0, :width=>155, :height=>200)

$mailAddtext = FXText.new(groupBxL, nil, 0,
FRAME_SUNKEN|FRAME_THICK|LAYOUT_FIX_HEIGHT|JUSTIFY_LEFT|LAYOUT_FILL_COLUMN|LAYOUT_FIX_WIDTH,
:height=>175, :width=>150)

$combobox = FXComboBox.new(groupBxR,20, nil, 0,
COMBOBOX_INSERT_LAST|FRAME_SUNKEN|FRAME_THICK)
My_FileNames.new.getFilenames
$combobox.connect(SEL_COMMAND){My_FileNames.new.getFileContent}

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

sendMailButton = FXButton.new(groupBxR, “&Send
Emails”,:opts=>BUTTON_NORMAL|LAYOUT_FIX_X|LAYOUT_FIX_Y,:x=>45,:y=>46)
sendMailButton.tipText = “Send To These Emails”
sendMailButton.connect(SEL_COMMAND) {My_FileNames.new.sendEmails}

FXToolTip.new(theApp)

theApp.create

theMainWindow.show

theApp.run

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 = Array.new
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”
end
end

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

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

yoyoyo

MESSAGE_END

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

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
in…
- 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)
@loginfo.info(‘setup email settings NOW…’)
@server = server
@address = address
end

 # this to send just an email
 #

def send_email(from, to, subject, msg)
@loginfo.info(‘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}")
end
#
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)
@loginfo.info(‘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
 @table.tables

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

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