Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Mime::Lite and SMTP authentication?

by ikaruga (Novice)
on Jan 25, 2010 at 01:26 UTC ( [id://819376]=perlquestion: print w/replies, xml ) Need Help??

ikaruga has asked for the wisdom of the Perl Monks concerning the following question:

Hi all. I am trying to send an HTML email using Mime::Lite. However, I use SMTP and my email provider requires me to use authentication. Taking a look at the Mime::Lite docs, it's not really clear. Can someone help me out? This is the code I have so far. (OK, so I'm using MIME::Lite::TT::HTML, but this uses Mime::Lite for send.)
#!/usr/bin/perl use strict; use warnings; use MIME::Lite::TT::HTML; my %params; my %options; $options{INCLUDE_PATH} = 'c:/Documents and Settings/Uriel/My Documents +/Sandbox/perl/'; my $msg = MIME::Lite::TT::HTML->new( From => 'admin@example.com', To => 'test@yahoo.com', Subject => 'Hello world', Template => { text => 'test.txt', html => 'test.html', }, TmplOptions => \%options, TmplParams => \%params, ); $msg->send('smtp', 'smtp.mail.yahoo.com', Timeout => 60 );

Replies are listed 'Best First'.
Re: Mime::Lite and SMTP authentication?
by Corion (Patriarch) on Oct 27, 2019 at 18:12 UTC

    Just for posteriority, MIME::Lite 3.031 now supports SSL directly:

    MIME::Lite->send('smtp','some.host', SSL => 1, Port => 465 );
Re: Mime::Lite and SMTP authentication?
by pkaluski (Initiate) on Aug 30, 2018 at 09:13 UTC

    Hello,

    I have just finished my struggle (successful :))with MIME::Lite and SSL. I was using ActivePerl on Windows. Here are my observations:

    1. Make sure that you have the most recent version of perl and all libraries - in most cases new versions of security related libraries are issued in order to fix bugs and compensate detected weaknesses, not to provide new bells and whistles. Therefore, backward compatibility is not the highest priority. Thus, if you have installed perl 3-4 years ago, uninstall it and get the newest version from ActiveState (or whatever distro you use)

    2. MIME::Lite as it is right now DOES NOT support SSL. Or at least there is no clear simple way to make it work. With all that said, it IS POSSIBLE to make it work, but you have to modify the library code. But the hack is simple: In the MIME/Lite.pm file find a definition of @_net_smtp_opts table and make sure that it looks like this:

    my @_net_smtp_opts = qw( Hello LocalAddr LocalPort Timeout AuthUser AuthPass SSL Port ExactAddresses Debug );

    Explanation: MIME::Lite uses Net::SMTP for SMTP protocol. However, for some reason it does not pass all arguments of MIME::Lite->send* method to Net::SMTP->new. It filters them by @_net_smtp_opts table. Only parameters which are in this table are being passed to Net::SMTP constructor. Originally SSL, AuthPass, AuthUser are not there. That's why they never make it to Net::SMTP module.

Re: Mime::Lite and SMTP authentication?
by ikaruga (Novice) on Jan 25, 2010 at 01:46 UTC
    Looks like I spoke to soon. I found this:
    MIME::List->send('smtp', $host, AuthUser=>$user, AuthPass=>$pass, Port +=>465);
    So I used this form of the send function with the correct settings for my email provider. However, I then got a "Bad file descriptor error". Turning on Debug=>1 in the send call, it says that
    Unexpected EOF on command channel at c:/Perl/site/lib/MIME/Lite.pm li +ne 2865
    Ideas?

      If your providers server listens on port 465 (as indicated in the code snippet above), it almost certainly expects an SSL connection (465 is the universally accepted port for SMTPS,i.e. SMTP over SSL). AFAIK you can't use SSL in conjunction with the MIME::Lite send() method (or rather, you probably could but it's more trouble than it is worth), so you'll need to use something like Net::SMTP::SSL for sending. You can still use MIME::Lite for constructing the message. For example (untested):

      #!/usr/bin/perl use strict; use warnings; use MIME::Lite::TT::HTML; use Net::SMTP::SSL; my %params; my %options; $options{INCLUDE_PATH} = 'c:/Documents and Settings/Uriel/My Documents +/Sandbox/perl/'; my $msg = MIME::Lite::TT::HTML->new( From => 'admin@example.com', To => 'test@yahoo.com', Subject => 'Hello world', Template => { text => 'test.txt', html => 'test.html', }, TmplOptions => \%options, TmplParams => \%params, ); my $smtp; $smtp = Net::SMTP::SSL->new($host, Port=>465) or die "Can't connect"; $smtp->auth($user, $pass) or die "Can't authenticate:".$smtp->message( +); $smtp->mail('admin@example.com') or die "Error:".$smtp->message(); $smtp->to('test@yahoo.com') or die "Error:".$smtp->message(); $smtp->data() or die "Error:".$smtp->message(); $smtp->datasend($msg->as_string) or die "Error:".$smtp->message(); $smtp->dataend() or die "Error:".$smtp->message(); $smtp->quit() or die "Error:".$smtp->message();

      All dogma is stupid.
Re: Mime::Lite and SMTP authentication?
by Krambambuli (Curate) on Jan 25, 2010 at 08:28 UTC
    Maybe you just cannot access the file you want, being 'inside' the sandbox and referring it with a complete path?

    You could have a look at and around line 2865 in your MIME::Lite, to get a first clou: is the problem SMTP related or has it something to do with what should happen _before_ the message is ready to be sent?

    Just an idea.


    Krambambuli
    ---

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://819376]
Approved by biohisham
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 22:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found