using use strict and then skipping it for a small code scrap is A TRAP. And this one got both of us.
Re^3 actually _is_ the answer you were looking for. Let's rephrase it a bit, and I'll use your original scrap for speed's sake:
diff -u
--- x 2009-09-29 21:00:01.392812366 +0200
+++ perl.scrap.mime-lite-mail-sending 2009-09-29 17:56:51.900791614
+ +0200
@@ -1,23 +1,27 @@
+use MIME::Lite;
+$html="<b>testhtml</b>";
+$text="testtext";
my $message = MIME::Lite->build (
From =>'someemail',
- To =>'anotheremail',
# a present from peter to postfix :)
+ To =>'jakobi',
Subject=>'somesubject',
Type =>'multipart/alternative'
);
# this is the if(1) note
if ('i have a attachment') {
$message->replace(Type => 'multipart/mixed');
$message->attach(
Type=> 'AUTO',
# this is your major issue: **Path being undefined**
- Path=> $upload_file,
+ Path=> '/etc/hosts',
Filename=>$basename,
Disposition => 'attachment'
);
}
# this might be a prob or might be no prob;
# but usually it's text/plain in emails
- $message->attach(Type=> 'TEXT',
+ $message->attach(Type=> 'text/plain', # TEXT
Data=> $text,
);
$message->attach(Type=> 'text/html',
Data=> $html
);
$message->send('smtp','000.000.000.000',
Debug=>1,Timeout=>60,Port=>26);
Now why did you run in trouble with your scrap from Re^4: To me it looks like ::Lite HATES to be invoked without
a Type. Resulting in an excess dummy mime part with undef'd data (this is a new, different bug apart from our initial issue), which isn't silently suppressed by send (finally triggering the same abort as before). Which IMHO should be considered a bug in Mime::Lite.
Note the use of Data::Dumper for debugging (or use perl -d and suitable breakpoints, but I'm too lazy for that)
--- y.orig 2009-09-29 21:11:50.544939894 +0200
+++ y 2009-09-29 21:31:44.072812281 +0200
@@ -4,27 +4,49 @@
# ---------
use strict;
use warnings;
+use vars;
use diagnostics;
+use Data::Dumper;
use MIME::Lite;
use Email::Valid;
# If we have a valid email address.
- my $to_email = 'jakobi';
- my $email_valid = Email::Valid->new(mxcheck => 1,tldcheck
+ => 1);
- if ($email_valid->address($to_email)) {
+ my $to_email = 'jakobi@anuurn.compact';
+ #my $email_valid = Email::Valid->new(mxcheck => 1,tldcheck
+ => 1);
+ #if ($email_valid->address($to_email)) {
+ if (1){
+
+# data dumper always shows an extra undef entry ???
+# bless( {
+# 'SubAttrs' => {},
+# 'Parts' => [],
+# 'FH' => undef,
+# 'Attrs' => {
+# 'content-disposition' => 'inline',
+# 'content-type' => 'text/plain',
+# 'content-length' => undef,
+# 'content-transfer-encoding' => 'binary'
+# },
+# 'Path' => undef,
+# 'Data' => undef,
+# 'Header' => []
+# }, 'MIME::Lite' ),
+# and undef's seem to kill the send later on.
+# to avoid this, **SET THE TYPE in ->build**!
+
+
# Everything looks good, make up our message
my $message = MIME::Lite->build (
From => $to_email,
To => $to_email,
Subject => 'This is the subject',
- #Type => 'multipart/alternative' If I default to th
+is then it never gets updated with a attachment.
+ Type => 'multipart/alternative' # If I default to t
+his then it never gets updated with a attachment.
);
# Did we get a attachment?
- my $attachment = '/tmp/38293132401.pdf';
+ my $attachment = '/etc/hosts';
+
if (-e $attachment) {
#$message->replace(Type => ''); # Tried this
- #$message->replace(Type => 'multipart/mixed'); # Tr
+ied this
- $message->add(Type => 'multipart/mixed');
+ $message->replace(Type => 'multipart/mixed'); # Tri
+ed this
+ #$message->add(Type => 'multipart/mixed');
$message->attach(
Type => 'AUTO',
Path => $attachment,
@@ -32,16 +54,22 @@
Disposition => 'attachment'
);
} else {
- $message->add(Type => 'multipart/alternative');
+ # $message->add(Type => 'multipart/alternative');
}
+
my $text_email = 'yadda yadda yadda';
my $html_email = '<em>yadda</em> <strong>yadda</strong> <u>ya
+dda</u>';
# Do up the message
- $message->attach(Type => 'TEXT',
+ $message->attach(Type => 'text/plain', # TEXT, but seems i
+nnocent and translates to text/plain
Data => $text_email
);
$message->attach(Type => 'text/html',
Data => $html_email
);
- $message->send('smtp','0.0.0.0',Debug=>1,Timeout=>60,Port=>26
+);
-
+warn "attach $attachment\n";
+warn "attach $attachment exists\n" if -e $attachment;
+print Dumper($message);
+ my $rc=$message->send('smtp','0.0.0.0',Debug=>1,Timeout=>60,P
+ort=>26);
+ # not reached, as we die due to lack of eval{} around send an
+yway!?
+ die "send returned not true" if not $rc;
+}