net/smtp Multiple Email recipients Issue

J

Joseph Clark

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 = "(e-mail address removed)"
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
 
J

Joseph Clark

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 = "(e-mail address removed)"
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
 
D

Dave Lilley

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

SMTP problem sending to multiple recipients 0
net/smtp 6
SMTP SMS Reminder Script 5
net/smtp question 6
Email/SMTP::NET Problems 24
net/smtp 3
smtp check response 500 6
How to Send to Multiple People with Java.Mail 2

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top