使用data :: dumper在终端上打印出电子邮件

时间:2018-04-11 15:44:50

标签: perl email data-dumper

即使在阅读Perl文档并查看git中的其他脚本后,我也不了解如何使用Data :: Dumper。我在网上看到很多关于哈希的例子,但我并不认为这与我需要做的事情相符。

我正在创建一个脚本,以便向经理或团队发送有关已终止员工的电子邮件。我被告知要在我的代码中添加print Dumper $email,以便在使用--dry_run选项时,我们可以在终端上看到电子邮件的外观打印输出。 --dry_run还会确保电子邮件未实际发送。当我运行perl <script> --dry_run时,没有任何反应。也许我需要按照$d = Data::Dumper->new(

的方式做点什么

以下是我的代码片段:

#!/usr/bin/perl
use strict;
use warnings;
use NIE::Email;
use Data::Dumper;
use List::Util qw(any);
use Getopt::Long;
Getopt::Long::Configure qw(gnu_getopt);
my ($qa, $verbose, $dry_run, $help, $dbh);

GetOptions(
    'qa' => \$qa,
    'verbose|v' => \$verbose,
    'dry_run' => \$dry_run,
    'help|h' => \$help
);

#Generate email here
sub mail_func {
    print "Prepare email\n" if $verbose;
    my $n = shift;    #user
    my $i = shift;    #ips
    my $t = shift;    #testnets
    my $m = shift;    #managers (multiple if owner is undef)
    my @to_list;      # send to field
    foreach my $value (values %{$t}) {
        if ($value ne 'lab@abc.com') {    #don't send this email to lab@
            if (any { $value eq $_ } @to_list) { #check not already listed
                next;
            }
            else { push(@to_list, $value); }
        }
    }
    foreach my $key (keys %{$m}) {
        if ($key ne 'def') {
            if (any { $key eq $_ } @to_list) {
                next;
            }
            else { push(@to_list, $key . '@abc.com'); }
        }
    }
    my @body;
    while (my ($key, $value) = each %{$i}) {
        my $b = "IP " . $key . " : Testnet " . $value . "\n";
        push(@body, $b);
    }
    my $sub1 = "Ownership needed!";
    my $sub2 = "Ownership needed script special case";
    my $email;

    #Email testnet group (if not lab) as well as manager of term employee
    if (@to_list) {
        $email = NIE::Email->new(
            From       => 'do-not-reply@abc.com',
            To         => join(',', @to_list),
            'Reply-to' => 'def@abc.com',
            Subject    => $sub1,
            );
        $email->data(
            "Good Day, \n\n The below machines need claimed as their previous"
              . " owner, $n, is showing up as no longer with the company. \n"
              . "Please visit website to change"
              . " ownership of these machhines. \n\n"
              . "@body \n\n"
              . "If you have already requested an ownership change for these"
              . " machines, please disregard this message."
              . "\n\n Thank you \n -Lab team \n\n"
              . "This script is under active development and could contain"
              . " bugs, so please speak up if you have doubts or something "
              . "looks strange."
              . "\n Script name: lab_ownership_needed_email");
        if ($dry_run) {print Dumper($email);}
        else {$email->send();}
    }

任何帮助理解如何将此用于我的目的将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

恢复原状,在代码中重新添加,重新运行脚本,然后就可以了。 上面的代码是正确的。 感谢simbabque,他们首先说代码看起来是正确的。