Search code examples
pluginshtml-emailcatalyst

Send html in email with Catalyst and Catalyst::Plugin::Email


I have been reading about Catalyst framework and I am trying to send an email with HTML content without success.

I tried to use Catalyst::Plugin::Email, just as the example here. The email is sent, but all the content is showed in plain text.

sub send_email : Local {
    my ($self, $c) = @_;

    $c->email(
      header => [
        To      => 'me@localhost',
        Subject => 'A TT Email',
      ],
      body => $c->view('Web')->render($c, 'email.tt', {
          additional_template_paths => [ $c->config->{root} . '/email_templates'],
          email_tmpl_param1 => 'foo'
        }
      ),
    );
    # Redirect or display a message
}

I also read about Catalyst::View::Email::Template, but I chouldn't install it.

Any idea?


Solution

  • I am now available to send HTML emails with Catalyst::Plugin::Email. From documentation:

    "email() accepts the same arguments as Email::MIME::Creator's create()."

    Looking into Email::MIME::Creator, the create method structure is:

    my $single = Email::MIME->create(
        header_str => [ ... ],
        body_str   => '...',
        attributes => { ... },
    );
    
    my $multi = Email::MIME->create(
        header_str => [ ... ],
        parts      => [ ... ],
        attributes => { ... },
    );
    

    "Back to attributes. The hash keys correspond directly to methods or modifying a message from Email::MIME::Modifier. The allowed keys are: content_type, charset, name, format, boundary, encoding, disposition, and filename. They will be mapped to "$attr_set" for message modification."

    This is the code it is working:

    sub send_email : Local {
        my ($self, $c) = @_;
    
        $c->email(
          header => [
            To      => 'me@localhost',
            Subject => 'A TT Email',
          ],
          body => $c->view('Web')->render($c, 'email.tt', {
              additional_template_paths => [ $c->config->{root} . '/email_templates'],
              email_tmpl_param1 => 'foo'
            }
          ),
          attributes => {
            content_type => 'text/html',
            charset => 'utf-8'
          },
        );
    
        # Redirect or display a message
    }