user1821820
user1821820

Reputation: 121

Perl, read file and insert content into email's body

i am facing small challenge but time is running and not a really perl guy, so any help is welcome. What I have is script that check all running processes and write the status in /tmp/error Then i have perl script to send email with attachement via external stmp but what i need is pierce of code that takes that /tmp/error and put that in the emails body so thre is not need for attachement. Here is what I found> this sends file as attachement but i need that in the body.

 #!/usr/bin/perl

use MIME::Lite;

# Set this variable to your smtp server name 
my $ServerName = "smtp.comcast.net"; 

my $from_address = '[email protected]';
my $to_address   = '[email protected]';
my $subject      = 'MIME Test: Text';
my $mime_type    = 'text';
my $message_body = "Testing text in email.\n";

# Create the initial text of the message
my $mime_msg = MIME::Lite->new(
   From => $from_address,
   To   => $to_address,
   Subject => $subject,
   Type => $mime_type,
   Data => $message_body
   )
  or die "Error creating MIME body: $!\n";


# Attach the text file
my $filename = 'C:\tmp\test.txt';
my $recommended_filename = 'test.txt';
$mime_msg->attach(
   Type => 'application/text',
   Path => $filename,
   Filename => $recommended_filename
   )
  or die "Error attaching text file: $!\n";

# encode body of message as a string so that we can pass it to Net::SMTP.
my $message_body = $mime_msg->body_as_string();

# Let MIME::Lite handle the Net::SMTP details
MIME::Lite->send('smtp', $ServerName);
$mime_msg->send() or die "Error sending message: $!\n";

Please help

Upvotes: 2

Views: 4663

Answers (1)

sidyll
sidyll

Reputation: 59287

Just add the file contents to your $message_body, right?

my $message_body = "Testing text in email.\n";
{
  local $/ = undef;
  open FILE, "file" or die "...: !$";
  $message_body .= <FILE>;
  close FILE;
}

Be careful if that file is too big though.

Upvotes: 1

Related Questions