Ian
Ian

Reputation: 13842

Sending using Email::Simple with To/From in body text instead of headers

Currently we are using a print to send an html mail via a sendmail pipe, which works fine.

$fh->open( q{|sendmail -f<fromaddress> .......} );

This comes from a template which contains text like...

From: <Fromaddress>
Reply-to: <replyaddress>
Subject: etc
Mine-Version: 1.o
Content-Type: text/html

<!DOCTYPE html...
<html...
rest of body

I am now trying to use Email::Simple with the same text

my $email = Email::simple->create(
    header => $header,
    body => $body
);

sendmail( $email, { transport => $transport });

My headers are like

[ To => <toaddress>, From => <fromaddress>, 'Content-Type' => 'text/html' ]

This all sends fine, except it includes also the 'From:.. To:: Subject..' as part of the html to be displayed also, whereas sendmail consumes this to be used.

I could use regex to strip out the header content that's included in the text (as it's already generated, but that feels risk prone and clunky), but I'm wondering, is there an Email::Simple way of discarding that, or using only the html instead of the From/To/Subject specific headers provided to Email::Simple ?

I have looked here but can't see any reference to that.

Upvotes: 1

Views: 187

Answers (2)

Ian
Ian

Reputation: 13842

Actually the answer is simpler than I thought...

If you just use

my $email = Email::Simple->new( $text );
sendmail( $email, { transport => $transport });

It will use the headers from the text/body, and not include them in the main body.

If you use

my $email = Email::Simple->create( $text );
sendmail( $email, { transport => $transport });

It seems to need the separate headers, and sends out the headers in the text as part of the main data/html that's sent out.

Upvotes: 2

Dave Cross
Dave Cross

Reputation: 69224

I assume your $body variable contains the text from one of your template files. And as those templates contain the headers, they will end up duplicated in the body of your email messages.

The best approach is to edit your templates to remove the header sections.

But if you can't do that for some reason, you can pre-process $body before passing it to the create() method. Something like this, perhaps:

(undef, $body) = split /\n\n/, $body, 2;

Upvotes: 2

Related Questions