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

Hi there,

I am having issues with the format of the emails sent via MIME::Lite.

I am outputting two items of data per line. The first column of data is not fixed length, and I want the second item of data to be correctly aligned in the same position each time so I am using sprintf to format the data.

The print command output illustrates that the data is been outputted correctly but when I receive my email the data is all over the place.

What I did notice in my eclipse Perl environment that the string is padded out exactly like it outputs in my email, yet the print statement shows the data correctly aligned!!!

Any help on why the formatting is all over the place would be appreciated. The mailer is doing its job correctly and working with the data given. The problem seems to be with how sprintf holds the data internally.
use MIME::Lite; $smtp = "mailserver"; $internal_email_address = 'myemailaddess'; $a = sprintf ("%-90s %-s\n", "the amount for Apple is ","34"); $b = sprintf ("%-90s %-s\n", "the amount for Lemons is", "7"); print $a; print $b; $c = $a.$b; mailer( $internal_email_address,"issue", $c); sub mailer { my ( $addr, $subj, $output ) = @_; print "$_\n" for $addr; print "$_\n" for $subj; print "$_\n" for $output; $msg = MIME::Lite->new( From => 'xxxx', To => $addr, Subject => $subj, Data => $output ); MIME::Lite->send( 'smtp', $smtp, Timeout => 60 ); eval { $msg->send }; $mailerror = "Status: ERROR email - MIME::Lite->send failed: $@\n" + if $@; if ( $mailerror eq '' ) { $status = "Status: Mail sent\n"; } else { $status = $mailerror; } }
  • Comment on sprintf format and print inconsistencies whilst creating fixed width column
  • Download Code

Replies are listed 'Best First'.
Re: sprintf format and print inconsistencies whilst creating fixed width column
by roboticus (Chancellor) on Dec 19, 2013 at 19:45 UTC

    newbieperlperson:

    Are you by any chance using a proportional font in your mail reader? If so, that may be the cause of the ragged formatting.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      I am using a regular font, the sprintf spacing represents exactly what is output to email, i.e. its all over the place, oddly, the print statement shows that the spacing is correct.

        So, if I take the email output, throw it into a notepad document, the doc perfectly aligns as expected.

        It turned out that the font was causing this after all, a few users suggested it and I had looked at the wrong part of outlook for the font I was using for emails received. I needed to use a non proportional font, this link helped. http://stackoverflow.com/questions/12588021/perl-formatted-output-to-email-body

Re: MIME::Lite formatting issues
by toolic (Bishop) on Dec 19, 2013 at 18:57 UTC
    Maybe the "90" is causing problems. Can you decrease it? At least for debugging, try something like:
    $a = sprintf ("%-30s %-s\n", "the amount for Apple is ",34); $b = sprintf ("%-30s %-s\n", "the amount for Lemons is", 7);
      Thanks for responding, I already tried reducing the length to 7 and the formatting is still messed up.
Re: MIME::Lite formatting issues
by karlgoethebier (Abbot) on Dec 19, 2013 at 19:00 UTC

    I've been using MIME::Lite for a while and liked it very much - but please wait.

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      Hi there, I am limited to what libraries I can use and MIME::Lite is the one I need to use.