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

Hi monks,

I've been trying to send a sample email using below sample script and receiving an error,

Code:

#!/usr/bin/perl use strict; use warnings; use plmconst ':constants'; use lib "C:\\Strawberry\\perl\\lib"; use lib "C:\\Strawberry\\perl\\site\\lib"; use lib "C:\\Strawberry\\perl\\vendor\\lib\\Email"; use lib "C:\\Strawberry\\perl\\vendor\\lib"; use lib "C:\\Users\\magesh\\SF\\Personal Folders\\Material master\\Mat +erial_Master_Prod\\Test"; use lib "C:\\Strawberry\\perl\\c\\bin"; use plm_sap_material_master_constant_definitions ':constants'; # first, create your message use Email::MIME::Creator; use Email::Sender::Success; use Email::Sender::Transport::SMTP qw(); use Email::Sender::Role::CommonSending; use Email::MIME::Header::AddressList; use Email::Sender::Transport; use Email::Sender; use Email::MIME; use Throwable; use Throwable::Error; use StackTrace::Auto; use IO::All; my $message = Email::MIME->create( header_str => [ From => SENDER_EMAIL, To => 'mzkuma@abc.com', Subject => 'Happy birthday!', ], attributes => { encoding => 'quoted-printable', charset => 'ISO-8859-1', }, body_str => "Happy birthday to you!\n", ); my $mime_entity = $message->cast('Email::MIME'); # send the message use Email::Sender::Simple qw(sendmail); sendmail($mime_entity,{ from => SENDER_EMAIL, transport => Email::Sender::Transport::SMTP->new ({ host => MAIL_SERVER, port => MAIL_SERVER_PORT, }) });

Error: "Don't know how to handle Email::MIME at C:\Users\AADDAA~1\AppData\Local\Temp\11 par-616164646161646d696e\cache-59feac640e183270594e4311795b30444ad958c9\inc\lib Email/Sender/Role/CommonSending.pm line 78." Can you please help me to understand what I'm missing? Thanks a lot. BR, Magesh

Replies are listed 'Best First'.
Re: How to send email using Email::MIME & Email::Sender::Simple?
by haukex (Archbishop) on May 03, 2020 at 15:42 UTC

    The following code, which is distilled from your example, works fine for me. Just a guess, perhaps you have an old version of one of the modules in one of your many use lib directories? Also, since you seem to be using PAR (I think?), have you tried running the script outside of that?

    use warnings; use strict; use Email::MIME; use Email::Sender::Simple 'sendmail'; use Email::Sender::Transport::SMTP; my $message = Email::MIME->create( header_str => [ From => 'test@example.com', To => $ENV{USER}.'@localhost', Subject => 'Happy birthday!', ], attributes => { encoding => 'quoted-printable', charset => 'ISO-8859-1', }, body_str => "Happy birthday to you!\n", ); sendmail($message, { from => 'test@example.com', transport => Email::Sender::Transport::SMTP->new( { host => 'localhost', port => 25 }), });

      Yes I'm using PAR indeed, below is the command I'm using to pack the script into executable. The script is running fine as .pl & .exe in my local system. However when I try to distribute the same exe to a Win Server machine, it fails with the mentioned error.

      pp -u -o test.exe test.pl

      Also, I did try using Email::Stuffer, but it is again in turn invoking Email::Sender::Simple , which gives me the same error. Perhaps the way I'm packing the script to executable is wrong?

        Although I'm not an expert on PAR, I still suspect it has something to do with all those use lib dirs. I suggest you inspect all of the directories listed in @INC when the program runs (e.g. print "<<$_>>\n" for @INC;) for which versions of Email::Sender etc. are installed there. Also, I would suggest just removing all of the use lib dirs from the script, as I don't see a good reason why all the C:\Strawberry ones are there in the first place (and "C:\\Strawberry\\perl\\vendor\\lib\\Email" is almost certainly wrong), and just working with Perl's defaults - that's what cpanm will use as well.

      Yes I'm using PAR indeed, below is the code I'm using to pack the script into executable. The script is running fine as .pl & .exe in my local system. However when I try to distribute the same exe to a Win Server machine, it fails with the mentioned error.

      pp -u -o test.exe test.pl

      Also, I did try using Email::Stuffer, but it is again in turn invoking Email::Sender::Simple , which gives me the same error. Perhaps the way I'm packing the script to executable is wrong?

