From 6777a92539fee6f6a1eba37c99e918ce1530f976 Mon Sep 17 00:00:00 2001
From: Thomas Schwinge <tschwinge@gnu.org>
Date: Tue, 1 Jan 2008 22:14:09 +0100
Subject: Use Email::Send instead of Mail::Sendmail.

IkiWiki.pm (sendmail): New.
doc/plugins/write.mdwn: Update.
IkiWiki/Plugin/passwordauth.pm: Use it.
TODO.  Other files.
---
 IkiWiki.pm                     |   49 +++++++++++++++++++++++++++++++++++++++-
 IkiWiki/Plugin/passwordauth.pm |   11 ++++-----
 Makefile.PL                    |    3 +-
 debian/control                 |    6 ++--
 doc/install.mdwn               |    5 ++-
 doc/plugins/write.mdwn         |    9 +++++++
 6 files changed, 70 insertions(+), 13 deletions(-)

diff --git a/IkiWiki.pm b/IkiWiki.pm
index e476521..6498fc1 100644
--- a/IkiWiki.pm
+++ b/IkiWiki.pm
@@ -4,6 +4,7 @@ package IkiWiki;
 
 use warnings;
 use strict;
+use Return::Value;
 use Encode;
 use HTML::Entities;
 use URI::Escape q{uri_escape_utf8};
@@ -19,7 +20,7 @@ use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase
 use Exporter q{import};
 our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
                  bestlink htmllink readfile writefile pagetype srcfile pagename
-                 displaytime will_render gettext urlto targetpage
+                 displaytime will_render gettext urlto targetpage sendmail
 		 add_underlay
                  %config %links %pagestate %renderedfiles
                  %pagesources %destsources);
@@ -1690,6 +1691,52 @@ sub glob2re ($) { #{{{
 	return $re;
 } #}}}
 
+sub sendmail(%) { #{{{
+	my %args=@_;
+
+	eval q{use Email::Simple};
+	return failure gettext('Failed to send mail: ').$@ if $@;
+	eval q{use Email::Simple::Creator};
+	return failure gettext('Failed to send mail: ').$@ if $@;
+
+	# TODO.  Might there be a better way to do this?
+	my $message = Email::Simple->create(body => $args{Message});
+	delete $args{Message};
+	$message->header_set($_, $args{$_}) foreach keys %args;
+
+	eval q{use Email::Send};
+	return failure gettext('Failed to send mail: ').$@ if $@;
+
+	# TODO.  Help a bit to find the sendmail executable.
+	# <http://rt.cpan.org/Public/Bug/Display.html?id=24482>
+	my $path_orig = $ENV{PATH};
+	$ENV{PATH}.=':/usr/sbin:/usr/lib';
+
+	my $mailer=Email::Send->new;
+	my $sent;
+	my $log;
+	for (qw[Qmail Sendmail SMTP]) {
+		$log.="\n$_: ";
+		if (! $mailer->mailer_available($_)) {
+			$log.='not available.';
+			next;
+		}
+		$mailer->mailer($_);
+		my $result = $mailer->send($message);
+		$log.=$result;
+		if ($result) {
+			$sent='yes';
+			$log="$_: $result";
+			last;
+		}
+	}
+
+	$ENV{PATH}=$path_orig;
+
+	return failure gettext('Failed to send mail: ').$log unless defined $sent;
+	return success $log;
+} #}}}
+
 package IkiWiki::FailReason;
 
 use overload ( #{{{
diff --git a/IkiWiki/Plugin/passwordauth.pm b/IkiWiki/Plugin/passwordauth.pm
index e0c0a3b..0733966 100644
--- a/IkiWiki/Plugin/passwordauth.pm
+++ b/IkiWiki/Plugin/passwordauth.pm
@@ -279,18 +279,17 @@ sub formbuilder (@) { #{{{
 					REMOTE_ADDR => $ENV{REMOTE_ADDR},
 				);
 				
-				eval q{use Mail::Sendmail};
-				error($@) if $@;
-				sendmail(
+				my $result=sendmail(
 					To => IkiWiki::userinfo_get($user_name, "email"),
 					From => "$config{wikiname} admin <".
 						(defined $config{adminemail} ? $config{adminemail} : "")
 						.">",
 					Subject => "$config{wikiname} information",
 					Message => $template->output,
-				) or error(gettext("Failed to send mail"));
-				
-				$form->text(gettext("You have been mailed password reset instructions."));
+				);
+				error($result) unless $result;
+
+				$form->text(gettext("You have been mailed password reset instructions.")." $result");
 				$form->field(name => "name", required => 0);
 				push @$buttons, "Reset Password";
 			}
diff --git a/Makefile.PL b/Makefile.PL
index 1fc0bf3..833851b 100755
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -99,6 +99,7 @@ extra_install:
 }
 }
 
