From: Thomas Schwinge <thomas@schwinge.name>
Subject: [PATCH] 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               |    2 +-
 doc/plugins/write.mdwn         |    9 +++++++
 6 files changed, 68 insertions(+), 12 deletions(-)

diff --git a/IkiWiki.pm b/IkiWiki.pm
index 08a3d78..6ba4af7 100644
--- a/IkiWiki.pm
+++ b/IkiWiki.pm
@@ -4,6 +4,7 @@ package IkiWiki;
 
 use warnings;
 use strict;
+use Return::Value;
 use Encode;
 use URI::Escape q{uri_escape_utf8};
 use POSIX ();
@@ -22,7 +23,7 @@ our @EXPORT = qw(hook debug error htmlpage template template_depends
 	htmllink readfile writefile pagetype srcfile pagename
 	displaytime will_render gettext ngettext urlto targetpage
 	add_underlay pagetitle titlepage linkpage newpagefile
-	inject add_link add_autofile
+	inject add_link add_autofile sendmail
 	%config %links %pagestate %wikistate %renderedfiles
 	%pagesources %destsources %typedlinks);
 our $VERSION = 3.00; # plugin interface version, next is ikiwiki version
@@ -2391,6 +2392,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 35ebd96..6a04c67 100644
--- a/IkiWiki/Plugin/passwordauth.pm
+++ b/IkiWiki/Plugin/passwordauth.pm
@@ -300,18 +300,17 @@ sub formbuilder (@) {
 					remote_addr => $session->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 b289684..2c74350 100755
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -154,6 +154,7 @@ extra_install: underlay_install
 }
 }
 
+# TODO.  Have to add `Email::Simple::Create' and `Return::Value' here?
 WriteMakefile(
 	NAME		=> 'IkiWiki',
 	PREFIX		=> "/usr/local",
@@ -167,7 +168,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 71c9e18..1a69e1a 100644
--- a/debian/control
+++ b/debian/control
@@ -7,7 +7,7 @@ Build-Depends-Indep: dpkg-dev (>= 1.9.0), libxml-simple-perl,
   libtimedate-perl, libhtml-template-perl,
   libhtml-scrubber-perl, wdg-html-validator,
   libhtml-parser-perl, liburi-perl (>= 1.36), perlmagick, po4a (>= 0.34),
-  libfile-chdir-perl,
+  libfile-chdir-perl, libreturn-value-perl
 Maintainer: Joey Hess <joeyh@debian.org>
 Uploaders: Josh Triplett <josh@freedesktop.org>
 Standards-Version: 3.9.1
@@ -20,14 +20,14 @@ Architecture: all
 Depends: ${misc:Depends}, ${perl:Depends}, ${python:Depends}, 
   libtext-markdown-perl | markdown,
   libhtml-scrubber-perl, libhtml-template-perl,
-  libhtml-parser-perl, liburi-perl (>= 1.36)
+  libhtml-parser-perl, liburi-perl (>= 1.36), libreturn-value-perl
 Recommends: gcc | c-compiler, 
   libc6-dev | libc-dev,
   subversion | git-core (>= 1:1.5.0) | git (>= 1:1.7) | tla | bzr (>= 0.91) | mercurial | monotone (>= 0.38) | darcs,
   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, libterm-readline-gnu-perl
+  libemail-send-perl, libemail-simple-creator-perl, libauthen-passphrase-perl, libterm-readline-gnu-perl
 Suggests: viewvc | gitweb | viewcvs, libsearch-xapian-perl,
   xapian-omega (>= 1.0.5), librpc-xml-perl, libtext-wikiformat-perl,
   python, python-docutils, polygen, tidy, libhtml-tree-perl,
diff --git a/doc/install.mdwn b/doc/install.mdwn
index f38ae2a..7361c08 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.
 
diff --git a/doc/plugins/write.mdwn b/doc/plugins/write.mdwn
index 6b751f0..99e30a1 100644
--- a/doc/plugins/write.mdwn
+++ b/doc/plugins/write.mdwn
@@ -1031,6 +1031,15 @@ user, ikiwiki will avoid re-adding it again.
 This function needs to be called during the scan hook, or earlier in the
 build process, in order to add the file early enough for it to be built.
 
+### `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
-- 
tg: (a12dd23..) sendmail (depends on: master)

