4

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='abcd@acv.com';
$from= 'xyz@xyz.com';
$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
  • 545
  • 1
  • 7
  • 15
  • check http://search.cpan.org/~rjbs/MIME-Lite-3.029/lib/MIME/Lite.pm or http://search.cpan.org/~rjbs/Email-MIME-1.920/lib/Email/MIME.pm – mpapec Jun 19 '13 at 07:07
  • just one ques, can't i use html formatting with sendmail protocol ? – SteveScm Jun 19 '13 at 08:26
  • Possible duplicate of [How can I send an HTML email with Perl?](http://stackoverflow.com/questions/2623651/how-can-i-send-an-html-email-with-perl) – Eugen Konkov Dec 10 '16 at 21:10

4 Answers4

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.
Dave Cross
  • 68,119
  • 3
  • 51
  • 97
1

Use Mime::Lite. This an example:

my $msg = MIME::Lite->new(
     To      => 'you@yourhost.com',
     Subject => 'HTML example',
     Type    => 'text/html',
     Data    => '<h1>Hello world!</h1>'
);

$msg->send();
Miguel Prz
  • 13,718
  • 29
  • 42
0

Use Net::SMTP instead

Here is a link already existing on how to use it in HTML format.

Net::SMTP using HTML

The same link also shows you how you can Use Mime::Lite.

Community
  • 1
  • 1
Harry Barry
  • 194
  • 7
  • thanks . Could you please explain me what are these Net::SMTP. As i am new to perl and net protocol. – SteveScm Jun 19 '13 at 07:43
  • @ 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 Jun 19 '13 at 08:08
  • @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=http://username:password@proxy_servername_or_IP:port" – Harry Barry Jun 19 '13 at 08:10
  • Just one ques, can't i use html with sendmail protocol ? – SteveScm Jun 19 '13 at 08:18
  • I am not sure, is sendmail not the old blat? I have not used it in a very long time. – Harry Barry Jun 19 '13 at 08:21
0

Many of modern smtp-servers use SSL Authentication

So you can use Net::SMTP::SSL

The code looks like

use Net::SMTP::SSL; 

my $to = 'tomail@server.com';
my $subject = 'Message subject';
my $message = '<h1>Hello</h1>';

my $user = 'USERLOGIN';
my $pass = 'USERPASSWORD';

my $server     = 'smtp.server.com';
my $from_name  = 'NAME';
my $from_email = 'userlogin@server.com';

my $smtps = Net::SMTP::SSL->new($server, Port => 465, DEBUG => 1) or warn "$!\n"; 

defined ($smtps->auth($user, $pass)) or die "Can't authenticate: $!\n";

$smtps->mail($from_email);
$smtps->to($to);
$smtps->data();
$smtps->datasend("To: $to\n");
$smtps->datasend(qq^From: "$from_name" <$from_email>\n^);
$smtps->datasend("Subject: $subject\n\n");
$smtps->datasend($message."\n");
$smtps->dataend();

if ($smtps->quit()) {
    print "Ok";
}
ABelikov
  • 613
  • 5
  • 10