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='[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.

3

4 Answers 4

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.
Sign up to request clarification or add additional context in comments.

Comments

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

6 Comments

could you please explain this line: MIME::Lite->new(
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>
you need to install the CPAN MIME::Lite module, and then use it. See the link I provided
could i check on my linux machine whether it is installed or not. Any command
run "ppm install MIME::Lite" if it is installed it will skip, if not, it will install it.
|
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.

5 Comments

thanks . Could you please explain me what are these Net::SMTP. As i am new to perl and net protocol.
@ 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.
@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"
Just one ques, can't i use html with sendmail protocol ?
I am not sure, is sendmail not the old blat? I have not used it in a very long time.
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 = '[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";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.