1

I have a legacy app that needs to change. it is written in perl and uses send::mail to send mails to users. Previously we sent links in the email message body but now they want pdf attachments instead. The PDF's are generated on another server using php.

the workflow would be

  1. create the email body
  2. get the pdf from another server via a URL
  3. add the pdf as an attachment to the email
  4. send it.

I think I can use

use LWP::Simple;
unless (defined ($content = get $URL)) {
    die "could not get $URL\n";
}

to get the contents of the URL but I can't figure out how to use that var as an attachment in sendmail. current sendmail code is:

my %maildata = (To  => $to,
        From    => 'OurSite - Billing <billing@ourSite.com>',
        Organization => 'OurSite, LLC      http://www.OurSite.com/',
        Bcc => 'sent-billing@ourSite.com',
        Subject => $subject{$message} || 'ourSite invoice',
        Message => $body
           );
print STDERR "notify1 now calling sendmail\n";
sendmail(%maildata) || print STDERR $Mail::Sendmail::error;

The other issue I have is I don't know how to find out if the version of sendmail I have (old freebsd system) is even capable of sending attachments ?

AnFi
  • 10,006
  • 3
  • 21
  • 43
MB.
  • 669
  • 9
  • 26
  • If you can get your code to generate a valid MIME message, Sendmail versions older than yourself will happily deliver it. – tripleee Sep 02 '20 at 17:28
  • I'm 52 are you sure about that - LOL – MB. Sep 02 '20 at 22:39
  • Then you have some margin (-: but anyway, if your mail server is able to interoperate with other mail servers at all, it will handle any valid SMTP message just fine, and not know or care what its contents are (barring size restrictions; some mail servers are configured to not allow messages larger than 50 MB for example). – tripleee Sep 03 '20 at 04:30

1 Answers1

1

ok thanks for the posters who gave me some direction / the will to give it a go.

In the end I built the mime body doing the following

use LWP::Simple;
use MIME::Base64;

unless (defined ($content = get $URL)) {
    die "could not get $URL\n";
} 
my $pdfencoded = encode_base64($content);  

my %maildata = (To  => $to,
        From    => 'OurSite - Billing <billing@ourSite.com>',
        Organization => 'OurSite, LLC      http://www.OurSite.com/',
        Bcc => 'sent-billing@ourSite.com',
        Subject => $subject{$message} || 'ourSite invoice',

           );

my $boundary = "====" . time() . "====";

$maildata{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
$maildata{'Message'} = "--".$boundary."\n"."Content-Type: text/plain\n".$body.
"\n--".$boundary."\nContent-Transfer-Encoding: base64\nContent-Type: 
application/pdf; name=\"invoice.pdf\"\n".$pdfencoded."\n--".$boundary."--";

sendmail(%maildata) || print STDERR $Mail::Sendmail::error;

This gave me a hand built MIME format for the body content.

Thanks for the help !

MB.
  • 669
  • 9
  • 26