perlquestion
astrobal
<p>I crave the wisdom of the Perl Monks</p>
<p>I am trying write a cgi program which sends an email to a customer with the invoice (in pdf format) attached. My previous experiences in sending emails has reduced me to using Net::SMTP as the only module that works - but I am open to enlightenment!</p>
<p>The script below has been cribbed from previous questions on this topic, and the attachment of a binary file came from and example answer posted back in 2008. And it works - sort of. That is, the text part of the email comes through fine, but the ascii encoded binary file is just tagged on to the text part of the email rather than appearing as an attachment with it paperclip, as it should. I give the whole script below and then what is received as an email:</p>
<p>Thanks for your consideration.</p>
<code>
#!/usr/bin/perl
print "Content-type: text/html\n\n";
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use warnings;
use Net::SMTP;
use MIME::Base64;
my ($buf, $picture);
my $company = 'my_company.com';
my $path = "/home/sites/$company/public_html";
my $attachBinaryFile = "image.jpg";
my $boundary = 'frontier';
my $passwd = "password";
my $contact = "name";
my $email = "info\@$company";
$smtp = Net::SMTP->new("mail.$company", Timeout => 30,Debug => 0,);
$smtp->datasend("AUTH LOGIN\n");
$smtp->response();
$smtp->datasend(encode_base64("$contact\@$company") );
$smtp->response();
$smtp->datasend(encode_base64("$passwd") );
$smtp->response();
$smtp->mail("$contact\@$company");
$smtp->to($email);
$smtp->cc();
$smtp->data();
$smtp->datasend("From: $contact\@$company\n");
$smtp->datasend("To: $email\n");
$smtp->datasend("Cc: info\@$company\n");
$smtp->datasend("Subject: To see if this will come through\n");
$smtp->datasend("\n");
$smtp->datasend("
This is some text.
");
$smtp->datasend("--$boundary\n");
$smtp->datasend("Content-Type: image/jpeg; name=\"$attachBinaryFile\"\n");
$smtp->datasend("Content-Transfer-Encoding: base64\n");
$smtp->datasend("Content-Disposition: attachment; filename=\"$attachBinaryFile\"\n");
$smtp->datasend("\n");
open(DAT, "$path/$attachBinaryFile") || die("Could not open binary file!");
binmode(DAT);
local $/=undef;
while (read(DAT, $picture, 4096)) {
$buf = &encode_base64( $picture );
$smtp->datasend($buf);
}
close(DAT);
$smtp->datasend("\n");
$smtp->datasend("--$boundary\n");
$smtp->dataend();
$smtp->quit;
print "Mail sent\n";
exit;
print "</body></html>";
</code>
<p>And here is what I get at the other end.</p>
<code>
This is some text.
--frontier
Content-Type: image/jpeg; name="image.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="image.jpg"
AAABAAEAICAAAAEAIACoEAAAFgAAACgAAAAgAAAAQAAAAAEAIAAAAAAAABAAABILAAASCwAAAAAA
AAAAAAD4//L/9v7+//j7///6////+v/v////8////v7///39//L/9P/l/+7/6P/3//r+/////P//
///5///69f/7//////b////3//////n/7//r/+j/+P/k////7/z////6////+f////P////1////</code>
8<----------snip
<code>
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAA==
--frontier
</code>