?
Solved

Sending plain text and html with MIME::Lite

Posted on 2003-02-24
3
Medium Priority
?
212 Views
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();
     my($to,$subject,$message,$path,$name,$idx,$username)=@_;
     my($email)=$to;
     $username=~/^\s*(\w{1,20})\s*(\w{0,20}).*$/;
     $secname=$2;$secname=~tr/A-Z/a-z/;$secname="\u$secname";
     $username=$1;$username=~tr/A-Z/a-z/;$username="\u$username";
     my($fromars,$namears,$unlink)=(getarlist("$path/$path.cfg"));
     $fromaddr="$namears<$fromars>";
     if ($unlink == 1) {$unlink="\r\n\r\n To unsubscribe:\r\n$scripturl?mode=r&a=$path&e=$to"}
     $to=~s/@/\@/;
     if ($secname ne ""){$secname=" ".$secname};
     $to="$username$secname <$to>";
     $name=~/.*\.(.*)/;
        my($real)="upfile$idx.$1";
     $path="$path/$real";
     my($current)="$mday/$mon/$year";
     $message=~s/\[FIRSTNAME\]/$username/gie;
     $message=~s/\[EMAIL\]/$email/gie;
     $message=~s/\[DATE\]/$current/gie;
     $message=~s/\[FULLNAME\]/$username.$secname/gie;
     $message.="\n\n$unlink\n\n";
     $subject=~s/\[FIRSTNAME\]/$username/gie;
     $subject=~s/\[EMAIL\]/$email/gie;
     $subject=~s/\[DATE\]/$current/gie;
     $subject=~s/\[FULLNAME\]/$username.$secname/gie;
     my($mime)=&mimeformat($name);
      my $msg = MIME::Lite->new
                 (Type =>'multipart/mixed',
                   From =>"$fromaddr",
               To =>"$to",
                Subject =>"$subject",
               Type => "TEXT",
               Data =>"$message");
     if($name ne ""){
              $msg->attach
               (Type =>$mime->[0],
               Encoding =>$mime->[1],
               Filename =>"$name",
               Path =>"$path",
                  );
     }
     MIME::Lite->send('sendmail',$mailprog);
      $msg->send;
}
0
Comment
Question by:misterbrick
[X]
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
3 Comments
 
LVL 26

Accepted Solution

by:
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.
0
 
LVL 20

Expert Comment

by:jmcg
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

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