Sending plain text and html with MIME::Lite

Posted on 2003-02-24
Medium Priority
Last Modified: 2008-02-20
I have an autoresponder script and am trying to set up an option in the script to send either plain text messages or html messages.
It currently will only send plain text with an attachment and the ability to personalize the messages.
The script uses MIME::Lite and sendmail. For the life of me I cannot figure out how to get this to work.
Below is the code for sending the messages.
Can any one help me?

sub sendmail{
     my($mon,$mday,$year) = ltime();
     if ($unlink == 1) {$unlink="\r\n\r\n To unsubscribe:\r\n$scripturl?mode=r&a=$path&e=$to"}
     if ($secname ne ""){$secname=" ".$secname};
     $to="$username$secname <$to>";
      my $msg = MIME::Lite->new
                 (Type =>'multipart/mixed',
                   From =>"$fromaddr",
               To =>"$to",
                Subject =>"$subject",
               Type => "TEXT",
               Data =>"$message");
     if($name ne ""){
               (Type =>$mime->[0],
               Encoding =>$mime->[1],
               Filename =>"$name",
               Path =>"$path",
Question by:misterbrick
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 26

Accepted Solution

wilcoxon earned 400 total points
ID: 8013320
One problem is you have two "Type =>" entries in your MIME::Lite->new call.  Reading the docs further, it looks like you are trying to use the documented hack.  Remove the "Type => 'multipart/mixed'," line for that.

Based on the MIME::Lite docs it looks like this should work:

my $msg;
my $type = ($send_type eq 'text') ? 'TEXT' : 'text/html';
if ($name ne '') {
    $msg = MIME::Lite->new(Type => 'multipart/mixed', From => $fromaddr, To => $to, Subject => $subject);
    $msg->attach(Type => $type, Data => $message);
    $msg->attach(Type => $mime->[0], Encoding => $mime->[1], Filename => $name, Path => $path);
} else {
    $msg = MIME::Lite->new(Type => $type, From => $fromaddr, To => $to, Subject => $subject, Data => $message);

Instead of choosing text of html, you could look into sending a multipart/alternative message with both text and html included.
LVL 20

Expert Comment

ID: 9692074
Nothing has happened on this question in over 8 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by Wilcoxon.

Please post any comments here within the next seven days. Moderators check comments here before acting on the recommendation. Experts: silence will likely be taken as assent.


EE Cleanup Volunteer

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans
Suggested Courses

766 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