Send mail in html format from perl script

11,568

Solution 1

Use Mime::Lite. This an example:

my $msg = MIME::Lite->new(
     To      => '[email protected]',
     Subject => 'HTML example',
     Type    => 'text/html',
     Data    => '<h1>Hello world!</h1>'
);

$msg->send();

Solution 2

The fix for your problem is to add a content-type header saying that the mail is text/html.

However.

  1. Please don't send HTML email without sending an equivalent plain-text attachment as well.
  2. Please make your life easier by using a module. Stuff in the Email::* namespace is best.
  3. Please throw away whatever book told you to call Perl subroutines using &. It's almost twenty years out of date.
Share:
11,568
SteveScm
Author by

SteveScm

Hi, I am working as a Devops Engineer.

Updated on June 05, 2022

Comments

  • SteveScm
    SteveScm almost 2 years

    I am working on unix environment and have a perl scrip to send mail, but i need to send HTML formatted mail,but it printing as it is html code. so could anyone please let me know how it manipulate or compile the html and send formatted mail.

    #!/usr/bin/perl
    #print "Content-type: text/html\n\n";
    
    print("enter my name");
    chop($name=<stdin>);
    &mail();
    
    
    sub mail{
    
    $title='perl';
    $to='[email protected]';
    $from= '[email protected]';
    $subject=$name;
    
    open(MAIL, "|/usr/sbin/sendmail -t");
    
    ## Mail Header
    print MAIL "To: $to\n";
    print MAIL "From: $from\n";
    print MAIL "Subject: $subject\n\n";
    ## Mail Body
    print MAIL $name;
    print MAIL "<html><body><p>";
    print MAIL "<b>Hello</b>";
    print MAIL "This is a test message from Cyberciti.biz! You can write your";
    
    print MAIL "</p></body></html>";
    ##print MAIL "$title";
    close(MAIL);
    }
    

    Its printing in mail:

    <html><body><p><b>Hello</b>This is a test message from Cyberciti.biz! You can write your</p></body></html>
    

    like this...as it is seems its not converting it into html format. So please help me over this.

  • SteveScm
    SteveScm almost 11 years
    thanks . Could you please explain me what are these Net::SMTP. As i am new to perl and net protocol.
  • SteveScm
    SteveScm almost 11 years
    could you please explain this line: MIME::Lite->new(
  • SteveScm
    SteveScm almost 11 years
    i run this code, but its not sending the mail , instead it printing the same line on console. <h1></h1><p>A message has been sent from to </p></body></html>
  • Miguel Prz
    Miguel Prz almost 11 years
    you need to install the CPAN MIME::Lite module, and then use it. See the link I provided
  • SteveScm
    SteveScm almost 11 years
    could i check on my linux machine whether it is installed or not. Any command
  • Harry Barry
    Harry Barry almost 11 years
    @ Rohit889 Net::SMTP is module for perl, it allows you to do certain things instead of having to call external commands. so instead of you having to call Blat or sendmail as external command, you just install the module and then use it in your script.
  • Harry Barry
    Harry Barry almost 11 years
    @Rohit889 Not all modules come preinstalled on perl, you need to install it yourself. easiest method is to run from command line "ppm install Net::SMTP" if behind a firewall, run this first. "set http_proxy=username:password@proxy_servername_or_IP:port"
  • Harry Barry
    Harry Barry almost 11 years
    run "ppm install MIME::Lite" if it is installed it will skip, if not, it will install it.
  • SteveScm
    SteveScm almost 11 years
    Just one ques, can't i use html with sendmail protocol ?
  • Harry Barry
    Harry Barry almost 11 years
    I am not sure, is sendmail not the old blat? I have not used it in a very long time.
  • Dave Cross
    Dave Cross almost 11 years
    @Harry: Most Linux machine won't have ppm available.