http://www.perlmonks.org?node_id=1086009

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

I'm need to send multiple attachments in an email but getting errors Bad or missing From address:

I have tried to fix these errors by setting mailcfg{from} address but then I get error No recipient!

I know sendmail is working as echo "test from sendmail" | /usr/sbin/sendmail myEmail@gmail.com from a terminal window sends an email

Email address is false as I don't want it displayed here, but it is a gmail account. Full code below

#!/usr/bin/perl use strict; use warnings; use Email::MIME; use Mail::Sendmail; #$Mail::Sendmail::mailcfg{'from'} = 'myEmail@gmail.com'; #$Mail::Sendmail::mailcfg{'recipient'} = 'myEmail@gmail.com'; # echo "test from sendmail" | /usr/sbin/sendmail myEmail@gmail.com my %attachments = ( 'doc_name' => 'contact_test.xls', 'doc_path' => '/home/docs/', 'doc_mime_type' => 'application/vnd.ms-exce +l', 'doc_template_name' => 'spreadsheet'); my @parts; push @parts, Email::MIME->create( attributes => { filename => $attachments{doc_path}.$attachments{doc_nam +e}, content_type => $attachments{doc_mime_type}, disposition => 'attachment', encoding => 'base64', name => $attachments{doc_name} } ); push @parts, Email::MIME->create( attributes => { content_type => 'text/plain', disposition => 'attachment', encoding => 'quoted-printable', charset => 'US-ASCII' }, body_str => 'this is the body' ); my $email = Email::MIME->create( header_str => [ From => 'myEmail@gmail.com', To => 'myEmail@g +mail.com', Subject => 'this is my subject' ], parts => [ @parts ] ); print $email->as_string; print "end of mail as_string\n"; my $response = sendmail($email); print "Response = $response\n"; print "error1 $!\n"; print "error2 $Mail::Sendmail::error\n"; print "Log\n", $Mail::Sendmail::log;

Output from running the script below

From: myEmail@gmail.com To: myEmail@gmail.com Subject: this is my subject Date: Wed, 14 May 2014 11:56:45 +0100 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="1400065005.D7DeeC3a0.15978"; +charset="us-ascii" --1400065005.D7DeeC3a0.15978 Date: Wed, 14 May 2014 11:56:45 +0100 MIME-Version: 1.0 Content-Type: application/vnd.ms-excel; name="contact_test.xls" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="/home/docs/contact_test.xls +" --1400065005.D7DeeC3a0.15978 Date: Wed, 14 May 2014 11:56:45 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment this is the body= --1400065005.D7DeeC3a0.15978-- end of mail as_string Response = 0 error1 Bad file descriptor error2 Bad or missing From address: '' Log Mail::Sendmail v. 0.79_16 - Wed May 14 11:56:45 2014

Any help is appreciated

Replies are listed 'Best First'.
Re: Sending email with attachments using Email::Mime
by McA (Priest) on May 14, 2014 at 12:11 UTC

    Hi,

    one of your problems is the line

    my $response = sendmail($email);

    This doesn't fit to the API of the function sendmail. Have a look at the docs of Mail::Sendmail. What you put in is a Email::MIME-object which doesn't work this way.

    Regards
    McA

Re: Sending email with attachments using Email::Mime
by Theodore (Friar) on May 14, 2014 at 11:45 UTC
    Try to use

    use Email::MIME::Creator;

    instead of

    use Email::MIME;

    (not tested, just had a look at the module's page).

      Hi,

      may I add Email::Stuffer.

      Is a wrapper around Email::MIME from the same author and for common cases very easy.

      Regards
      McA

      (Recommendation based on own usage ;-) )

        Thanks McA. I did read the message on cpan within Email::Mime to use Email::Stuffer. The problem was I could not install Stuffer. I have now managed to install Stuffer now and got it working. So much easier.

Re: Sending email with attachments using Email::Mime
by cord-bin (Friar) on May 14, 2014 at 15:06 UTC
    You need just a small change to your code, I tested on my machine and it works. You should change these lines and install Email::Sender::Simple module. I know the documentation of Email::MIME is not very clear on how to send actually the email. I use Email::Sender::Simple and works very fine, don't use the Mail::Sendmail. Here's the code:
    #use Mail::Sendmail; print $email->as_string; print "end of mail as_string\n"; use Email::Sender::Simple qw(sendmail); my $response = sendmail($email); print "Response = $response\n"; print "error1 $!\n";
    Cheers!

      Thanks for your reply. I installed Email::Sender::Simple and changed the code as you suggested. A lot closer now. I am receiving the attachments but they are 0 file size so they cant open or save the documents from the email. I am also receiving the error `error1 Illegal seek` . Should not make any difference but I am using Ubuntu. Any ideas.

        Was curious to see what I might find about this: Not a great deal, but thought this was interesting in case you were using postfix:

        Postfix Illegal seek. Probably way outside of what your experincing... but then sometimes long shots pay off.

        Best of luck...

        ...the majority is always wrong, and always the last to know about it...
        Insanity: Doing the same thing over and over again and expecting different results...
        Ok, we are almost there, you need again a module and a very light change. I tried not to change your code too much just to get it work on my machine.
        You need to change, actually add one line to this part:
        use IO::All; # install this module first push @parts, Email::MIME->create( attributes => { filename => $attachments{doc_name}, content_type => $attachments{doc_mime_type}, disposition => 'attachment', encoding => 'base64', name => $attachments{doc_name} }, body => io($attachments{doc_name})->all, # add this line );
        For the illegal seek error, just delete these lines, you don't need them:
        #print "error1 $!\n"; #print "error2 $Mail::Sendmail::error\n";
        Cheers, have fun!
Re: Sending email with attachments using Email::Mime
by mr_mischief (Monsignor) on May 14, 2014 at 18:26 UTC
    Try this line (untested).
    my $response = sendmail( 'From' => 'myEmail@gmail.com', 'To' => 'myEm +ail@gmail.com', 'Message' => $email->as_string );