Hi, I've managed to get backups working for what I'm doing :D and now need to send them by email once done.
In searching around I noticed several recommendations to use MIME Lite instead of Sendmail (never used MIME Lite before). This code, evidently, does that - but I need to make sure it's okay and how to send more than one attachment.
use MIME::Lite;
$msg = MIME::Lite->new (
From => "someone\@somedomeain.com (Someone)",
To => "receiver\@anotherdomain.com",
Subject => "Subject Title",
Type => "multipart/mixed"
);
# message
$msg->attach(Type => "TEXT", Data => "Short message here.");
Would multiple attachments work like this? Or do I have to do 3 seperate blocks (one for each attachment)?
# Attachment (can I send more than one with message?)
$msg->attach(Type => "application/zip",
Path => "/server/path/to/zipname.zip, /server/path/again/zipname2.zip, /server/path/another/zipname3.zip",
Filename => "zipname.zip, zipname2.zip, zipname3.zip",
Dispostion => "attachment");
$text = $msg->as_string;
$msg->send;
Any help appreciated.
sohguanh
08-01-2010, 09:40 PM
The thing about CPAN Perl is there are just too many Open Source modules. Besides reading the reviews (which can be biased), the best way is to test each module one by one.
I prefer CPAN modules to have as few dependencies as possible so it is easy to install. In fact I prefer CPAN modules to only depend on what is in the core Perl and that's it.
For email sending, I explored Mail::Sender and it is quite easy to use and support a lot of methods I want even though it is not updated for quite some time.
Attached is my testing script. You may want to play around to decide if you want this module over MIME::Lite (which is no longer Lite with too much dependencies).
#!/usr/bin/perl -w
use strict;
use warnings;
use threads;
use threads::shared;
use Mail::Sender;
my $sender = Mail::Sender->new({smtp=>'localhost',encoding=>"Quoted-printable"}) or die "Can't create the Mail::Sender object: $Mail::Sender::Error\n";
#usually it is ->new, ->Open, ->SendEnc ->SendLineEnc or ->GetHandle.... finally ->Close
# or ->new, ->OpenMultipart, ->Body or ->Part ->EndPart, ->Attach... finally ->Close
#print $sender->QueryAuthProtocols('localhost');
sub testTextShortCut {
#simple text message email - MailMsg is a shortcut method!
if ($sender->MailMsg({
from => 'abc@abc.com',
to =>'def@abc.com',
subject => 'this is a test using shortcut method',
msg => "Hi Guan Hoe.\nHello World can see ?"
}) < 0) {
die "$Mail::Sender::Error\n";
}
}
sub testTextAttachShortcut {
#simple text message with attachment email - MailFile is a shortcut method!
if ($sender->MailFile({
from => 'abc@abc.com',
to =>'def@abc.com',
subject => 'this is a test with attachments using shortcut methods',
msg => "Hi Guan Hoe.\nI'm sending you the attachments you wanted.",
file => 'abc.pl,def.pl'
}) < 0) {
die "$Mail::Sender::Error\n";
}
}
sub testTextTraditional {
#simple text message email - traditional way
$sender->Open({
from => 'abc@abc.com',
to =>'def@abc.com',
subject => 'this is a test using traditional method'}) or die "$Mail::Sender::Error\n";
$sender->SendLineEnc("Hi Guan Hoe.\nHello World can see ?");
$sender->Close() or die "$Mail::Sender::Error\n";
}
sub testTextAttachTraditional {
#simple text message with attachment email - traditional way!
$sender->OpenMultipart({
from => 'abc@abc.com',
to =>'def@abc.com',
subject => 'this is a test with attachments using traditional method'}
) or die "$Mail::Sender::Error\n";
$sender->Body({ msg => <<'*END*' });
Hi Guan Hoe.
I'm sending you the attachments you wanted.
*END*
$sender->Part({ctype => "multipart/mixed",
encoding => 'base64',
disposition => 'attachment'
});
$sender->Attach(
{ disposition=>'attachment; filename="abc.pl"', file=>'abc.pl', description=>'Perl Abc'
}
); #filename is for display on email client. actual filename can be different
$sender->Attach(
{ disposition=>'attachment; filename="def.pl"', file=>'def.pl', description=>'Perl Def'
}
);
$sender->EndPart("multipart/mixed");
$sender->Close()or die "$Mail::Sender::Error\n";
}
sub testHTMLTraditional {
#simple html message email - traditional way
$sender->Open({
from => 'abc@abc.com',
to =>'def@abc.com',
ctype => 'text/html',
encoding => 'quoted-printable',
subject => 'this is a test html using traditional method'}) or die "$Mail::Sender::Error\n";
my $line = qx[cat <<'EOF';
<html>
<body>
<title>Hello World</title>
<h1>Hi Guan Hoe.</h1>
<p>
Can see me ?<br>
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
</body>
</html>
EOF
];
$sender->SendLineEnc($line);
$sender->Close() or die "$Mail::Sender::Error\n";
}
sub testHTMLAttachTraditional {
#simple HTML message with attachment email - traditional way!
$sender->OpenMultipart({
from => 'abc@abc.com',
to =>'def@abc.com',
subject => 'this is a test html with attachments using traditional method'}
) or die "$Mail::Sender::Error\n";
sub testHTMLInlineImageTraditional {
#simple HTML message with inline images email - traditional way!
$sender->OpenMultipart({
from => 'abc@abc.com',
to =>'def@abc.com',
boundary =>'boundary-test-1',
subject => 'this is a test html with inline images using traditional method'}
) or die "$Mail::Sender::Error\n";
$sender->EndPart("multipart/related");
$sender->Close()or die "$Mail::Sender::Error\n";
}
my $thrDesc = "Thread ".threads->self()->tid()." ";
print $thrDesc."started... ",qx(date),"\n";
testTextShortCut;
testTextAttachShortcut;
testTextTraditional;
testTextAttachTraditional;
testHTMLTraditional;
testHTMLAttachTraditional;
testHTMLInlineImageTraditional;
print "Mail sent OK.\n";
print $thrDesc."exited... ",qx(date),"\n";
edatz
08-02-2010, 03:09 AM
Thanks, I'll have a look at it.
Mail::Sender is not on my server and they are EXtremely fussy about mail modules (because of spam problems in the past), so woujldn't be able to use it on a live test. I might be able to use it on my local testbed (Indigo Perl on Windows machine), if there's a version available to work with it.
The "tester" concept is a good idea. That's something could be a real help when developing.
sohguanh
08-04-2010, 03:28 AM
The "tester" concept is a good idea. That's something could be a real help when developing.
If you develop in Java, you will notice I copy shamelessly from the JUnit testing framework. I label all the function with prefix test<FunctionName> so it is easier for me to visualize and in future if I need to have automated unit testing I can do it.
I know JUnit concept is now available in CPAN Perl but I have this concept before that module was available.
edatz
08-04-2010, 05:26 AM
When I test stuff, it's always on my PC with a local testbed server (Indigo Perl). If it doesn't work there it doesn't go live.
If I'm setting up smething that get's mailed out (usually SendMail), I write a temporary sub in the script that will come up on screen with everything the email would contain. Saves me a ton of hassle.
I'll probably use SendMail for this one as I know how to use it and have decided any backed up folders will get emailed one at a time - instead of me trying to be fancy :)
One I get my friend's site all working, there are some bits that I've going make so they can used as stand alone items (or added to an exting CMS) and have one or two on my site as free downloads. One of them will be the backup facility.
edatz
08-06-2010, 06:11 AM
I got zipped files being emailed. I ended up using the Mime Lite way as SendMail just didn't like what I was doing. Works fine, except for one real problem.
When the file is unzipped, it contains all the folders (empty) that are in the pathway to the folder that is being backed up.
Does anyone now how to stop that in Archive Zip?
This is the code I'm using:
sub doBakup {
$obj = Archive::Zip->new();
foreach $file (@files) {$obj->addFile($file);}
if ($obj->writeToFileNamed("$fvara/fvarb/$fvarc/$zipname.zip") != AZ_OK) {
print qq ~Error cannot create backup!~;
} else {
print qq ~The Folder <b>($zipname)</b> has been archived.~;
}
webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved.