Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using MIME::Lite & trying multi line text 2

Status
Not open for further replies.

jcarrott

Programmer
May 28, 2009
130
US
I am trying to use Perl with MIME::Lite to send emails. I want the body of the email to contain a multi line statement and I keep getting error messages.

#! /usr/bin/perl -w
use MIME::Lite;

$vPath1='D:\\Inetpub\\ftproot\\Email';
$vName1='D:\\Inetpub\\ftproot\\Email\\emailout.txt';

$rc=system("cd $vPath1");

open(IN, "$vName1") or die 'Could not open emailout.txt';
$line=<IN>;
chomp;
@field = split(/\,/, $line);
$sendTo = $field[3];
print "Send To: ";
print $sendTo;
print "\n";

my $msg = MIME::Lite->new(
From => 'mmis@ochsner.org',
To => 'jcarrott@ochsner.org',
Subject => 're: PO from Ochsner',
Type => 'multipart/mixed',
Data => qq{
<body>
Attached you will find a Purchase Order from Ochsner Health System.

To confirm this order please fax (504) 842-9132 or email (mmis@ochsner.org)
with this purchase order and note any;
1. Item unit price difference
2. Backorder quantity by item
3. Different delivery date than requested

Ochsner Health System is not responsible for duplicate orders.
Please include buyer's name on returned copy.
If freight is going to be charged for this order, please ship the product via
Federal Express and choose "Bill Third Party" and use account #331453706

If you have any questions please call the buyer listed on the purchase order.

CONFIDENTIALITY NOTICE: Privileged/Confidential information may be contained
in this electronic transmission. The information is intended for the use of
the recipient named above. If you have received this electronic mail in error,
please immediately notify us by email or calling (504) 842-3420. You are
cautioned that any disclosure, copying, distribution, or other use of the
transmitted information is strictly prohibited.
</body>
};


$msg->attach(
Type => 'TEXT',
Path => 'D:\\Appdata\\Email',
Filename => '03041015150200.pdf',
);

$msg->send('smtp', 'smtp.ochsner.org');

For now the attachment is hard coded.

Can anybody tell me how to have a multi line text body in the email?
 
I changed the program to limit what is wrong.

#! /usr/bin/perl -w
use MIME::Lite;

$vPath1='D:\\Inetpub\\ftproot\\Email';
$vName1='D:\\Inetpub\\ftproot\\Email\\emailout.bak';

$rc=system("cd $vPath1");

open(IN, "$vName1") or die 'Could not open emailout.txt';
$line=<IN>;
chomp;
@field = split(/\,/, $line);
$sendTo = $field[3];

# print "Send To: ";
# print $sendTo;
# print "\n";

my %options;
$options{INCLUDE_PATH} = 'D:\Program Files\OHS\Email';

my $msg = MIME::Lite->new(
From => 'mmis@ochsner.org',
To => 'jcarrott@ochsner.org',
Subject => 're: PO from Ochsner',
Type => 'multipart/mixed',
Template => {
text => 'EmailBody.txt',
},
);
# $msg->attach(
# Type => 'application/pdf',
# Path => 'D:\Appdata\Email',
# Filename => '03051010442900.pdf',
# );

$msg->send('smtp', 'smtp.ochsner.org');

As you can see I am not adding an attachment at this time, and I am trying to use a template for the body of the email.

The email does get sent, but the body is blank.

Does anybody have any ideas??
 
I do it this way..
Code:
use constant SMTP_DOMAIN => "127.0.0.1";

##############################
###### SENDMAIL ROUTINE ######
##############################
# USING MIME::Lite;
sub send_mail {

#_[0] = To
#_[1] = From
#_[2] = Subject
#_[3] = Body Text
#_[4] = Filename
#_[5] = File
#_[6] = Type

use MIME::Lite;

my @TO = split(/\,/, $_[0]);

foreach my $to (@TO){
    my $msg = MIME::Lite->new(  From    => "$_[1]",
                            To      => "$to",
                            Subject => "$_[2]",
                            Type    => 'multipart/mixed');

    if($_[4]){$msg->attach( Type        => "$_[6]",
                        Path        => "$_[5]",
                        Filename    => "$_[4]");
    }
 
    $msg->attach(Type        => 'TEXT/PLAIN',
             Data        => "$_[3]");

    $msg->send('smtp', SMTP_DOMAIN);
}

}

And call it like so
Code:
my $body = "This is my email body";
my $subject = "This is my subject";
my $recip = "them\@theirdomain.com";
my $from = "me\@mydomain.com";

# Send Email
&send_mail($recip,$from,$subject,$body);

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
I am using,

Code:
$vPath1='D:\\Inetpub\\ftproot\\Email';
$vName1='D:\\Inetpub\\ftproot\\Email\\emailout.bak';
$vName2='D:\\Program Files\\OHS\\Email\\EmailBody.txt';
$rc=system("cd $vPath1");

open(IN, "$vName1") or die 'Could not open emailout.txt';
open(IN2, "$vName2") or die 'Could not open EmailBody.txt';

$line=<IN>;
$temp01=<IN2>;
$temp02="\n";
$temp03=<IN2>;
chomp;
@field = split(/\,/, $line);
$sendTo = $field[3];
my %options;
$message = 'EmailBody.txt';

my $msg = MIME::Lite->new(
    From     => 'mmis@ochsner.org',
    To       => $sendTo,
    Subject  => 're: PO from Ochsner',
    Data     => $temp01, 
                $temp02,
                $temp03
        );

$msg->send('smtp', 'smtp.ochsner.org');

The text of EmailBody.txt is:

Code:
Attached you will find a Purchase Order from Ochsner Health System.
To confirm this order please fax (504) 842-9132 or email (mmis@ochsner.org) 
with this purchase order and note any of the following discrepancies on the 
purchase order. If no discrepancies are found please note "Fulfilled" on 
first page of PO.

The email is being sent with only the first line of the EmailBody.txt document.The good news is that I am making progress. I just need the rest of the EmailBody.txt.Any ideas??
 
I've noticed for a start you have surrounded your email text with HTML body tags.

firstly you need to decide if you are sending plain text or html and amend the
Code:
$msg->attach(Type        => [b]'TEXT/PLAIN'[/b],             
             Data        => $body);
accordingly.

Then if you are using html for the email body, you need the correct newline tag <br> alternatively have you tried \n?

the way you are crerating the email is via the constructor, where as I am using the methods to add the content.

Dunno if this is makng a difference.

Have you tried my code passing in your text read from the file?

Also when ever I read a file I like do do it like this..
Code:
my $body;
open(IN, "$vName1")
while (<IN>) {
	$body .= $_;
}
close (IN);

Then pass $body as the email body not the filehandle the way you have done it.

Dunno if it makes a difference, it's just how I do things and it works for me.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
It does make a difference, $temp03=<IN2>; will only read one line of the input file. If you want to read the entire file you need to read it into an array, e.g. @temp03=<IN2>;, or use a loop like 1DMF's code does.

Annihilannic.
 
I wondered if passing the filehandle only read one line.

If you assinged the filehandle to an array, can you pass MIME::Lite an array as the email body or would you still need to loop and concatenate?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
I'm not sure, but certainly no need for a loop when you could just pass join '\n',@temp03, or similar.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top