Why is attachment file on email blank? - perl

My script sends email to a user with an attachment file.
The file name is hello.txt.
It contains "HELLO WORLD!! :D".
When I send the email, the file hello.txt is attached with the email.
But, when I open the file, hello.txt is blank and empty.
It should contain "HELLO WORLD!! :D"
The CODE
#!/usr/bin/perl
use MIME::Lite;
$to = 'abc#gmail.com';
$from = 'abc#gmail.com';
$subject = 'Test Email';
$message = 'This is test email sent by Perl Script';
$msg = MIME::Lite->new(
From => $from,
To => $to,
Cc => $cc,
Subject => $subject,
Type => 'multipart/mixed' #I SUSPECT, THE PROBLEM AT HERE
);
# Add your text message.
$msg->attach(
Type => 'text',
Data => $message
);
# Specify your file as attachement.
$msg->attach(Type => 'application/text', #I SUSPECT, THE PROBLEM AT HERE
Path => 'C:\Users\Desktop',
Filename => 'hello.txt',
Disposition => 'attachment'
);
$msg->send;
print "Email Sent Successfully\n";
Should I change Type or application/text?

Path => 'C:\Users\Desktop',
Path should be the path name of the file on your system. What you gave is only the directory name, where the file is located. Trying to read this will result in empty data - and thus empty content for the attached file. Instead you need to use
Path => 'C:\Users\Desktop\hello.txt',

Related

Perl MIME::Lite nested boundaries

I am working on a server that is missing MANY Perl libraries, so I am left with using MIME::Lite. Yes, I should be using something else, but it is not within my control.
Overall, the email package needs to be "multipart/mixed". It will have the plain text version, html version, and an attachment. The issue is, it shows BOTH the plain text and html version in the email body.
I would like to still support "multipart/alternative" so it shows EITHER plain text or html. However, since I need an attachment, the whole email should be "multipart/mixed" with an inner boundary section with "multipart/alternative" for text/html versions.
I though I had it working, but it drops off the attachment due to the whole email package was "multipart/alternative". Inspecting some emails in my inbox, and I noticed you can have "mixed" first, then inside have "alternative". Like a div in a div :)
Here is my current code snip (please look for my comments defining the boundary):
my $type = 'multipart/mixed';
sub sendMail {
my($from, $to, $subject, $messageHtml, $messageText, $type, %attachment) = #_;
my $msg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject,
Type => $type, # multipart/mixed
);
# -- start new boundary and multipart/alternative
if ( $messageText ne '' ) {
$msg->attach(
Type => 'text/plain; charset=UTF-8',
Encoding => 'quoted-printable',
Data => $messageText,
);
}
if ( $messageHtml ne '' ) {
$msg->attach(
Type => 'text/html; charset=UTF-8',
Encoding => 'quoted-printable',
Data => $messageHtml,
);
}
# -- end new boundary
if ( %attachment ) {
$msg->attach(
Type => $attachment{"type"},
Encoding => 'base64',
Path => $attachment{"path"},
Filename => $attachment{"filename"},
Disposition => 'attachment'
);
}
if ($debugmode) {
my $result = '';
$msg->print($result);
print $result;
exit;
}
if ($msg->send) {
return 1;
}
return 0;
}
I would modify the code to check if both html and plain text were passed. I am well aware, that I don't need alternative if only 1 of them are used.
Again, I need the plain text and html sections, to be inside their own boundary as "multipart/alternative"
You have to first create the message as multipart/mixed. Inside this message you need to create an inner part as multipart/alternative. Onto this inner part you should attach the HTML and text parts. And the attachment should be added to the outer (mixed) part. This could be achieved with code like this:
use strict;
use warnings;
use MIME::Lite;
# main message is multipart/mixed
my $msg = MIME::Lite->new(
From => 'me#example.com',
To => 'you#example.com',
Subject => 'some test message',
Type => 'multipart/mixed'
);
# inner part as multipart/alternative to include both HTML and text
my $alternative = $msg->attach(
Type => 'multipart/alternative',
);
$alternative->attach(
Type => 'text/plain',
Data => 'some TEXT here'
);
$alternative->attach(
Type => 'text/html',
Data => '<b>some HTML here</b>'
);
# attached image goes to main message
$msg->attach(
Type => 'image/gif',
Data => 'GIF89a....'
);
print $msg->as_string;

perl mime::lite attach text file line feed error

