Skip to content

Instantly share code, notes, and snippets.

@tommybutler
Last active June 26, 2020 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommybutler/7b92e52b07a608de0c303f6b1ca23482 to your computer and use it in GitHub Desktop.
Save tommybutler/7b92e52b07a608de0c303f6b1ca23482 to your computer and use it in GitHub Desktop.
Email a PDF with Mojolicious::Plugin::Mail, and PDF::WebKit
my $pdf = 'my-html-template-for-conversion-to-pdf'; # needs to be a .html.ep template
my $mail = 'my-template-for-the-html-email'; # needs to be a .mail.ep template
$pdf = $c->render_to_string( $pdf )->to_string();
$pdf = PDF::WebKit->new( \$pdf, %pdf_opt )->to_pdf(); # see PDF::WebKit documentation for details
$c->mail
(
mail =>
{
To => $recipients,
From => $from,
ReplyTo => $reply_to,
Type => 'multipart/mixed',
Subject => $mail_subject,
},
attach =>
[
{
Type => 'text/html',
Data => $c->render_mail( $mail ),
},
{
Type => 'application/pdf',
Filename => 'my-pdf-file.pdf',
Disposition => 'attachment',
Data => $pdf,
},
]
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment