# Net::SMTP and Unknown User

**URL:** <https://www.ruby-forum.com/t/net-smtp-and-unknown-user/145294>\
**Category:** Ruby\
**Created:** [August 7, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/net-smtp-and-unknown-user/145294 "2008-08-07T16:46:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [August 7, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/net-smtp-and-unknown-user/145294/1 "2008-08-07T16:46:03Z")

</div>

Hi,

Situation: I’ve got a program that automatically sends out emails to  
large numbers of people in certain situations.

Problem: Sometimes people leave the company and their managers aren’t  
always good about updating things. Since Net::SMTP#send\_message seems  
to be an “all or nothing” method (i.e. either an email is sent to  
everyone or no email is sent to anyone), I’m not sure what to do about  
this situation.

Things I’d like to avoid if possible:

LDAP: I’d rather not do an ldap lookup on every email address every  
time an email is sent out. It’s slow, and seems like there ought to be  
an easier solution.

Manual Parsing: I _could_ parse the offending email out of the error  
message, delete it from the array of email addresses and retry. But  
that feels clunky and is probably error prone.

Suggestions?

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [August 7, 2008, 5:11pm UTC](https://www.ruby-forum.com/t/net-smtp-and-unknown-user/145294/2 "2008-08-07T17:11:12Z")

</div>

On Thu, Aug 7, 2008 at 10:43 PM, Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Problem: Sometimes people leave the company and their managers aren’t  
> always good about updating things. Since Net::SMTP#send\_message seems  
> to be an “all or nothing” method (i.e. either an email is sent to  
> everyone or no email is sent to anyone), I’m not sure what to do about  
> this situation.

forgive me if i dont get you right, but why don’t you send/route the  
mails to a real mail server and/or a mailing list server? A mail  
server has builtin routing/retry/timeout mechnisms and  
understands/implems the full smtp protocol.

kind regards -botp

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [August 8, 2008, 6:01pm UTC](https://www.ruby-forum.com/t/net-smtp-and-unknown-user/145294/3 "2008-08-08T18:01:58Z")

</div>

On Aug 7, 9:08 am, botp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> understands/implems the full smtp protocol.  
> I’m afraid I don’t follow. Perhaps a bit of sample code will help:

require ‘net/smtp’

mhost = ‘[mailgate.work.com](http://mailgate.work.com)’  
from = ‘removed\_email\_address@domain.invalid’

# First email is valid, second one is not

to = [‘removed\_email\_address@domain.invalid’, ‘removed\_email\_address@domain.invalid’]

# Raises a Net::SMTPFatalError

Net::SMTP.start(mhost, 25){ |smtp|  
smtp.send\_message(“hello”, from, to)  
}

If this is the wrong approach, I’m all ears.

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [August 9, 2008, 6:38am UTC](https://www.ruby-forum.com/t/net-smtp-and-unknown-user/145294/4 "2008-08-09T06:38:57Z")

</div>

From: Daniel B. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
#DB: require ‘net/smtp’  
#DB: mhost = ‘[mailgate.work.com](http://mailgate.work.com)’  
#DB: from = ‘removed\_email\_address@domain.invalid’  
#DB: # First email is valid, second one is not  
#DB: to = [‘removed\_email\_address@domain.invalid’, ‘removed\_email\_address@domain.invalid’]  
#DB:  
#DB: # Raises a Net::SMTPFatalError

NO, it should not (unless you reached the limit of the server’s max  
recipient setting). what are the details of the error?

#DB: Net::SMTP.start(mhost, 25){ |smtp|  
#DB: smtp.send\_message(“hello”, from, to)  
#DB: }

below is the actual code i used, adapted fr your code. i just modified  
the params to work in my case and put some debugging so i know where in  
the smtp session a certain error occurred just in case. i also show an  
actual run. result is that i got one email for the test and another  
bounced email for the dummy foo.bar address.

C:\family\ruby\mail\>cat daniel.rb  
require ‘rubygems’  
require ‘net/smtp’

smtp\_server = ‘[bugomail.delmonte-phil.com](http://bugomail.delmonte-phil.com)’  
smtp\_port = 25  
from = ‘removed\_email\_address@domain.invalid’

# First email is valid, second one is not

to = [‘removed\_email\_address@domain.invalid’, ‘removed\_email\_address@domain.invalid’]

# Raises a Net::SMTPFatalError

smtp = Net::SMTP.new(smtp\_server, smtp\_port)  
smtp.set\_debug\_output $stderr # use for debugging; outputs mail  
sessions  
smtp.start { |smtp|  
smtp.send\_message(“this is a test”, from, to)  
}

C:\family\ruby\mail\>ruby daniel.rb  
Connection opened: [bugomail.delmonte-phil.com:25](http://bugomail.delmonte-phil.com:25)  
 → “220 [BUGOMAIL.delmonte-phil.com](http://BUGOMAIL.delmonte-phil.com) Microsoft ESMTP MAIL Service,  
Version: 6.0.37  
90.3959 ready at Sat, 9 Aug 2008 12:24:50 +0800 \r\n”  
 ← “EHLO localhost.localdomain\r\n”  
 → “[250-BUGOMAIL.delmonte-phil.com](http://250-BUGOMAIL.delmonte-phil.com) Hello [10.2.10.123]\r\n”  
 → “250-TURN\r\n”  
 → “250-SIZE\r\n”  
 → “250-ETRN\r\n”  
 → “250-PIPELINING\r\n”  
 → “250-DSN\r\n”  
 → “250-ENHANCEDSTATUSCODES\r\n”  
 → “250-8bitmime\r\n”  
 → “250-BINARYMIME\r\n”  
 → “250-CHUNKING\r\n”  
 → “250-VRFY\r\n”  
 → “250-X-EXPS GSSAPI NTLM LOGIN\r\n”  
 → “250-X-EXPS=LOGIN\r\n”  
 → “250-AUTH GSSAPI NTLM LOGIN\r\n”  
 → “250-AUTH=LOGIN\r\n”  
 → “250-X-LINK2STATE\r\n”  
 → “250-XEXCH50\r\n”  
 → “250 OK\r\n”  
 ← “MAIL FROM:[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\r\n”  
 → “250 2.1.0 removed\_email\_address@domain.invalid OK\r\n”  
 ← “RCPT TO:[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\r\n”  
 → “250 2.1.5 removed\_email\_address@domain.invalid \r\n”  
 ← “RCPT TO:[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)\r\n”  
 → “250 2.1.5 removed\_email\_address@domain.invalid \r\n”  
 ← “DATA\r\n”  
 → “354 Start mail input; end with .\r\n”  
writing message from String  
wrote 19 bytes  
 → “250 2.6.0 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Queued ma  
il for delivery\r\n”  
 ← “QUIT\r\n”  
 → “221 2.0.0 [BUGOMAIL.delmonte-phil.com](http://BUGOMAIL.delmonte-phil.com) Service closing transmission  
channel\r  
n”

C:\family\ruby\mail\>

#DB: If this is the wrong approach, I’m all ears.

that approach is fine for small number of recipients. generally, mail  
servers limit the number of recipients of a single mail.

So, for a big number (we do not care how many),

I. use a loop

Net::SMTP.start(mhost, 25){ |smtp|  
to.each do |recipient|  
smtp.send\_message(“hello”, from, recipient)  
end  
end

II. or send to a mail\_group address to the mail server configured w the  
aliases, ie mail\_group =\> removed\_email\_address@domain.invalid, removed\_email\_address@domain.invalid, …

III. or send to a mail\_group address to a mail server capable of routing  
it to a mailing list server (eg majordomo). this is the ideal setup for  
big lists.

kind regards -botp