Re: How to send email using Email::MIME & Email::Sender::Simple?
by haukex (Archbishop) on May 03, 2020 at 14:20 UTC

    Your code looks pretty much like what the documentation of Email::Sender says to do. I am confused however about the line my $mime_entity = $message->cast('Email::MIME');, where did you get that from, and what happens when you remove that line? Also, note how the Email::MIME documentation starts with:

    Wait! Before you read this, maybe you just need Email::Stuffer, which is a much easier-to-use tool for building simple email messages that might have attachments or both plain text and HTML.

    Update: Also, have you made sure you have the newest versions of all of those modules?

      I did make sure to do cpanm on all the modules in @INC and it said they're up to date. Please ignore the cast line that you've highlighted, that was added by me to try troubleshooting. The error however doesn't change with/without that line. Thanks for pointing Email::Stuffer, I will have a look.

      However, there's an email notification system already running, which is configured same way as in the sample provided. The monk who coded that has left the firm and so I'm learning perl. Not really sure if I will have time rewire the system with Stuffer. Let me see.

      Thanks!

Re: How to send email using Email::MIME & Email::Sender::Simple?
by Magkumar (Novice) on May 03, 2020 at 17:49 UTC

    After multiple tests, I can confirm that the issue happens only after packing the script into an exe. I used the below commands to pack the perl into exe. Can anyone help please?

    Updated code:
    #!/usr/bin/perl use strict; use warnings; use lib "C:\\Users\\magesh\\SF\\Personal Folders\\Material master\\Mat +erial_Master_Prod\\Test"; use plmconst ':constants'; use plm_sap_material_master_constant_definitions ':constants'; use Email::Stuffer; use Email::Sender::Transport::SMTP; use Email::MIME (); use Email::MIME::Creator (); use Email::Sender::Simple (); use Email::Sender::Role::CommonSending; use Email::Sender; use Email::MIME::Header::AddressList; use Throwable; use Throwable::Error; use StackTrace::Auto; use IO::All; my $msg_body .= "Hello this is test email"; Email::Stuffer ->html_body($msg_body) ->subject('Hello') ->from(SENDER_EMAIL) ->to('mzkuma@abc.com') ->transport(Email::Sender::Transport::SMTP->new ({ host => MAIL_SERVER, port => MAIL_SERVER_PORT, })) ->send;

    To pack the above test.pl into exe, used below command

     pp -o test.exe test.pl

    Also tried

     pp -u -o test.exe test.pl

    Any leads would greatly help me. Thank you!

      See my suggestions here: if you could show @INC for the script running from the command line and the script running from the exe, that might give some clues. Try also adding print $Email::Sender::VERSION; and seeing what that outputs, whether the versions are different. And also search the @INC paths in the filesystem for multiple installed copies of the corresponding Email:: modules, and their versions, and let us know.

        Hi Haukex,

        Thanks for your suggestion. I did verify the module versions of .pl vs .exe. But here's what I found and which resolved my issue, I packed the perl using following pp command

         pp -x -o test.exe test.pl

        -x in pp executed my perl script completely and identified all runtime dependencies on the fly, then gave a compatible exe. This resolved my problem.

        Thanks a lot again.

Re: How to send email using Email::MIME & Email::Sender::Simple?
by Magkumar (Novice) on May 03, 2020 at 14:19 UTC

    Update on my question:

    I have removed the below line from the code, that was added by me to troubleshoot the error.

    my $mime_entity = $message->cast('Email::MIME');

    No change in error with/ without above line in code. I'm using Strawberry Perl 5.20.3