| 1 | ################################################################################### | ||
| 2 | # | ||
| 3 | # Embperl - Copyright (c) 1997-2008 Gerald Richter / ecos gmbh www.ecos.de | ||
| 4 | # Embperl - Copyright (c) 2008-2015 Gerald Richter | ||
| 5 | # Embperl - Copyright (c) 2015-2023 actevy.io | ||
| 6 | # | ||
| 7 | # You may distribute under the terms of either the GNU General Public | ||
| 8 | # License or the Artistic License, as specified in the Perl README file. | ||
| 9 | # | ||
| 10 | # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR | ||
| 11 | # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | ||
| 12 | # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
| 13 | # | ||
| 14 | ################################################################################### | ||
| 15 | |||
| 16 | |||
| 17 | package Embperl::Mail ; | ||
| 18 | |||
| 19 | |||
| 20 | require Embperl ; | ||
| 21 | require Embperl::Constant ; | ||
| 22 | |||
| 23 | |||
| 24 | use strict ; | ||
| 25 | use vars qw( | ||
| 26 | @ISA | ||
| 27 | $VERSION | ||
| 28 | ) ; | ||
| 29 | |||
| 30 | no warnings "uninitialized" ; | ||
| 31 | |||
| 32 | |||
| 33 | @ISA = qw(Embperl); | ||
| 34 | |||
| 35 | |||
| 36 | $VERSION = '3.0.1'; | ||
| 37 | |||
| 38 | |||
| 39 | sub _quote_hdr | ||
| 40 | { | ||
| 41 | my $chunk = shift; | ||
| 42 | my $encoding = shift ; | ||
| 43 | |||
| 44 | return $chunk unless ($encoding && ($chunk =~ /[\x80-\xff]/)) ; | ||
| 45 | |||
| 46 | $chunk =~ s{ | ||
| 47 | ([^0-9A-Za-z]) | ||
| 48 | }{ | ||
| 49 | join("" => map {sprintf "=%02X", $_} unpack("C*", $1)) | ||
| 50 | }egox; | ||
| 51 | return "=?$encoding?Q?$chunk?="; | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | sub Execute | ||
| 59 | |||
| 60 | { | ||
| 61 | my ($req) = @_ ; | ||
| 62 | |||
| 63 | my $data ; | ||
| 64 | my @errors ; | ||
| 65 | |||
| 66 | $req -> {options} ||= &Embperl::Constant::optKeepSpaces | &Embperl::Constant::optReturnError; | ||
| 67 | |||
| 68 | $req -> {syntax} ||= 'EmbperlBlocks' ; | ||
| 69 | $req -> {escmode} ||= 0 ; | ||
| 70 | $req -> {output} = \$data ; | ||
| 71 | $req -> {errors} ||= \@errors ; | ||
| 72 | |||
| 73 | if ($Embperl::req) | ||
| 74 | { | ||
| 75 | $Embperl::req -> execute_component ($req) ; | ||
| 76 | } | ||
| 77 | else | ||
| 78 | { | ||
| 79 | Embperl::Execute ($req) ; | ||
| 80 | } | ||
| 81 | |||
| 82 | die "@errors" if (@errors) ; | ||
| 83 | |||
| 84 | eval | ||
| 85 | { | ||
| 86 | require Net::SMTP ; | ||
| 87 | |||
| 88 | $req -> {mailhost} ||= $ENV{'EMBPERL_MAILHOST'} || 'localhost' ; | ||
| 89 | |||
| 90 | my $helo = $req -> {mailhelo} || $ENV{'EMBPERL_MAILHELO'} ; | ||
| 91 | |||
| 92 | my $smtp = Net::SMTP->new($req -> {mailhost}, | ||
| 93 | Debug => ($req -> {maildebug} || $ENV{'EMBPERL_MAILDEBUG'} || 0), | ||
| 94 | ($helo?(Hello => $helo):()) | ||
| 95 | ) or die "Cannot connect to mailhost $req->{mailhost}" ; | ||
| 96 | |||
| 97 | my $from = $req -> {from} || $ENV{'EMBPERL_MAILFROM'} || 'WWW-Server\@' . ($ENV{SERVER_NAME} || 'localhost') ; | ||
| 98 | $smtp->mail($from); | ||
| 99 | |||
| 100 | my $to ; | ||
| 101 | if (ref ($req -> {'to'})) | ||
| 102 | { | ||
| 103 | $to = $req -> {'to'} ; | ||
| 104 | } | ||
| 105 | else | ||
| 106 | { | ||
| 107 | $to = [] ; | ||
| 108 | @$to = split (/\s*;\s*/, $req -> {'to'}) ; | ||
| 109 | } | ||
| 110 | |||
| 111 | my $cc ; | ||
| 112 | if (ref ($req -> {'cc'})) | ||
| 113 | { | ||
| 114 | $cc = $req -> {'cc'} ; | ||
| 115 | } | ||
| 116 | else | ||
| 117 | { | ||
| 118 | $cc = [] ; | ||
| 119 | @$cc = split (/\s*;\s*/, $req -> {'cc'}) if ($req -> {'cc'}) ; | ||
| 120 | } | ||
| 121 | |||
| 122 | my $bcc ; | ||
| 123 | if (ref ($req -> {'bcc'})) | ||
| 124 | { | ||
| 125 | $bcc = $req -> {'bcc'} ; | ||
| 126 | } | ||
| 127 | else | ||
| 128 | { | ||
| 129 | $bcc = [] ; | ||
| 130 | @$bcc = split (/\s*;\s*/, $req -> {'bcc'}) if ($req -> {'bcc'}) ; | ||
| 131 | } | ||
| 132 | |||
| 133 | my $enc = $req->{headerencoding} || 'iso-8859-1'; | ||
| 134 | my $headers = $req->{mailheaders} ; | ||
| 135 | $smtp -> to (@$to, @$cc, @$bcc) ; | ||
| 136 | |||
| 137 | $smtp->data() or die "smtp data failed" ; | ||
| 138 | $smtp->datasend("Reply-To: " . _quote_hdr($req->{'reply-to'}, $enc) . "\n") or die "smtp data failed" if ($req->{'reply-to'}) ; | ||
| 139 | $smtp->datasend("From: " . _quote_hdr($from, $enc) . "\n") if ($from) ; | ||
| 140 | $smtp->datasend("To: " . _quote_hdr(join (', ', @$to), $enc) . "\n") or die "smtp datasend failed" ; | ||
| 141 | $smtp->datasend("Cc: " . _quote_hdr(join (', ', @$cc), $enc) . "\n") or die "smtp datasend failed" if ($req -> {'cc'}) ; | ||
| 142 | $smtp->datasend("Subject: " . _quote_hdr($req->{subject}, $enc) . "\n") or die "smtp datasend failed" ; | ||
| 143 | $smtp->datasend("Date: " . _quote_hdr(Embperl::get_date_time(), $enc) . "\n") or die "smtp datasend failed" ; | ||
| 144 | if (ref ($headers) eq 'ARRAY') | ||
| 145 | { | ||
| 146 | foreach (@$headers) | ||
| 147 | { | ||
| 148 | next unless (/^(.*?):\s*(.*?)$/) ; | ||
| 149 | $smtp->datasend("$1: " . _quote_hdr($2, $enc) . "\n") or die "smtp datasend failed" ; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | $smtp->datasend("\n") or die "smtp datasend failed" ; | ||
| 153 | # make sure we have only \n line endings (is made to \r\n by Net::SMTP) | ||
| 154 | $data =~ s/\r//g ; | ||
| 155 | $smtp->datasend($data) or die "smtp datasend failed" ; | ||
| 156 | $smtp->quit or die "smtp quit failed" ; | ||
| 157 | } ; | ||
| 158 | |||
| 159 | if ($@) | ||
| 160 | { | ||
| 161 | die "$@" if (ref ($req -> {errors}) eq \@errors) ; | ||
| 162 | |||
| 163 | push @{$req -> {errors}}, $@ ; | ||
| 164 | } | ||
| 165 | |||
| 166 | return ref ($req -> {errors})?@{$req -> {errors}}:0 ; | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | __END__ |