+# TODO.  Have to add `Email::Simple::Create' and `Return::Value' here?
 WriteMakefile(
 	NAME		=> 'IkiWiki',
 	PREFIX		=> "/usr/local",
@@ -112,7 +113,7 @@ WriteMakefile(
 		'HTML::Scrubber'	=> 0,
 		'CGI::FormBuilder'	=> 3.02.02,
 		'CGI::Session'		=> 0,
-		'Mail::Sendmail'	=> 0,
+		'Email::Send'		=> 0,
 		'HTML::Parser'		=> 0,
 		'URI'			=> 0,
 		'Data::Dumper'		=> 2.11,
diff --git a/debian/control b/debian/control
index 72aa12a..75500cf 100644
--- a/debian/control
+++ b/debian/control
@@ -2,7 +2,7 @@ Source: ikiwiki
 Section: web
 Priority: optional
 Build-Depends: perl, debhelper (>= 5)
-Build-Depends-Indep: dpkg-dev (>= 1.9.0), libxml-simple-perl, libtext-markdown-perl | markdown, libtimedate-perl, libhtml-template-perl, libhtml-scrubber-perl, wdg-html-validator, libhtml-parser-perl, liburi-perl, perlmagick
+Build-Depends-Indep: dpkg-dev (>= 1.9.0), libxml-simple-perl, libtext-markdown-perl | markdown, libtimedate-perl, libhtml-template-perl, libhtml-scrubber-perl, wdg-html-validator, libhtml-parser-perl, liburi-perl, perlmagick, libreturn-value-perl
 Maintainer: Joey Hess <joeyh@debian.org>
 Uploaders: Joey Hess <joeyh@debian.org>, Josh Triplett <josh@freedesktop.org>
 Standards-Version: 3.8.0
@@ -12,8 +12,8 @@ Vcs-Browser: http://git.ikiwiki.info/?p=ikiwiki
 
 Package: ikiwiki
 Architecture: all
-Depends: ${perl:Depends}, markdown | libtext-markdown-perl, libhtml-scrubber-perl, libhtml-template-perl, libhtml-parser-perl, liburi-perl
-Recommends: gcc | c-compiler, libc6-dev | libc-dev, subversion | git-core (>= 1:1.5.0) | tla | bzr (>= 0.91) | mercurial | monotone (>= 0.38), libxml-simple-perl, libnet-openid-consumer-perl, liblwpx-paranoidagent-perl, libtimedate-perl, libcgi-formbuilder-perl (>= 3.05), libcgi-session-perl (>= 4.14-1), libmail-sendmail-perl, libauthen-passphrase-perl
+Depends: ${perl:Depends}, markdown | libtext-markdown-perl, libhtml-scrubber-perl, libhtml-template-perl, libhtml-parser-perl, liburi-perl, libreturn-value-perl
+Recommends: gcc | c-compiler, libc6-dev | libc-dev, subversion | git-core (>= 1:1.5.0) | tla | bzr (>= 0.91) | mercurial | monotone (>= 0.38), libxml-simple-perl, libnet-openid-consumer-perl, liblwpx-paranoidagent-perl, libtimedate-perl, libcgi-formbuilder-perl (>= 3.05), libcgi-session-perl (>= 4.14-1), libemail-send-perl, libemail-simple-creator-perl, libauthen-passphrase-perl
 Suggests: viewvc | gitweb | viewcvs, libsearch-xapian-perl, xapian-omega (>= 1.0.5), librpc-xml-perl, libtext-wikiformat-perl, python, python-docutils, polygen, tidy, libxml-feed-perl, libmailtools-perl, perlmagick, libfile-mimeinfo-perl, libcrypt-ssleay-perl, liblocale-gettext-perl (>= 1.05-1), libtext-typography-perl, libtext-csv-perl, libdigest-sha1-perl, graphviz, libnet-amazon-s3-perl, sparkline-php, texlive, texlive-science
 Conflicts: ikiwiki-plugin-table
 Replaces: ikiwiki-plugin-table
diff --git a/doc/install.mdwn b/doc/install.mdwn
index d745737..8899c44 100644
--- a/doc/install.mdwn
+++ b/doc/install.mdwn
@@ -12,7 +12,7 @@ It's recommended you have a C compiler, as ikiwiki uses one to build
 wrappers.
 
 Ikiwiki requires the [[!cpan Text::Markdown]], [[!cpan URI]],
-[[!cpan HTML::Parser]], [[!cpan HTML::Template]], and [[!cpan HTML::Scrubber]]
+[[!cpan HTML::Parser]], [[!cpan HTML::Template]], [[!cpan HTML::Scrubber]], and [[!cpan Return::Value]]
 perl modules be installed. It can also use a lot of other perl modules, if
 they are available.
 
@@ -24,7 +24,8 @@ documentation for details.
 Here's an example of how to install ikiwiki's dependencies using yum
 on Fedora 7:
 
-	yum install perl-Text-Markdown perl-Mail-Sendmail perl-HTML-Scrubber \
+<!-- TODO.  I'm not sure about `perl-Email-Send' and `perl-Email-Simple-Creator'.  Someone please check.  --tschwinge -->
+	yum install perl-Text-Markdown perl-Email-Send perl-Email-Simple-Creator perl-HTML-Scrubber \
 	  perl-XML-Simple perl-TimeDate perl-HTML-Template perl-CGI-FormBuilder \
 	  perl-CGI-Session perl-File-MimeInfo perl-gettext perl-Authen-Passphrase
 
diff --git a/doc/plugins/write.mdwn b/doc/plugins/write.mdwn
index b9601ea..0644567 100644
--- a/doc/plugins/write.mdwn
+++ b/doc/plugins/write.mdwn
@@ -663,6 +663,15 @@ constructed instead of the default relative url.
 Passed a page and an extension, returns the filename that page will be
 rendered to.
 
+#### `sendmail(%)`
+
+Accepts a list (TODO: correct Perl-y term?) of parameters, according to what
+[[cpan Email::Simple]] does accept.  The additionally accepted parameter
+*Message* is to contain the body of the email message to be sent out.
+
+TODO.  Describe return value stuff.  Returns a boolean value and a string
+containing a log of the message injection.
+
 ## Miscellaneous
 
 ### Internal use pages
-- 
1.5.4.3

