Linto P D
Linto P D

Reputation: 9175

How can i send html content in my email,using perl

I am new to Perl.

Iam using following perl code for sending mail.

And its working fine.

But how can i include html content in it?

use strict;
use warnings;

use Email::Sender::Simple qw(sendmail);
use Email::Sender::Transport::SMTP ();
use Email::Simple ();
use Email::Simple::Creator ();

my $smtpserver = 'xxxx.xx.x.x.';
my $smtpport = 25;
my $smtpuser   = '[email protected]';
my $smtppassword = 'xxxxx';

my $transport = Email::Sender::Transport::SMTP->new({
  host => $smtpserver,
  port => $smtpport,
  sasl_username => $smtpuser,
  sasl_password => $smtppassword,
});

my $email = Email::Simple->create(
  header => [
    To      => '[email protected]',
    From    => '[email protected]',
    Subject => 'Hi!'

  ],
  body => "<table><tr><td>try</td></tr></table>",
);

sendmail($email, { transport => $transport });
print "Success \n";
1;

Upvotes: 0

Views: 4658

Answers (1)

Dave Cross
Dave Cross

Reputation: 69314

The joy of using modules from the Email::* namespace is that they all work together nicely. Currently you're using Email::Simple to create the email and Email::Sender to send it. But now you no longer want to create simple email, you want to create more complex ones. The format for sending complex email like this is MIME. So you should look for something in the Email::MIME namespace that you can plug in in place of Email::Simple. Email::MIME::CreateHTML looks like it's exactly what you want.

From the Synopsis:

use Email::MIME::CreateHTML;
my $email = Email::MIME->create_html(
        header => [
                From => 'my@address',
                To => 'your@address',
                Subject => 'Here is the information you requested',
        ],
        body => $html,
        text_body => $plain_text
);

use Email::Send;
my $sender = Email::Send->new({mailer => 'SMTP'});
$sender->mailer_args([Host => 'smtp.example.com']);
$sender->send($email);

It looks like this would be simple enough to convert to your usage.

Upvotes: 5

Related Questions