Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send an HTML email with Perl?

Tags:

html

email

perl

I am trying to send an HTML email using Perl.

 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 "Content-Type: text/html; charset=ISO-8859-1\n\n"
        . "<html><head></head><body>@emailBody";
 close(MAIL)

Is that the correct way of doing it? It is not working for some reason. Thanks for your help.

like image 819
AlexBrand Avatar asked Apr 12 '10 16:04

AlexBrand


2 Answers

Start with Email::Sender::Simple or Email::Sender.
There is a quickstart guide in CPAN, and Ricardo wrote a good use-me in his 2009 advent calendar

From the quickstart guide:

  use strict;
  use Email::Sender::Simple qw(sendmail);
  use Email::Simple;
  use Email::Simple::Creator;

  my $email = Email::Simple->create(
    header => [
      To             => '"Xavier Q. Ample" <[email protected]>',
      From           => '"Bob Fishman" <[email protected]>',
      Subject        => "don't forget to *enjoy the sauce*",
      'Content-Type' => 'text/html', 
    ],
    body => "<p>This message is short, but at least it's cheap.</p>",
  );
  sendmail($email);
like image 77
spazm Avatar answered Oct 03 '22 14:10

spazm


The content type should be part of the mail header. Right now it's part of the mail body. The header is separated from the body by a double newline. So, removing the second newline after the subject header should fix the problem of content type not being correctly interpreted.

like image 45
BalusC Avatar answered Oct 03 '22 14:10

BalusC