I think Net::SMTP is used as a general mail sender much more often than it should. People are usually using it on Windows because there's no /usr/sbin/sendmail or to gain Windows compatibility for their code even if sendmail(1) is available.

By using Net::SMTP instead of MIME::Lite, Mail::Sendmail or Mail::Sender we usually lose:

flexibility when specifying email addresses
Net::SMTP recipients() method (or to() alias) accepts only plain emails. You just cannot take a To: line from a mail template and pass it to Net::SMTP, because Net::SMTP will choke on $smtp->to("sales@domain.com, check@domain.com") or $smtp->to("Sales <sales@domain.com>"). You people usually don't want to parse RFC822 email headers, do you?

ability to send complex emails (MIME)
Net::SMTP only allows you to send raw data. If you have enough time to do MIME by hands, I admire and envy you. But if you use MIME::Entity or MIME::Lite, why even bother with Net::SMTP? These modules send their results to the Internet just perfectly (using Net::SMTP internally if you specify SMTP delivery).

ability to send mail in SMTP-challenged environments
These includes not only dialup systems but also uucp-connected (there're some). In spite of the fact that most of the time such systems have a local smtp-server running which will handle queueing and the like, there's a case which hit me just recently.

My hosting provider runs cgi-scripts inside a sandboxed environment without ip-connectivity to Internet. (And that's a good security practice for all free hostings out there). And the provider allows sending mails only via sendmail(1) just to be able to impose restrictions on sending mail.

Even if these facts may seem of little importance to the majority of Perl programmers out there, Net::SMTP gives practically no advantages over other mail-sending modules, which are usually capable of using either SMTP connection or sendmail(1). The only advantage I see is sending several mails via a single connection for speed. In this case your (very specialized) program is more of an SMTP client than a mail-sending entity. And that's the very case Net::SMTP is perfect to serve, but such cases are very rare (are you a bulk sender?).

And it's sad for me to see such posts as Net::SMTP v local MTA or SMTP Modules that are both Win32 and Unix compliant when people intentionally choose Net::SMTP over other alternatives due to Windows lacking sendmail(1).

Update: I just found the relevant item in perlfaq9. It confirms my strong negative feelings about too wide-spread use of Net::SMTP.

Replies are listed 'Best First'.
Re: Net::SMTP shortcomings if used as a general mail sending module
by lhoward (Vicar) on May 31, 2002 at 16:52 UTC
    I agree with you that many people use Net::SMTP when the should be using other modules that are more well suited to quickly "building and sending" an email. However, there are many situations where using Net::SMTP as part of an overall mail solution where you need/want more control and more percise error handling over the SMTP exchange, or for the performance reasons mentioned above.

    I frequently use Net:SMTP in conjunction with a module like MIME::Lite, using MIME::Lite to build the body of the message and Net::SMTP to inject it into an email system. Sure, I could use MIME::Lite to send the email via SMTP myself, but I'd loose control of the error handling of the SMTP exchange (for instance, failing over to a backup mailserver easily if my primary mailserver is down).

    I don't see any problem with having to separate out each address individually when dealing with Net::SMTP. I normally have the addresses in an array before, so its very easy to do a

    foreach(@recipient){ $smtp->to($_); }
Re: Net::SMTP shortcomings if used as a general mail sending module
by moodster (Hermit) on May 31, 2002 at 17:20 UTC
    Well, as I see it, Net::SMTP is exactly what the name implies: an easy-to use client for the SMTP protocol. Nothing more, nothing less. It's pretty lowlevel stuff, and if I was writing an application requiring robust mail handling, I'd probably go with a module that can guarantee that a mail will eventually reach its destination and won't choke on bad recipient addresses, just as I wouldn't use Assembler to do regular expression handling. On the other hand, if I was writing my own mail transport module it's possible that I'd base it on Net::SMTP. It's just a question of chosing the right tools for the job.

    However, Net::SMTP has a (albeit small) edge on the modules you mentioned: it's included in the base distribution, meaning easier script installation for uneducated admins.


      Is it really in the base? I don't see it in 5.6.1 nor in older 5.00503.
      Update: At least, not in original source tarballs from CPAN. But it is in the base (along with all the libnet) of 5.8.0-RC1.
        Well, both my Win2k ActiveState perl and the perl that's installed on my Debian system has Net::SMTP, and I can't recall having installed it separatly. Of course, the default module list varies between distributions, so it's possible it's been left out in some.


Re: Net::SMTP shortcomings if used as a general mail sending module
by samtregar (Abbot) on Jun 01, 2002 at 07:41 UTC
    I only use Net::SMTP. I have one simple reason: it works and when it doesn't (rarely) it's very easy to debug. This is because it has a simple elegent design and a careful implementation. What's not to like?

    Your points don't strike me as particularly damning. First, I've never had any problem writing programs that send email and require a simple email address for the destination. What's a mail template anyway?

    Second, I've used MIME::Lite with Net::SMTP and they work together just fine. Maybe MIME::Lite could have done it all internally but I never bothered to find out. It worked, and that's all she wrote.

    Your final point is the most absurd - what kind of bass-ackwards organization can't maintain an SMTP server? Your nutty ISP could just as well maintain their own SMTP server and program it for super-security.


      Wish you faced any real problems before choosing your favourite mail-sending module. The fact that you never heard of mail templates means you will need them in the near future :)

      And about provider, I'll say that there's no easy way to control access to SMTP based on local user names. Imagine that you want to let alex send 10 mails a day and to prevent sergey from sending any mails at all.

      I could probably follow your step and add that any organization that doesn't provide my scripts with a working /usr/sbin/sendmail is also bass-ackwards and nutty, but that's no use. That's life, and Net::SMTP fails in my case, while all the other modules work like a charm in both cases.

      Mentioned MIME::Lite, btw, has a perfect SMTP-sending abilities implemented via Net::SMTP!

        Heh. I suppose you wouldn't consider CGI::Application::MailPage a real problem. During the development of that module I tried a number of mail sending modules. They were all too complicated and when they failed it was difficult to tell why. When I settled on Net::SMTP my life became abruptly much simpler and my module started to work reliably.


Re: Net::SMTP shortcomings if used as a general mail sending module
by Aristotle (Chancellor) on Jun 01, 2002 at 15:48 UTC
    I haven't worked with it so I don't know for a fact, but I'd imagine that even if you want performance for mass mailing, you can sidestep Net::SMTP due to the Mail::Bulkmail module.

    Makeshifts last the longest.