in reply to eof without closing the pipe

I know Im going to get flamed for this becuase Im not using a module, but its quick and it works, plus that its quicker becuase it doesnt even open up a pipe..It uses a simple socket connection to a sendmail server (just one)
#!/usr/bin/perl use strict; use Socket; my @users=('email1@server.com', 'email2@server.com', 'email3@server.co +m'); my $mailhost="localhost"; my $myemail="mark\@cidera.com"; # Your email address # this is just the subject and body of the message # you can get this information from anywhere I just # put it in here for the sake of example my $subject= "hi"; my $data = "hi hi hi hi there"; my $userz; socket(MAIL,PF_INET,SOCK_STREAM,getprotobyname('tcp')); connect(MAIL,sockaddr_in(25,inet_aton($mailhost))); select(MAIL); $|=1; select('stdout'); print MAIL "HELO blah.com\n"; foreach $userz (<@users>) { print MAIL "MAIL FROM: $myemail\n"; print MAIL "RCPT TO: $userz\n"; print MAIL "DATA\n"; print MAIL "Subject: $subject\n"; print MAIL "$data\n"; print MAIL ".\n"; } print MAIL "quit\n"; close(MAIL);
This works becuase it doesnt quit the sendmail connection till after all the emails have been sent to each person in your @users array seperatly.

I know this isnt EXACTLY what you were looking for, but its just another way to do this.

Replies are listed 'Best First'.
RE: RE: eof without closing the pipe
by c-era (Curate) on Jun 30, 2000 at 15:47 UTC
    It's nice to see someone else who uses socket (not socket.pm) and knows a protocol or two.
RE: RE: eof without closing the pipe
by eduardo (Curate) on Jun 30, 2000 at 17:05 UTC
    now, maybe i am mistaken... however, opening up a socket connection, no matter how simple, should be MARKEDLY slower than opening up a pipe, no matter what the situation. If you are opening up a socket to a host you have to deal with:
    • TCP/IP overhead
    • Network Transport Layer Overhead
    • Communicational Latency
    • The fact that you are communicating via external means, instead of the bus!
    however, if you are opening up a pipe, to a process on the local machine then the situation is:
    • there is NO TCP/IP overhead
    • there is NO network layer overhead
    • the communicational latency is the velocity at which the receiving process can aquire STDIN
    • and you are communicating through memory and the bus, which have a MUCH greater bandwith and throughput than most any network connection!
    if speed is your constraint, and you are attempting to maximize the throughput (here being messages per minute) I can not conceive of a way faster than communicating directly with sendmail through a pipe.

    UPDATE: thanks to lhoward i have now learned that in modern operating systems, much to my suprise, the TCP subsystem has been optimized WAY better than I ever thought it would be, so, my entire coment above, should be disreguarded and i should be flogged for not having benchmarked in the first place :) keep the learning coming...

      Opening a socket is typically as fast or faster than opening a pipe.

      Pipe issues:

      • Have the overhead of starting a separate process. (you may or may not have this overhead when doing a TCP socket connection depending on if the server is forking or threading)
      socket/TCP port issues:
      • Most OSes have highly optimized TCP/IP stacks, generally much more optmized than the OSes core pipe processing functions. For today's applications network performance is typically much more important than pipe performance.
      • localhost TCP/IP connections (where client and server are on the same host) do not go out "on the wire", they are typically highly optimized with direct memory-to-memory exchange of the data. Localhost network connections have practially 0 network overhead.
        i stand absolutely corrected. i guess i am showing the fact that some of the things i learned are no longer quite as accurate as they were when i picked them up :) so, i wrote a bit of code to test this out:
        #!/usr/bin/perl -w use strict; use IO::Socket; use Benchmark; my $to_mail_addy = "junk\@????????.net"; my $from_mail_addy = "junk\@????????.net"; my $mailhost = "localhost"; sub with_pipe { open(SENDMAIL, "|/usr/sbin/sendmail") or die "cannot open sendmail" +; print SENDMAIL "To: $to_mail_addy\n"; print SENDMAIL "From: $from_mail_addy\n"; print SENDMAIL "Subject: A speed test...\n"; print SENDMAIL "Content-type: text/plain \n\n"; print SENDMAIL "Blah blah blah... this is my content..."; close(SENDMAIL); } sub with_socket { my $sock = new IO::Socket::INET(PeerAddr => 'localhost', PeerPort => 25, Proto => 'tcp'); die "unable to create $sock" unless defined $sock; print $sock "Hello arino.net\n"; print $sock "MAIL FROM: $from_mail_addy\n"; print $sock "RCPT TO: $to_mail_addy\n"; print $sock "DATA\n"; print $sock "Subject: A speed test...\n"; print $sock "Blah blah blah... this is my content..."; print $sock ".\n"; print $sock "quit\n"; close($sock); } timethese (1000, {"WITH PIPES" => \&with_pipe, "WITH SOCKET" => \&with_socket }); [ed@darkness ed]$ perl speedtest.pl Benchmark: timing 1000 iterations of WITH PIPES, WITH SOCKET... WITH PIPES: 22 wallclock secs ( 0.26 usr 0.68 sys + 6.95 cusr 4.93 +csys = 12.82 CPU) @ 1063.83/s (n=1000) WITH SOCKET: 17 wallclock secs ( 1.76 usr + 1.13 sys = 2.89 CPU) @ 3 +46.02/s (n=1000)
        so we see that sockets ARE much faster than pipes, which to me was counter intuitive... I guess they really have optimized the bejeezus out of the TCP stack... time to have a little chat with my networking professor :)

        UPDATE: out of curiosity, people have asked what the system specs were like on the machine i ran the tests on:
        Processor Pentium II (Deschutes) 348.491034 Mhz
        RAM Memtotal - 193060 kB
        MemFree - 20864 kB
        OS Linux 2.2.13 i686 (redhat)

RE: RE: eof without closing the pipe
by t0mas (Priest) on Jun 30, 2000 at 10:18 UTC
    I will not flame you. Looks like a nice way to do the job. There is not many who speak "natively" with sendmail nowdays ;)

    /brother t0mas