Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!perl -w
- ################################################################################
- #
- # Simple Perl script to crop and mount a HTML template to be sent via email
- #
- # Argument description
- #
- # --filename Image path to be cropped
- #
- # --clientUrl Complete host uri where the images would be hosted
- #
- # --redirectUrl Url to redirect the user when clicking the mail mkt
- #
- # --title Mail title
- #
- # Usage:
- #
- # perl mkt.pl --filename=image.png --clientUrl="//client.host.url" --title="Some Title" [--redirect="//some.valid.url"]
- #
- use 5.10.0;
- use strict;
- use utf8;
- use POSIX;
- use Getopt::Long;
- use File::Slurp qw/read_file write_file/;
- use Data::Dumper qw/Dumper/;
- my %data = (
- -title => '',
- -filename => 'mkt.png',
- -clientUrl => undef,
- -redirectUrl => undef,
- -templateTr => '
- <tr>
- <td>
- <img src="#image#">
- </td>
- </tr>',
- );
- GetOptions(
- "filename=s" => \$data{-filename},
- "redirectUrl=s" => \$data{-redirectUrl},
- "clientUrl=s" => \$data{-clientUrl},
- "title=s" => \$data{-title},
- );
- ##
- ## Check for file
- print 'File not found: '.$data{-filename}, "\n" and exit if ! -e $data{-filename};
- mountHtml();
- ##
- ## Mount HTML
- sub mountHtml {
- my $template = join "", <DATA>;
- splitImage($data{-filename});
- my @images = glob "mkt/*.png";
- my $contentTrs;
- foreach(@images) {
- my $imgTemplate = $data{-templateTr};
- my $imageUrl = $data{-clientUrl}.'/'.$_;
- $imgTemplate =~ s{\#image\#}{$imageUrl}iem;
- $contentTrs .= $imgTemplate;
- }
- ##
- ## rows and images
- $template =~ s{\#content\#}{$contentTrs}iem;
- ##
- ## title
- my $title = $data{-title};
- $template =~ s{\#title\#}{$title}iem;
- ##
- ## redirect link
- my $redirectLink = 'onclick="window.open('.$data{-redirectUrl} .')"' if defined $data{-redirectUrl};
- $template =~ s{\#link\#}{$redirectLink}iem if defined $data{-redirectUrl};
- $template =~ s{\#link\#}{} if not defined;
- write_file('mkt.html', $template);
- }
- ##
- ## Split image into equal parts
- sub splitImage {
- my ($filename) = @_;
- `mkdir mkt` if !-d 'mkt';
- `convert $filename -crop 100%x25% +repage mkt/%02d.png`;
- }
- __DATA__
- <html>
- <head>
- <title>#TITLE#</title>
- </head>
- <body #link#>
- <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse; border: 0px;">
- #CONTENT#
- </table>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement