Boardy Boardy - 2 months ago 10
Perl Question

Create Email object and update body_str afterwards in Perl

I am trying to update a script so that it can send an email using the

object.

I create email object, and then later on update the body of the message, as the message body can change depending on different circumstances.

I have the following code

my $message = Email::MIME->create(
header_str => [
From => 'someone@example.com',
To => 'someoneelse@example.com',
Subject => 'This is the subject'
],
attributes => {
encoding => 'quoted-printable',
charset => 'ISO-8859-1'
}
);

if ( $i > 1 ) {
$message->body_str = "Here's one message";
}
else {
$message->body_str = "Here's seconds message";
}


When I do the above I get the following error:


Can't modify non-lvalue subroutine call


The error line is referencing where I am updating
body_str

Answer

$message->body_str is a get method, not set.

What you are looking for is body_set or probably body_str_set.

#!/usr/bin/perl
use strict;
use warnings;
use Email::MIME;
my $message = Email::MIME->create(
    header_str => [
        From => 'someone@example.com',
        To  => 'someoneelse@example.com',
        Subject => 'This is the subject'
    ],
    attributes => {
        encoding => 'quoted-printable',
        charset => 'ISO-8859-1'
    }
);
#put your conditionals here
print $message->body_set('Blah');
Comments