LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 06-28-2012, 06:26 PM   #1
oihsu21
LQ Newbie
 
Registered: Jun 2012
Posts: 4

Rep: Reputation: Disabled
Unhappy 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?
 
Old 06-28-2012, 07:28 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415

Rep: Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785
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.
Old 06-28-2012, 09:51 PM   #3
oihsu21
LQ Newbie
 
Registered: Jun 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
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!
 
Old 06-29-2012, 03:49 AM   #4
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Hi,

I don't know any Perl but I thought you could look at Perl Mail::Sendmail FAQ
or Sending email with Perl with attachment
or Perl using NET::SMTP

if something will work.

good luck
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mail attachment using perl ++nick++ Programming 5 03-01-2010 06:17 AM
How to send a html file as an file attachment to my mail from crontab? GRD Linux - Newbie 2 06-03-2008 12:39 PM
bash script to send a mail attachment aeby Linux - General 6 05-28-2007 07:13 AM
howto send a mail with attachment via perl script ? cccc Programming 24 03-05-2004 08:49 PM
mail -a would not work to send e-mail attachment saavik Linux - Networking 3 12-18-2003 10:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration