SteveScm
SteveScm

Reputation: 565

Send mail in html format from perl script

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.

Upvotes: 4

Views: 12512

Answers (4)

ABelikov
ABelikov

Reputation: 612

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 = '[email protected]';
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 = '[email protected]';

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";
}

Upvotes: 0

Dave Cross
Dave Cross

Reputation: 69314

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.

Upvotes: 2

Miguel Prz
Miguel Prz

Reputation: 13792

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();

Upvotes: 1

Harry Barry
Harry Barry

Reputation: 194

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.

Upvotes: 0

Related Questions