Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
LVL 27

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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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

564 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