An smtp client supporting tls and auth
Version 0.12
use Net::SMTP::TLS; my $mailer = new Net::SMTP::TLS( 'your.mail.host', Hello => 'some.host.name', Port => 25, #redundant User => 'emailguy', Password=> 's3cr3t'); $mailer->mail('[email protected]'); $mailer->to('[email protected]'); $mailer->data; $mailer->datasend("Sent thru TLS!"); $mailer->dataend; $mailer->quit;
Net::SMTP::TLS is a \s-1TLS\s0 and \s-1AUTH\s0 capable \s-1SMTP\s0 client which offers an interface that users will find familiar from Net::SMTP. Net::SMTP::TLS implements a subset of the methods provided by that module, but certainly not (yet) a complete mirror image of that \s-1API\s0.
The methods supported by Net::SMTP::TLS are used in the above example. Though self explanatory for the most part, please see the perldoc for Net::SMTP if you are unclear.
The differences in the methods provided are as follows:
The mail method does not take the options list taken by Net::SMTP The to method also does not take options, and is the only method available to set the recipient (unlike the many synonyms provided by Net::SMTP). The constructor takes a limited number of Net::SMTP's parameters. The constructor for Net::SMTP::TLS takes the following (in addition to the hostname of the mail server, which must be the first parameter and is not explicitly named):
NoTLS - In the unlikely event that you need to use this class to perform non-TLS \s-1SMTP\s0 (you ought to be using Net::SMTP itself for that...), this will turn off \s-1TLS\s0 when supplied with a true value. This will most often cause an error related to authentication when used on a server that requires \s-1TLS\s0 Hello - hostname used in the \s-1EHLO\s0 command Port - port to connect to the \s-1SMTP\s0 service (defaults to 25) Timeout - Timeout for inital socket connection (defaults to 5, passed directly to IO::Socket::INET) User - username for \s-1SMTP\s0 \s-1AUTH\s0 Password - password for \s-1SMTP\s0 \s-1AUTH\s0
During construction of an Net::SMTP::TLS instance, the full login process will occur. This involves first sending \s-1EHLO\s0 to the server, then initiating a \s-1TLS\s0 session through \s-1STARTTLS\s0. Once this is complete, the module will attempt to login using the credentials supplied by the constructor, if such credentials have been supplied.
The \s-1AUTH\s0 method will depend on the features returned by the server after the \s-1EHLO\s0 command. Based on that, \s-1CRAM-MD5\s0 will be used if available, followed by \s-1LOGIN\s0, followed by \s-1PLAIN\s0. Please note that \s-1LOGIN\s0 is the only method of authentication that has been tested. \s-1CRAM-MD5\s0 and \s-1PLAIN\s0 login functionality was taken directly from the script mentioned in the acknowledgements section, however, I have not tested them personally.
This module will croak in the event of an \s-1SMTP\s0 error. Should you wish to handle this gracefully in your application, you may wrap your mail transmission in an eval {} block and check $@ afterward.
This code was blatantly plagiarized from Michal Ludvig's smtp-client.pl script. See <http://www.logix.cz/michal/devel/smtp> for his excellent work.
Alexander Christian Westholm, awestholm at verizon dawt net
Improvements courtesy of Tomek Zielinski