LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-27-2008, 09:28 AM   #1
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Rep: Reputation: 15
Thumbs up How can I attach a text file to the email in perl


Hai,

I wish to send a text file as attachment to the my mailid from my centos box using perl. I tried but could no succeed. Please help in writing the code in perl to attach a text file

Thanks,
Prakash.
 
Old 10-27-2008, 11:11 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,318

Rep: Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879
Quote:
Originally Posted by prakash.akumalla View Post
Hai,

I wish to send a text file as attachment to the my mailid from my centos box using perl. I tried but could no succeed. Please help in writing the code in perl to attach a text file

Thanks,
Prakash.
"Help in writing the code"? Sure, post what you've written so far, along with the error(s) you're getting, and we can help you. Otherwise, look up mime::lite or email::mime on CPAN.

If you're looking for someone to write code for you, that's called a "consultant" or "programmer", and they typically get paid to do it.
 
Old 10-29-2008, 12:40 AM   #3
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Original Poster
Rep: Reputation: 15
Hi,

Thanks for your reply. This is how i am sending the mail using perl.

#!/usr/bin/perl

$to='mymail@gmail.com';
$from= 'root@localhost.com';
$subject='TEST MESSAGE';

open(MAIL, "|/usr/sbin/sendmail -t");

## Mail Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
## Mail Body
print MAIL " Hai this is a test message. \n";

close(MAIL);


Please help me in attaching a text file to it.

Thanks,
Prakash.
 
Old 10-29-2008, 02:07 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
As mentioned, read up on Mime::Lite at search.cpan.org
Here's one I wrote
Code:
    # Set the fields required by Mailer...
    $contact_email = $cfg::params{'USER_EMAIL'};
    $sender = "$prog_full_name";
    $subject = " Asset Mgr Report for : $table_name";
    $body_text = "This is an automated message:\n\n".
                 "CSV report of active rows in $table_name\n\n".
                 "Regards,\n$prog_full_name at $hostname\n\n";

    # ... and send it
    # Header
    $msg = MIME::Lite->new(
                            From    => $sender,
                            To      => $contact_email,
                            Subject => $subject,
                            Type    =>'multipart/mixed',
                            Debug   => 1
                            );
    # Body Content
    $msg->attach(
                Type     => 'TEXT',
                Data     => $body_text
                );
    # Attachment
    $msg->attach(
                Type     => 'text/plain',
                Path     => "$rpt_file",
                Filename => "${rpt_filename}.${rpt_filext}",
                Disposition => 'attachment'
                );
 
Old 10-29-2008, 02:18 AM   #5
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Original Poster
Rep: Reputation: 15
Hi,
Thanks for your reply. When i execute the above code there occurred an error saying that

Can't locate object method "new" via package "MIME::Lite" (perhaps you forgot to load "MIME::Lite"?) at file.pl

I have already install MIME::Lite using cpan.

Why is this happening so?

Please look at this once.

Thanks,
Prakash.
 
Old 10-29-2008, 02:22 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
You need to read those links I put in your other thread. They'll teach you how to use Perl.
If you want Perl to use a non-Core module, you have to specify it to be used explicitly, a bit like you would in C.
In this case

use Mime::Lite;

at the top
 
Old 10-29-2008, 07:18 AM   #7
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Original Poster
Rep: Reputation: 15
Hi,

Finally succeeded in sending the file with an email. But I am able to send it in the form of only text. That's enough for me now.

Thanks for all the guys who helped me.
Prakash.
 
Old 10-30-2008, 05:11 AM   #8
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Original Poster
Rep: Reputation: 15
Hi,

This is how i used to send the mail.

Quote:
$contact_email = "prakash.akumalla@xxx.com";
$sender = "root@localhost.com";
$subject = " Asset Mgr Report for : $table_name";
$body_text = "This is an automated message:\n\n".
"CSV report of active rows in $table_name\n\n".
"Regards,\n$prog_full_name at $hostname\n\n";

# ... and send it
# Header
$msg = MIME::Lite->new(
From => $sender,
To => $contact_email,
Subject => $subject,
Type =>'multipart/mixed',
Debug => 1
);
# Body Content
$msg->attach(
Type => 'TEXT',
Data => $body_text
);
# Attachment
$msg->attach(
Type => 'text/plain',
Path => "/root/localhost.csv",
Filename => "${rpt_filename}.${rpt_filext}",
Disposition => 'attachment'
);
It did not work. Apart from the attachment, mail is also not been sent.

Please look at this once.

Thanks,
Prakash.
 
Old 10-30-2008, 07:41 AM   #9
prakash.akumalla
Member
 
Registered: Jun 2008
Location: India
Posts: 109

Original Poster
Rep: Reputation: 15
This is the script I finally used and succeeded.

[HTML]use MIME::Lite;
use Net::SMTP;

$msg = MIME::Lite->new (
From => 'root@localhost.com',
To => 'prakash.akumalla@xxx.com',
Subject => 'Test Mail',
Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";

$Mail_msg = "This is the test mail";
$attachment = "/root/localhost.csv";
$datafilename = "localhost.csv";

### Add the text message part
$msg->attach (
Type => 'TEXT',
Data => $Mail_msg
) or die "Error adding the text message part: $!\n";

### Add the text file
$msg->attach (
Encoding => 'base64',
Type => "text/csv",
Path => $attachment,
Filename => $datafilename,
Disposition => 'attachment'
) or die "Error adding $datafile: $!\n";

MIME::Lite->send('smtp', 'mail_server', Timeout=>60);

$msg->send;[/HTML]


Thanks for all those guys who helped me,
Prakash.
 
  


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
Perl replace text in file ShaqDiesel Programming 27 08-11-2010 11:50 PM
sendmail : How to email contents of a text file? concoran Linux - Newbie 5 03-09-2008 09:27 PM
How to use "mail" to attach a file with an email? meili100 Linux - Software 6 02-23-2008 06:21 AM
Parsing large text file with perl smaida Programming 5 09-13-2004 05:33 AM
cron job that email a text file paynsr Linux - Newbie 1 03-29-2004 02:36 PM


All times are GMT -5. The time now is 03:36 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration