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

x
?
Solved

Sending plain text and html with MIME::Lite

Posted on 2003-02-24
3
Medium Priority
?
220 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
2 Comments
 
LVL 27

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

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