I have used this script to send the text file, the email go out with attachment but when I open the attached file it's blank. any idea why? did I miss anything. thx
#!/usr/bin/perl -wl
my $msg = MIME::Lite->new(
From => 'xxx.net',
To => 'xxx.com',
Subject => 'Report',
Type => 'multipart/mixed',
)or die "Error creating multipart container: $!\n";
$msg->attach(
Type => 'TEXT',
Data => " Please check the attached file.",
)or die "Error adding the text message part: $!\n";
$msg->attach (
Type => 'text/plain',
Path => '/myfile/file1',
Filename => 'result.txt',
Disposition => 'attachment'
)or die "Error adding the attached file part: $!\n" ;
$msg->send;
You're a little confused about the arguments to attach. From the fine manual:
Filename
Optional. The name of the attachment. You can use this to supply a recommended filename for the end-user who is saving the attachment to disk. You only need this if the filename at the end of the "Path" is inadequate, or if you're using "Data" instead of "Path". You should not put path information in here (e.g., no "/" or "\" or ":" characters should be used).
[...]
Path
Alternative to "Data" or "FH". Path to a file containing the data... actually, it can be any open()able expression. If it looks like a path, the last element will automatically be treated as the filename. See "ReadNow" also.
The Path is the full path to the file that you want to attach, the Filename is the name that you want to receiver to see for that file.
I think you want this:
$msg->attach (
Type => 'text/plain',
Path => '/myfile/file1/result.txt',
Filename => 'result.txt',
Disposition => 'attachment'
) or die "Error adding the attached file part: $!\n" ;
Related
The following Perl program gives an access denied error at line 44 when I try to print either the entire string or the encoded portion. If I print just the header using $msg->print_header(\*STDOUT).
What I am trying to do is generate a text file that contains all the information that could be used in a telnet command to test a Message Transfer Agent (MTA) by sending an email with an attachment.
use MIME::Lite;
use Net::SMTP;
### Add the sender, recipient and your SMTP mailhost
my $from_address = 'temp999 at gmail.com';
my $to_address = 'test05#gmail.com';
my $mail_host = 'gmail.com';
### Adjust subject and body message
my $subject = 'Testing script';
my $message_body = "I am sending an email with an attachment";
### Adjust the filenames
my $my_file_xlsx = 'c:/temp';
my $your_file_xlsx = 'count.xlsx';
### Create the multipart container
$msg = MIME::Lite->new(
From => $from_address,
To => $to_address,
Subject => $subject,
Type => 'multipart/mixed'
) or die "Error creating multipart container: $!\n";
### Add the text for the message body
$msg->attach(
Type => 'TEXT',
Data => $message_body
) or die "Error adding the text message part: $!\n";
### Adding an Excel file
$msg->attach(
Type => 'application/octet-stream',
Path => $my_file_xlsx,
Filename => $your_file_xlsx,
Disposition => 'attachment'
) or die "Error adding $file_xls: $!\n";
### Send the Message
MIME::Lite->send('smtp', $mail_host, Timeout => 60);
#$msg->send;
$msg->print(\*STDOUT); # Write to a file handle ### LINE 44 ###
#$msg->print_header(\*STDOUT); # Write the header
#$msg->print_body(\*STDOUT); # Write the encoded body
I haven't found anything matching exactly what I am trying to do, but I might not be using the correct terminology when searching.
You're trying to attach the file c:/temp, which is (presumably) a directory, not a file. When MIME::Lite tries to open it as a file to read the contents,it fails with that error. You probably meant to pass c:/temp/count.xlsx as Path.
I am copying the file from remote machine to the local and this operation to be perform everyday once. In case of append of content in the remote file content, i will just copy the appended content to local file(as it is already exists at local machine). I am using Net::SFTP::Foreign module from CPAN, but seems like it is copying the full file in case of append(which is not expected).
use strict;
use warnings;
use Net::SFTP::Foreign;
my $file = '/home/user/temp/test.txt';
my $destination = '/home/user/dest.txt';
my $sftp = Net::SFTP::Foreign->new(
host => 'localhost', # using localhost for destination and source
more => [ -o => 'Compression yes', '-v' ]
);
$sftp->get( $file, $destination, copy_perm => 1, append => 1 );
if($sftp->error) {
print "get operation failed for $file : " . $sftp->error . "\n";
}
I checked the Net/SFTP/Foreign.pm module for get() implementation and found below code snippet in case of append -
my $flags = Fcntl::O_CREAT|Fcntl::O_WRONLY;
$flags |= Fcntl::O_APPEND if $append;
$lstart = sysseek($fh, 0, 1) if $append;
In case of append,$lstart contains the 0 only, which is beginning of the file. Am i missing something here?
Thanks for you comments, actually i found the reason why it was not working properly. It was keep overwriting the local file with remote one.
But when i use below code :
$sftp->get(
$file,
'/home/user/test.log',
append => 1,
overwrite => 0,
);
Now it won't overwrite the file, but append the whole file to the local file.
While i wanted to just append the text which is added to the remote file rather than the whole file.
This feature does not support by the Net::SFTP::Foreign.
I'm using this script to send an email with attachment using perl
The issue I do have is that I'm sending a csv file with some formatting, the send part goes very well without any error but when I receive the email, the attachment that has NO formatting with funny characters.
screen the file that I send - http://imgur.com/Gkkz26W
screen the file that I received - http://imgur.com/UMlXp3F
Anyone understand why this is happening?
#!/usr/bin/perl
use MIME::Lite;
use Net::SMTP;
### Adjust sender, recipient and your SMTP mailhost
my $from_address = 'martin dot zahn at akadia dot ch';
my $to_address = 'martin dot zahn at akadia dot ch';
my $mail_host = 'mailhost.domain.com';
### Adjust subject and body message
my $subject = 'A message with 2 parts ...';
my $message_body = "Here's the attachment file(s) you wanted";
### Adjust the filenames
my $my_file_csv = 'my_file.csv';
my $your_file_csv = 'your_file.csv';
### Create the multipart container
$msg = MIME::Lite->new (
From => $from_address,
To => $to_address,
Subject => $subject,
Type =>'multipart/mixed'
) or die "Error creating multipart container: $!\n";
### Add the text message part
$msg->attach (
Type => 'TEXT',
Data => $message_body
) or die "Error adding the text message part: $!\n";
### Add the CSV file
$msg->attach (
Type => 'text/csv',
Path => $my_file_csv,
Filename => $your_file_csv,
Disposition => 'attachment'
) or die "Error adding $file_csv: $!\n";
### Send the Message
MIME::Lite->send('smtp', $mail_host, Timeout=>60);
$msg->send;
500 Internal server error when attaching a file, but not when sending without attachment.
use MIME::Lite;
$msg = MIME::Lite->new(
From =>'email#domain.com',
To =>'email#domain2.com',
Subject =>'A message with 2 parts...',
CC => '',
Type =>'TEXT',
Data =>'Thank you for your interest in'
);
### If I comment out the following attachment code the email sends OK, otherwise i get 500 internal server error
$msg->attach(
Type =>'image/gif',
Path =>'/images/tree.gif',
Filename =>'tree.gif',
Disposition => 'attachment'
)or die "error attaching file\n";
$msg->send;
Just a suggestion and a few things I can recommend for this also. Applying this method will allow you to split your text/html parts and attachments to include, so you can send a message with multi attributes if you would like.
use strict;
use warnings;
use MIME::Lite;
my $msg = MIME::Lite->new(
To => 'email#domain2.com',
From => 'email#domain.com',
Subject => 'A message with 2 parts...',
Type => 'multipart/alternative',
);
# Make my text part
my $txt = MIME::Lite->new(
Type => "text/plain",
Data => 'Thank you for your interest in',
);
# Make my html part
my $html = MIME::Lite->new(
Type => 'multipart/related',
);
# Here you can attach what html tags you would like to include.
$html->attach(
Type => 'text/html',
Data => "<b>my html is here</b>",
);
$html->attach(
Type => 'image/gif',
Id => 'tree.gif',
Path => "../images/tree.gif",
);
$msg->attach($txt);
$msg->attach($html);
my $data = $msg->as_string;
Also I seen where you were using die for error handling, no need to do that here.
The error ended up being in that the URI has to be written relative to the script.
So I had to change /images/tree.gif
To
../images/tree.gif
We have private MediaWiki installation inside our company. Based on daily builds on our source code, we update the wiki with Perforce labels so that people can use the build that is labeled for streamlined process. We tried to automate this using Perl scripts on a Windows server using MediaWiki::Bot and MediaWiki::API.
use MediaWiki::Bot;
use MediaWiki::API;
my $mw = MediaWiki::API->new();
$mw->{config}->{api_url} = 'http://somewiki/w/index.php/title#feature_List';
# log in to the wiki
$mw->login({
lgname => 'username',
lgpassword => 'password'
|| die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
# get a list of articles in category
my $articles = $mw->list({
action => 'query',
list => 'categorymembers',
cmtitle => 'Category:Perl',
cmlimit => 'max'
}) || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
# and print the article titles
foreach (#{$articles}) {
print "$_->{title}\n";
}
Output:
2: Failed to decode JSON returned by http://vaporwiki/w/index.php/Executor#Execu
tor_Feature_List
Decoding Error:
malformed JSON string, neither array, object, number, string or atom, at charact
er offset 0 (before "<!DOCTYPE html PUBLI...") at C:/Perl/lib/MediaWiki/API.pm l
ine 398
Returned Data: <whole page data>
The API URL is wrong. Try http://vaporwiki/w/api.php.