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

Hi, I tried to use this simple script to send mail to my gmail account.
use MIME::Lite $user = "user"; $pass = "password"; MIME::Lite->send("smtp","smtp.gmail.com", AuthUser=>$user, AuthPass=>$pass); my $msg = MIME::Lite->new ( From => "user@gmail.com", To => "user@gmail.com", Data => "This is a test\n", Subject => "Test", ); $msg->send()
I keep getting a "Bad File descriptor error at the $msg->send() line. Can anyone tell me whats wrong with the code. Thanks.

Replies are listed 'Best First'.
Re: Sending Mail from Perl Script
by skx (Parson) on Nov 19, 2007 at 06:21 UTC

    The "@" character is special inside quotes, and this is the source of your problem. Either:

    a) Escape the symbol:

    my $msg = MIME::Lite->new ( From => "user\@gmail.com", To => "user\@gmail.com", Data => "This is a test\n", Subject => "Test", );

    or b) Use single-quotes to avoid the interpolation:

    my $msg = MIME::Lite->new ( From => 'user@gmail.com', To => 'user@gmail.com', Data => "This is a test\n", Subject => "Test", );
    Steve
    --
      Ok Thanks. That was a dumb error. Corrected that problem but I fail to be able to connect to the mail server. Do you know what else would be wrong with this code?
      use MIME::Lite; $user = 'user@yahoo.com'; $pass = 'passwd'; # Configure smtp server - required one time only MIME::Lite->send("smtp","smtp.mail.yahoo.com", AuthUser=>$user, AuthPass=>$pass); my $msg = MIME::Lite->new ( From => 'user@yahoo.com', To => 'user@yahoo.com', Data => "A simple test message", Subject => "Hello World", ); $msg->send();
      Hi Steve, Thanks. By replacing double codes with single am able to send mails to gmail account.
Re: Sending Mail from Perl Script
by thezip (Vicar) on Nov 20, 2007 at 01:45 UTC
    • One thing that is definitely wrong is that you are not using strict nor warnings! All bets are off until you do this...

    • Try using the SMTP send method and setting the Debug=>1 flag. This could give you some additional information to work with (NOTE: this code is mostly right out of the MIME::Lite perldocs),
      use strict; use warnings; use MIME::Lite; ### Create a new multipart message: my $msg = MIME::Lite->new( From =>'me@myhost.com', To =>'you@yourhost.com', Cc =>'some@other.com, some@more.com', Subject =>'A message with 2 parts...', Type =>'multipart/mixed' ); ### use Net:SMTP to do the sending $msg->send('smtp','some.host', Debug=>1 );

    Where do you want *them* to go today?
      I submitted this awhile ago and haven't been able to get back to it now. I modified my script to be like this...
      use strict; use warnings; use MIME::Lite; my $user = 'james@gmail.com'; my $pass = 'james12'; my $msg = MIME::Lite->new( From =>'James', To =>'james@hotmail.com', CC =>'', Subject =>'yah it worked', Type =>'Hello world' ); MIME::Lite->send('smtp','smtp.gmail.com:465', AuthUser=>$user, AuthPass=>$pass);
      Yet, I still can't connect correctly. Everything compiles ok but the email doesn't get sent at all. Can anyone tell me why?

        Have you tried enabling Debug as I suggested? What were the resulting messages?

        This might be a natural next step in the troubleshooting process.


        Your wish is my commandline.