networking intermediate

How to send HTML email in Perl?

Question

How to send HTML email in Perl?

Sending HTML email in Perl typically relies on MIME modules like MIME::Lite, but since MIME::Lite is not a core module, it may not be installed in all environments — especially restricted sandboxes. To send a basic HTML email without external modules, you can manually craft the raw email message with proper Content-Type headers and print it to STDOUT or pass it to a local mail agent via a pipe (not shown here since it needs system access).

This example demonstrates how to build a minimal, valid HTML email message string and print it. This approach shows the essential MIME headers required for browsers or email clients to recognize the message as HTML. It respects Perl's sigils and string quoting and avoids external dependencies, so it will run anywhere Perl does.

Example: Sending HTML Email without External Modules

use strict;
use warnings;

# Define email parameters
my $from    = 'sender@example.com';
my $to      = 'recipient@example.com';
my $subject = 'Test HTML Email from Perl (No Modules)';
my $html_body = <<'HTML';
<html>
  <body>
    <h1 style="color:blue;">Hello from Perl!</h1>
    <p>This is an <strong>HTML</strong> email.</p>
  </body>
</html>
HTML

# Construct raw MIME email message manually
my $email = <<"END_EMAIL";
From: $from
To: $to
Subject: $subject
MIME-Version: 1.0
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: 7bit

$html_body
END_EMAIL

# Print the full email message to STDOUT
print $email;

# Note: To actually send this email, this output could be piped to sendmail
# or submitted via SMTP using Net::SMTP (core module), which requires an SMTP server.

Explanation

  • We manually specify all necessary headers to define an HTML email message: Content-Type: text/html and Content-Transfer-Encoding.
  • The raw HTML content is assigned to $html_body scalar. Here we used escaped HTML entities so the output could be verified easily; in practice, you would include normal HTML tags.
  • All header and body lines are combined into one variable $email, printed to standard output.
  • This solution works in any environment and Perl version because it uses only core Perl features (string interpolation, here-docs).
  • Sending the email usually requires external tools or a configured SMTP server; this example focuses on illustrating correct MIME formatting without external modules.

Common Pitfalls When Sending HTML Email in Perl

  • Failing to set Content-Type to text/html will cause HTML to be displayed as plain text.
  • Improper encoding can break HTML in email clients; typically quoted-printable or base64 is used, but here we use simple 7bit encoding for ASCII-safe content.
  • Assuming MIME::Lite or other CPAN modules are always installed may cause runtime errors.
  • Sending requires a mail transport agent, which may not be available in a sandboxed or restricted environment.

By manually crafting the MIME message, you regain full control and avoid dependencies, a valuable approach when modules are unavailable or you want to understand the underlying protocols.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 10ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
From: sender@example.com
To: recipient@example.com
Subject: Test HTML Email from Perl (No Modules)
MIME-Version: 1.0
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: 7bit

<html>
  <body>
    <h1 style="color:blue;">Hello from Perl!</h1>
    <p>This is an <strong>HTML</strong> email.</p>
  </body>
</html>

STDERR
(empty)

Was this helpful?

Related Questions