my #test = ("Row1", "Row2", "Row3");
my $attch = join("<cr><lf><br>\\n", #test);
$message = MIME::Lite->new(
From => $mailFrom ,
To => $address,
Subject => $title,
Type => 'text/html',
Encoding => '8bit',
Data => $data
);
$message->attach(
Type =>'TEXT',
Data => $attch
);
MIME::Lite->send('smtp', $host, Timeout => 20);
$message->send;
Good Day, i'm trying send a file in a email, but i cant write correct line feed, the code sends a email with a attached file, this attached file contains the next information:
"Row1<cr><lf><br>\nRow2<cr><lf><br>\nRow3"
How i can get:
Row1Row2Row3
In the attached file?
$message->attach(
Type => 'TEXT',
Data => join('', map { "$_\n" } #test),
);

People in cc are not receiving the mail using MIME::Lite api

Please find below my code snippet that send a mail to person and his friend in cc.
In the cc list I also have a DL.
use MIME::Lite;
$to = 'ABC#DOMAIN1.com';
$from = 'MAILER#DOMAIN2.com';
$subject = 'How are you doing';
$message = 'This is test email sent by Perl Script';
my #cc=('XYZ#DOMAIN2.com','DL#DOMAIN2.com');
$msg = MIME::Lite->new(
From => $from,
To => $to,
Cc =>\#cc,
Subject => $subject,
Data => $message
);
$msg->send('smtp','smtpserver', Timeout => 60 );
print "Email Sent Successfully\n";
The problem is the person and DL is cc are not receiving the mails.
Is there any log in the api MIME::Lite where I can check what is the error(if any)Or what do you think the problem can be?
Perhaps you want to take a closer look at the documentation for MIME::Lite. Here's the first example from the synopsis.
use MIME::Lite;
### Create a new single-part message, to send a GIF file:
$msg = MIME::Lite->new(
From => 'me#myhost.com',
To => 'you#yourhost.com',
Cc => 'some#other.com, some#more.com',
Subject => 'Helloooooo, nurse!',
Type => 'image/gif',
Encoding => 'base64',
Path => 'hellonurse.gif'
);
$msg->send; # send via default
The Cc parameter here is sent as a text string containing comma-separated email addresses. You are passing in a reference to an array of email addresses.

MIME::Lite - Mail to multiple recipients [duplicate]

Please find below my code snippet that send a mail to person and his friend in cc.
In the cc list I also have a DL.
use MIME::Lite;
$to = 'ABC#DOMAIN1.com';
$from = 'MAILER#DOMAIN2.com';
$subject = 'How are you doing';
$message = 'This is test email sent by Perl Script';
my #cc=('XYZ#DOMAIN2.com','DL#DOMAIN2.com');
$msg = MIME::Lite->new(
From => $from,
To => $to,
Cc =>\#cc,
Subject => $subject,
Data => $message
);
$msg->send('smtp','smtpserver', Timeout => 60 );
print "Email Sent Successfully\n";
The problem is the person and DL is cc are not receiving the mails.
Is there any log in the api MIME::Lite where I can check what is the error(if any)Or what do you think the problem can be?
Perhaps you want to take a closer look at the documentation for MIME::Lite. Here's the first example from the synopsis.
use MIME::Lite;
### Create a new single-part message, to send a GIF file:
$msg = MIME::Lite->new(
From => 'me#myhost.com',
To => 'you#yourhost.com',
Cc => 'some#other.com, some#more.com',
Subject => 'Helloooooo, nurse!',
Type => 'image/gif',
Encoding => 'base64',
Path => 'hellonurse.gif'
);
$msg->send; # send via default
The Cc parameter here is sent as a text string containing comma-separated email addresses. You are passing in a reference to an array of email addresses.

Perl Net::SMTP Email Size issue while using html format

I am sending an email via SMTP in perl . The email contains some tables,links and lists.
I am using html format data.
$smtp->data();
$smtp->datasend("MIME-Version: 1.0\nContent-Type: text/html; charset=UTF-8 \n\n<H1>");
$smtp->datasend("$message");
...
$smtp->dataend();
$smtp->quit;
Sometimes the email size is too large around 1mb. Is there any way I can reduce the size of email without reducing the amount of data.I do not want the message as an attachment. I use outlook to open the mails.
You should use Mail::Sender for sending attachments through email
#!/usr/bin/perl
use Mail::Sender
$to = 'email1#example1.com,email2#example2.com';
$sender =new Mail::Sender {
smtp => 'smtp.mailserver.com',
from => 'script#somedomain.com,
});
$subject = 'This is a Test Email';
$sender->OpenMultipart({
to => "$to",
subject => "$subject",
});
$sender->Body;
$sender->SendLineEnc("Test line 1");
$sender->SendLineEnc("Test line 2");
$sender->Attach({
description => 'Test file',
ctype => 'application/x-zip-encoded',
encoding => 'Base64',
disposition => 'attachment;
filename="File.zip"; type="ZIP archive"',
file => "$file",
});
$sender->Close();
exit();
or using MIME::Lite
use MIME::Lite;
$msg = MIME::Lite->new (
From => $from_address,
To => $to_address,
Subject => $subject,
Type =>'multipart/mixed'
) or die "$!\n";
### Add the ZIP file
$msg->attach (
Type => 'application/zip',
Path => $my_file_zip,
Filename => $your_file_zip,
Disposition => 'attachment'
) or die "Error adding $file_zip: $!\n";
### Send the Message
$msg->send('smtp', $mail_host, Timeout=>60);