1 package LWP::Protocol::mailto;
3 # This module implements the mailto protocol. It is just a simple
4 # frontend to the Unix sendmail program except on MacOS, where it uses
9 require HTTP::Response;
14 use vars qw(@ISA $SENDMAIL);
16 @ISA = qw(LWP::Protocol);
18 unless ($SENDMAIL = $ENV{SENDMAIL}) {
19 for my $sm (qw(/usr/sbin/sendmail
29 die "Can't find the 'sendmail' program" unless $SENDMAIL;
34 my($self, $request, $proxy, $arg, $size) = @_;
36 my ($mail, $addr) if $^O eq "MacOS";
37 my @text = () if $^O eq "MacOS";
42 return new HTTP::Response &HTTP::Status::RC_BAD_REQUEST,
43 'You can not proxy with mail';
47 my $method = $request->method;
49 if ($method ne 'POST') {
50 return new HTTP::Response &HTTP::Status::RC_BAD_REQUEST,
51 'Library does not allow method ' .
52 "$method for 'mailto:' URLs";
56 my $url = $request->url;
58 my $scheme = $url->scheme;
59 if ($scheme ne 'mailto') {
60 return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
61 "LWP::Protocol::mailto::request called for '$scheme'";
65 require Mail::Internet;
68 return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
69 "You don't have MailTools installed";
71 unless ($ENV{SMTPHOSTS}) {
72 return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
73 "You don't have SMTPHOSTS defined";
77 unless (-x $SENDMAIL) {
78 return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
79 "You don't have $SENDMAIL";
83 $mail = Mail::Internet->new or
84 return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
85 "Can't get a Mail::Internet object";
88 open(SENDMAIL, "| $SENDMAIL -oi -t") or
89 return new HTTP::Response &HTTP::Status::RC_INTERNAL_SERVER_ERROR,
90 "Can't run $SENDMAIL: $!";
93 $addr = $url->encoded822addr;
96 $request = $request->clone; # we modify a copy
97 my @h = $url->headers; # URL headers override those in the request
101 next unless defined $v;
102 if (lc($k) eq "body") {
103 $request->content($v);
106 $request->push_header($k => $v);
110 if ($^O eq "MacOS") {
111 $mail->add(To => $addr);
112 $mail->add(split(/[:\n]/,$request->headers_as_string));
115 print SENDMAIL $request->headers_as_string;
118 my $content = $request->content;
119 if (defined $content) {
120 my $contRef = ref($content) ? $content : \$content;
121 if (ref($contRef) eq 'SCALAR') {
122 if ($^O eq "MacOS") {
123 @text = split("\n",$$contRef);
129 print SENDMAIL $$contRef;
133 elsif (ref($contRef) eq 'CODE') {
134 # Callback provides data
136 if ($^O eq "MacOS") {
138 while (length($d = &$contRef)) {
141 @text = split("\n",$stuff);
151 if ($^O eq "MacOS") {
153 unless ($mail->smtpsend) {
154 return HTTP::Response->new(&HTTP::Status::RC_INTERNAL_SERVER_ERROR,
155 "Mail::Internet->smtpsend unable to send message to <$addr>");
159 unless (close(SENDMAIL)) {
160 my $err = $! ? "$!" : "Exit status $?";
161 return HTTP::Response->new(&HTTP::Status::RC_INTERNAL_SERVER_ERROR,
167 my $response = HTTP::Response->new(&HTTP::Status::RC_ACCEPTED,
169 $response->header('Content-Type', 'text/plain');
170 if ($^O eq "MacOS") {
171 $response->header('Server' => "Mail::Internet $Mail::Internet::VERSION");
172 $response->content("Message sent to <$addr>\n");
175 $response->header('Server' => $SENDMAIL);
176 my $to = $request->header("To");
177 $response->content("Message sent to <$to>\n");