CGI-Application-MailPage

 view release on metacpan or  search on metacpan

lib/CGI/Application/MailPage.pm  view on Meta::CPAN

    die "You must set either 'document_root' or 'remote_fetch' in PARAMS!"
        unless defined $self->param('document_root') || $self->param('remote_fetch');

    die "You must set 'your.smtp.server' in PARAMS!"
        unless defined $self->param('smtp_server');

    # default custom validation_profile is an empty hash
    $self->param(validation_profile => {} )
        unless defined($self->param('validation_profile'));
}

sub show_form {
    my ($self, $err_msgs) = @_;
    my $query = $self->query;

    my $page = $query->param('page');
    if (not defined $page) {
        unless($self->param('use_page_param')) {
            $page = $query->referer();
            return $self->error(
                "Sorry, I can't tell what page you want to send. " .
                "You need to be using either Netscape 4 or Internet Explorer 4 (or newer) " .
                "to use this feature. Please upgrade your browser and try again!"
            )
                unless defined $page;
        } else {
            return $self->error("no value for page param!") 
                unless defined $page;
        }
    }    

    my $template;
    if ($self->param('form_template')) {    
        $template = $self->load_tmpl($self->param('form_template'),
                                    die_on_bad_params   => 0,
                                    cache               => 1, 
                                    associate           => $query
        );
    
    } else {
        my @path = $self->tmpl_path;
        @path = @{ $self->tmpl_path} if(ref($path[0]) eq 'ARRAY');
        $template = $self->load_tmpl('CGI/Application/MailPage/templates/form.tmpl',
                                    die_on_bad_params   => 0,
                                    path                => [@path, @INC],
                                    cache               => 1,
                                    associate           => $query,
        );
    }

    my %formats = (
        both_attachment => 'Both Text and Full HTML as Attachments',
        html            => 'Full HTML',
        html_attachment => 'Full HTML as an Attachment',
        text            => 'Plain Text',
        text_attachment => 'Plain Text as an Attachment',
        url             => 'Just A Link',
    );
    #create the default dropdown menu
    $template->param(FORMAT_SELECTOR => 
                   $query->popup_menu(-name => 'format',
                                      '-values' => [ sort(keys %formats) ],
                                      -labels => \%formats,
                                      -default => 'both_attachment',
                   )
    );
    #create a loop that the user can use as they wish
    my @format_loop = ();
    foreach my $key (sort(keys(%formats))) {
        push(@format_loop, { value => $key, label => $formats{$key}});
    }
    $template->param(FORMAT_OPTIONS => \@format_loop);
  
    # set the default 'subject' as the email subject
    $query->param('subject' => $self->param('email_subject'))
        unless($query->param('subject'));
    # if we have any alerts or error messages
    if( $err_msgs && ref $err_msgs eq 'HASH' ) {
        $template->param(%$err_msgs);
    } 
    $template->param(%{$self->param('extra_tmpl_params')})
        if($self->param('extra_tmpl_params'));
    return $template->output();
}

sub send_mail {
    my $self = shift;
    my $query = $self->query;

    # the default validation profile
    my %validation_profile = (
        required    => [qw(
            name from_email to_emails format page subject
        )],
        optional    => [qw(note)],
        constraints => {
            name          => qr/^[\w '-\(\),\.]{1,50}$/,
            from_email    => 'email',
            to_emails     => sub {
                my @emails = split(/\s*,\s*|\s+/, shift);
                # make sure there aren't too many 
                # if we have 'max_emails_per_request'
                if( 
                    $self->param('max_emails_per_request')
                    && scalar @emails > $self->param('max_emails_per_request')
                ) {
                    return;
                }
                # check for valid email addresses
                foreach my $email (@emails) {
                    if(! Email::Valid->address($email) ) {
                        return;
                    }
                }
                return \@emails;
            },
            subject       => qr/^[\w '-\(\),\.\?\!]{1,50}$/,
            note          => qr/^[^\0]{1,250}$/,
            format        => sub {
                my $val = shift;
                my @valid_formats = qw(



( run in 1.212 second using v1.01-cache-2.11-cpan-364913b4093 )