Solved

perl to add html file as body of email

Posted on 2009-05-13
2
512 Views
Last Modified: 2012-05-06
I am simply trying to us MIME::Lite to add the contents of a pre-existing html file as the body of an email, not an attachment.  I have everything working, regarding the email, but can't figure out how to place the html file as the body of an email.  I can make it work using an attachment, but not in the body text.  
Please let me know how to place the contents of a file into the body content of the email message.
I am using Unix.
#!/usr/bin/perl
use MIME::Lite;
use Net::SMTP;
my $mail_host = '11.111.11.11';
$msg = MIME::Lite->new)
From =>'myemail@home.com';
To =>'myfrom@home.com';
Subject =>'send as html inline';
Type =>'text/html';
Data =>'this is where the contents of the external html should go'
);
MIME::Lite->send('smtp', $mail_host, Timeout=>60);
$msg->send;

Open in new window

0
Comment
Question by:bray007
2 Comments
 
LVL 25

Accepted Solution

by:
lwadwell earned 500 total points
ID: 24372148
Hi bray007,

try something like ...

lwadwell
#!/usr/bin/perl
use MIME::Lite;
use Net::SMTP;
# Open and read file contents
open(INFILE,"/path/to/filename.html") or die "Cannot open file $!";
my @filecontents = <INFILE>;
close(INFILE);
# Send email
my $mail_host = '11.111.11.11';
my $msg = MIME::Lite->new(
From =>'myemail@home.com',
To =>'myfrom@home.com',
Subject =>'send as html inline',
Type =>'text/html',
Data => join("",@filecontents)
);
MIME::Lite->send('smtp', $mail_host, Timeout=>60);
$msg->send;

Open in new window

0
 

Author Comment

by:bray007
ID: 24372179
Perfect, it worked.  The only edit is an additional ) was removed from your example.  Working sample in the code snippet.
thanks a bunch!
#!/usr/bin/perl
use MIME::Lite;
use Net::SMTP;
# Open and read file contents
open(INFILE,"/path/to/filename.html") or die "Cannot open file $!";
my @filecontents = <INFILE>;
close(INFILE);
# Send email
my $mail_host = '11.111.11.11';
my $msg = MIME::Lite->new(
From =>'myemail@home.com',
To =>'myfrom@home.com',
Subject =>'send as html inline',
Type =>'text/html',
Data => join("",@filecontents);
MIME::Lite->send('smtp', $mail_host, Timeout=>60);
$msg->send;

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question