| 1 | # $Id: Sendmail.pm,v 1.15 2005/04/13 16:04:21 kjetil Exp $ | ||
| 2 | |||
| 3 | package AxKit::XSP::Sendmail; | ||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 0 | ▶ | use Apache::AxKit::Language::XSP; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use Mail::Sendmail; | ||
| 7 | use Email::Valid; | ||
| 8 | use Carp; | ||
| 9 | use Apache::AxKit::CharsetConv; | ||
| 10 | |||
| 11 | use vars qw/@ISA $NS $VERSION $ForwardXSPExpr $TRIM_FIELD/; | ||
| 12 | |||
| 13 | @ISA = ('Apache::AxKit::Language::XSP'); | ||
| 14 | $NS = 'http://axkit.org/NS/xsp/sendmail/v1'; | ||
| 15 | |||
| 16 | $VERSION = "1.5"; | ||
| 17 | |||
| 18 | ## Taglib subs | ||
| 19 | |||
| 20 | # send mail | ||
| 21 | sub send_mail { | ||
| 22 | my ($document, $parent, $mailer_args) = @_; | ||
| 23 | my $address_errors; | ||
| 24 | |||
| 25 | foreach my $addr_type ('To', 'Cc', 'Bcc') { | ||
| 26 | if ($mailer_args->{$addr_type}) { | ||
| 27 | foreach my $addr (@{$mailer_args->{$addr_type}}) { | ||
| 28 | next if Email::Valid->address($addr); | ||
| 29 | $address_errors .= "Address $addr in '$addr_type' element failed $Email::Valid::Details check. "; | ||
| 30 | } | ||
| 31 | $mailer_args->{$addr_type} = join (', ', @{$mailer_args->{$addr_type}}); | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | # we want a bad "from" header to be caught as a user error so we'll trap it here. | ||
| 36 | $mailer_args->{From} ||= $Mail::Sendmail::mailcfg{from}; | ||
| 37 | |||
| 38 | unless ( Email::Valid->address($mailer_args->{From}) ) { | ||
| 39 | $address_errors .= "Address '$mailer_args->{From}' in 'From' element failed $Email::Valid::Details check. "; | ||
| 40 | } | ||
| 41 | |||
| 42 | # set the content-type | ||
| 43 | $mailer_args->{'Content-Type'} = ($mailer_args->{'Content-Type'})? $mailer_args->{'Content-Type'} : 'text/plain'; | ||
| 44 | $mailer_args->{'Content-Type'} .= '; charset='; | ||
| 45 | $mailer_args->{'Content-Type'} .= ($mailer_args->{'charset'})? $mailer_args->{'charset'} : 'utf-8'; | ||
| 46 | |||
| 47 | # munge the text if it needs to be | ||
| 48 | if ($mailer_args->{'charset'} and lc($mailer_args->{'charset'}) ne 'utf-8') { | ||
| 49 | my $conv = Apache::AxKit::CharsetConv->new('utf-8',$mailer_args->{'charset'}) | ||
| 50 | or croak "No such charset: $mailer_args->{'charset'}"; | ||
| 51 | $mailer_args->{'message'} = $conv->convert($mailer_args->{'message'}); | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | if ($address_errors) { | ||
| 56 | croak "Invalid Email Address(es): $address_errors"; | ||
| 57 | } | ||
| 58 | |||
| 59 | # all addresses okay? if so, send. | ||
| 60 | |||
| 61 | sendmail( %{$mailer_args} ) || croak $Mail::Sendmail::error; | ||
| 62 | } | ||
| 63 | |||
| 64 | ## Parser subs | ||
| 65 | |||
| 66 | sub parse_start { | ||
| 67 | my ($e, $tag, %attribs) = @_; | ||
| 68 | #warn "Checking: $tag\n"; | ||
| 69 | |||
| 70 | # check for trimming | ||
| 71 | $TRIM_FIELD = ($attribs{trim} eq 'no' ? 0 : 1); | ||
| 72 | |||
| 73 | if ($tag eq 'send-mail') { | ||
| 74 | return qq| {# start mail code\n | . | ||
| 75 | q| my (%mail_args, @to_addrs, @cc_addrs, @bcc_addrs);| . qq|\n|; | ||
| 76 | } | ||
| 77 | elsif ($tag eq 'to') { | ||
| 78 | return q| push (@to_addrs, ''|; | ||
| 79 | } | ||
| 80 | elsif ($tag eq 'cc') { | ||
| 81 | return q| push (@cc_addrs, ''|; | ||
| 82 | } | ||
| 83 | elsif ($tag eq 'bcc') { | ||
| 84 | return q| push (@bcc_addrs, ''|; | ||
| 85 | } | ||
| 86 | elsif ($tag eq 'content-type') { | ||
| 87 | return q| $mail_args{'Content-Type'} = ''|; | ||
| 88 | } | ||
| 89 | elsif ($tag eq 'content-transfer-encoding') { | ||
| 90 | return q| $mail_args{'Content-Transfer-Encoding'} = ''|; | ||
| 91 | } | ||
| 92 | elsif ($tag eq 'charset') { | ||
| 93 | return q| $mail_args{'charset'} = ''|; | ||
| 94 | } | ||
| 95 | elsif ($tag =~ /^(subject|message|from|body)$/) { | ||
| 96 | $tag = "From" if $tag eq 'from'; | ||
| 97 | $tag = "message" if $tag eq 'body'; | ||
| 98 | return qq| \$mail_args{'$tag'} = "" |; | ||
| 99 | } | ||
| 100 | elsif ($tag eq 'smtphost') { | ||
| 101 | return q| $mail_args{'smtp'} = "" |; | ||
| 102 | } | ||
| 103 | elsif ($tag eq 'header') { | ||
| 104 | return qq| \$mail_args{'$attribs{name}'} = ''|; | ||
| 105 | } | ||
| 106 | else { | ||
| 107 | die "Unknown sendmail tag: $tag"; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | sub parse_char { | ||
| 112 | my ($e, $text) = @_; | ||
| 113 | my $element_name = $e->current_element(); | ||
| 114 | |||
| 115 | |||
| 116 | if ($element_name ne 'body' and $TRIM_FIELD) { | ||
| 117 | $text =~ s/^\s*//; | ||
| 118 | $text =~ s/\s*$//; | ||
| 119 | } | ||
| 120 | |||
| 121 | return '' unless $text; | ||
| 122 | |||
| 123 | $text =~ s/\|/\\\|/g; | ||
| 124 | $text =~ s/\\$/\\\\/gsm; | ||
| 125 | return " . q|$text| "; | ||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | sub parse_end { | ||
| 130 | my ($e, $tag) = @_; | ||
| 131 | |||
| 132 | $TRIM_FIELD = 1; | ||
| 133 | |||
| 134 | if ($tag eq 'send-mail') { | ||
| 135 | return <<'EOF'; | ||
| 136 | AxKit::XSP::Sendmail::send_mail( | ||
| 137 | $document, $parent, | ||
| 138 | { | ||
| 139 | %mail_args, | ||
| 140 | To => \@to_addrs, | ||
| 141 | Cc => \@cc_addrs, | ||
| 142 | Bcc => \@bcc_addrs, | ||
| 143 | }, | ||
| 144 | ); | ||
| 145 | } # end mail code | ||
| 146 | EOF | ||
| 147 | } | ||
| 148 | elsif ($tag =~ /to|bcc|cc/) { | ||
| 149 | return ");\n"; | ||
| 150 | } | ||
| 151 | return ";"; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub parse_comment { | ||
| 155 | # compat only | ||
| 156 | } | ||
| 157 | |||
| 158 | sub parse_final { | ||
| 159 | # compat only | ||
| 160 | } | ||
| 161 | |||
| 162 | 1; | ||
| 163 | |||
| 164 | __END__ | ||