Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
06-28-2012, 06:26 PM
|
#1
|
LQ Newbie
Registered: Jun 2012
Posts: 4
Rep:
|
How do I send mail with attachment using Perl?
My server does not have MIME:Lite. I am using smtp in Perl trying to send zip file attachment.
Here is my code: <code>
$smtp->mail("abc\@company1.com");
$smtp->recipient("xyz\@company2.com");
$smtp->data();
$smtp->datasend("$Subj\n");
$smtp->datasend("MIME-Version: 1.0\n");
$smtp->datasend("Content-type: multipart/mixed;\n\tboundary=\"$boundary\"\n");
$smtp->datasend("\n--$boundary\n");
$smtp->datasend("Content-type: text/plain\n");
$smtp->datasend("Content-Disposition: quoted-printable\n");
$smtp->datasend("\nSee attached zip file.\n\n\n");
$smtp->datasend("--$boundary\n");
$smtp->datasend("Content-Transfer-Encoding: base64 \n");
$smtp->datasend("Content-Type: application/zip; name=\"$attachFile\"\n");
$smtp->datasend("Content-Disposition: attachment; filename=\"$attachFile\"\n");
$smtp->datasend("\n");
$smtp->datasend("$data_file\n");
$smtp->datasend("--$boundary--\n");
$smtp->dataend();
$smtp->quit;
</code>
It seems to work at the server end, but the attachment received is not a valid zip file. What do I need to do to prepare the $data_file? Do I need to uuencode before sending? What would be the simplest way to get it done?
|
|
|
06-28-2012, 07:28 PM
|
#2
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415
|
1. if you don't mind using/calling shell, then at work we use mutt to send attachments http://www.shelldorado.com/articles/...tachments.html
2. I tried what you are doing in Perl a while ago, but the code content is gone, so I probably found a better way.
Possibly this for a text rpt I generated; don't know if it will work with zip, but it maybe worth a try
Code:
# Send report inline in email
open( RPT_FILE, "<", "$cfg::params{'RPT_FILE'}" ) or
exit_with_error("do_report(): Can't open file for input: ".
"$cfg::params{'RPT_FILE'}: $!\n");
# Enable slurp mode & encode for mail attachment: see send_email()
$report_data = encode_base64( do { local $/; <RPT_FILE> });
close(RPT_FILE) or exit_with_error("do_report(): Can't close file for input: ".
"$cfg::params{'RPT_FILE'}: $!\n");
# Create report name to inc hostname
($r_name, $r_dir, $r_ext) = fileparse($cfg::params{'RPT_FILE'}, ".csv");
$report_name = $r_name."_".$hostname.$r_ext;
send_rpt_email($report_name, $report_data, $cfg::params{'RPT_EMAIL'});
#******************************************************************************
#
# Function : send_rpt_email
#
# Description : Send an email containing report data as attachment.
#
# NB: normally we'd use the Mail::Mailer module, but this
# Solaris 10 Perl 5.8 doesn't have it and we can't add it,
# so access sendmail directly, see the (old edition)
# Perl Cookbook Chap 18.3
#
# Params : $_[0] report name
# $_[1] Report data
# $_[2] target address(es). csv list if >1 address
#
# Returns : none
#
#******************************************************************************
sub send_rpt_email
{
my (
$report_name, # includes hostname
$report_data, # ie rpt file content: uuencode64 format
$target_addresses, # email address(es). csv list if >1 address
$hostname, # name of this box for sending
$prog_full_name, # prog path + name
$contact_email, # contact email address
$mailer, # mailer object
$sender, # sender of email as specified in cfg file
$subject, # subject line in email
$body_text # actual email msg text
);
# Assign input params
$report_name = $_[0];
$report_data = $_[1];
$target_addresses = $_[2];
# Get hostname anyway we can using Sys::Hostname;
# tries syscall(SYS_gethostname), `hostname`, `uname -n`
$hostname = Sys::Hostname::hostname();
# Get program full path name
$prog_full_name = cwd()."/${0}";
# Set the fields required ...
$sender = "$prog_full_name";
# $subject = "$prog_full_name";
$subject = "${0} on $hostname";
# Doing it like this because we don't have certain useful
# modules available; see fn header
$body_text = <<END_OF_BODY;
Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"
---q1w2e3r4t5
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
This is an automated message, please see attachment
---q1w2e3r4t5
Content-Type: application/octet-stream; name="$report_name"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$report_name"
$report_data
---q1w2e3r4t5--
END_OF_BODY
# Target email address is 1 or more addresses, as a csv list
for $contact_email ( split( /,/, $target_addresses) )
{
# ... and send it
open(SENDMAIL, "|/usr/sbin/sendmail -oi -t ")
or die "Can't open pipe to sendmail: $!\n";
print SENDMAIL "From: $sender\n".
"To: $contact_email\n".
"Subject: $subject\n".
"$body_text\n";
close(SENDMAIL) or die "Can't close sendmail pipe: $!\n";;
}
}
3. you could ask the gurus over at www.perlmonks.org
Please post your soln in any case.
|
|
1 members found this post helpful.
|
06-28-2012, 09:51 PM
|
#3
|
LQ Newbie
Registered: Jun 2012
Posts: 4
Original Poster
Rep:
|
Thanks!
I am almost there. My Perl can send zip attachment from the server end, but the receiving end cannot open the zip file (it did not have the valid format). All I need is to properly follow Perl's programming steps. Followed Perl script from the web. Here is what I got:
$smtp->mail("abc\@company1.com");
$smtp->recipient("xyz\@company2.com");
$smtp->data();
$smtp->datasend("$Subj\n");
$smtp->datasend("MIME-Version: 1.0\n");
$smtp->datasend("Content-type: multipart/mixed;\n\tboundary=\"$boundary\"\n");
$smtp->datasend("\n--$boundary\n");
$smtp->datasend("Content-type: text/plain\n");
$smtp->datasend("Content-Disposition: quoted-printable\n");
$smtp->datasend("\nSee attached zip file.\n\n\n");
$smtp->datasend("--$boundary\n");
$smtp->datasend("Content-Transfer-Encoding: base64 \n");
$smtp->datasend("Content-Type: application/zip; name=\"$attachFile\"\n");
$smtp->datasend("Content-Disposition: attachment; filename=\"$attachFile\"\n");
$smtp->datasend("\n");
$smtp->datasend("$data_file\n");
$smtp->datasend("--$boundary--\n");
$smtp->dataend();
$smtp->quit;
Please give suggestions on the code provided here.
Thank you!
|
|
|
All times are GMT -5. The time now is 11:43 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|