Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I have a Perl Module that sends email no problem, I am trying to this current module add the ability to send HMTL email, can anyone show me how I could add this feature to this module?
I added a line there but need to see if this is the best way to the current code I already have.
Thanks a lot!!!

sub my_sendmail { my ($to, $subject, $msg_body, $from_addr, $from_name, $to_name) = @_ +; my $input; # Make sure variables are set up correctly (ref($to) eq "ARRAY") || die "Argument \$to must be a reference to a +n array"; $subject = "(No subject)" unless ($subject); # Add the first email address to the To: name $to_name = ($to_name ? "$to_name " : ''); $to_name .= "<$to->[0]>"; # Use default 'from' address if not defined unless ($from_addr) { $from_addr = $default_from_addr; $from_name = $default_from_name; } # Add 'from' address to name. Some ISP's require a valid email in ' +From:' $from_name .= " <$from_addr>"; warn "<pre>\n" if ($sendmail_debug); warn "Calling SMTP server.\n" if ($sendmail_debug); my $sock = IO::Socket::INET->new( PeerAddr => $mail_server, Type => SOCK_STREAM, PeerPort => 'smtp(25)', Proto => 'tcp'); unless ($sock) { warn "Can't open a port on the mail server ($mail_server)."; return 0; } $input = <$sock>; warn "$input\r\n" if ($sendmail_debug); print $sock "HELO localhost\r\n"; $input = <$sock>; warn "$input\r\n" if ($sendmail_debug); print $sock "mail from:<$from_addr>\r\n"; $input = <$sock>; warn "$input\r\n" if ($sendmail_debug); # Send to each recipient here. They won't see the others' addresses my $rcpt; foreach $rcpt (@$to) { print $sock "rcpt to:<$rcpt>\r\n"; $input = <$sock>; warn "$input\r\n" if ($sendmail_debug); } print $sock "DATA\r\n"; print $sock "Subject: $subject\r\n"; print $sock "From: $from_name\r\n"; print $sock "Reply-To: $from_addr\r\n"; print $sock "To: $to_name\r\n"; print $sock "\r\n"; #test line to add HTML to the emails print $sock "Content-type: text/plain\n\n"; print $sock "$msg_body\r\n"; print $sock ".\r\n"; $input = <$sock>; warn "$input\r\n" if ($sendmail_debug); print $sock "QUIT"; close ($sock); return 1; } # End of my_sendmail() 1; # End of My Sendmail.pm __END__

Replies are listed 'Best First'.
Re: Sending HTML emails HELP!
by zentara (Cardinal) on Nov 25, 2008 at 18:06 UTC
    I think you may need some boundaries and content-type headers set right. MIME::Lite does it, and you can get the whole html mail as_string() and send it as your data. Otherwise, look at this

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Sending HTML emails HELP!
by oeuftete (Monk) on Nov 25, 2008 at 17:07 UTC

    Looks like Email::MIME::CreateHTML would do what you want quite easily, although it wouldn't do strictly what you're asking since it wouldn't require your module at all.

      Ware of those Modules, but need to use the current code, thanks!

        I recommend you use MIME::Base64 to base64-encode the HTML, and include the "content-encoding: base64" header.

        Otherwise, you will find that the HTML gets some extra newline characters where you don't want them (i.e. - in a hyperlink or image source attribute).