<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    Hello joel<br>
    <br>
    you can test you post with curl<br>
    <br>
     curl -d "param1=value1&param2=value2"
    <a class="moz-txt-link-freetext" href="http://localhost:80/reminder">http://localhost:80/reminder</a><br>
    <br>
    <br>
    add debug line in red<br>
    <br>
    <pre wrap="">
</pre>
    <tt>sub send_out_mail {</tt><tt><br>
    </tt><tt>        use Email::Stuffer;</tt><tt><br>
    </tt><tt>        my( $email, $body) = @_;</tt><tt><br>
    </tt><tt>       </tt><tt><font color="#ff0000"> debug $email, $body;</font></tt><tt><br>
    </tt><tt>        my $sender = '<a class="moz-txt-link-abbreviated" href="mailto:joelz@pobox.com">joelz@pobox.com</a>';</tt><tt><br>
    </tt><tt>        Email::Stuffer->from($sender)</tt><tt><br>
    </tt><tt>                                   ->to($email)</tt><tt><br>
    </tt><tt>                                   ->subject("Reminder")</tt><tt><br>
    </tt><tt>                                   ->text_body($body)</tt><tt><br>
    </tt><tt>                                   ->send;</tt><tt><br>
    </tt><tt><br>
    </tt><tt>}</tt><tt><br>
    </tt><br>
    and change <br>
    <br>
    <tt>sub find_passwd {</tt><tt><br>
    </tt><tt>        return({ email => '<a class="moz-txt-link-abbreviated" href="mailto:toto@wanadoo.fr">toto@wanadoo.fr</a>', name =>
      'Name', password => 'password', body => 'body' })</tt><tt><br>
    </tt><tt>                ;</tt><tt><br>
    </tt><tt>}</tt><tt><br>
    </tt><br>
    <br>
    <br>
    the result seems correct ( without crash )<br>
    <br>
    <tt>[7255]  core @0.000110> request: POST /reminder from
      127.0.0.1 in /usr/local/share/perl5/Dancer/Handler.pm l. 56</tt><tt><br>
    </tt><tt>[7255]  core @0.000573> [hit #1]Trying to match 'POST
      /reminder' against /^\/reminder$/ (generated from '/reminder') in
      /usr/local/share/perl5/Dancer/Route.pm l. 84</tt><tt><br>
    </tt><tt>[7255]  core @0.000695> [hit #1]  --> got 1 in
      /usr/local/share/perl5/Dancer/Route.pm l. 102</tt><tt><br>
    </tt><tt>[7255]  core @0.001035> [hit #1]entering before hook in
      /usr/local/share/perl5/Dancer/Hook.pm l. 58</tt><tt><br>
    </tt><tt>[7255] debug @0.003781> [hit #1]toto@wanadoo.frbody in
      /home/test/pobox/lib/pobox.pm l. 69</tt><tt><br>
    </tt><tt>[7255]  core @0.606941> [hit #1]response: 302 in
      /usr/local/share/perl5/Dancer/Handler.pm l. 181</tt><tt><br>
    </tt><br>
    for my point of view, pb come from<br>
    <pre wrap="">find_passwd { }</pre>
    add debug ligne  to check all,  before send email,<br>
    <br>
    bye<br>
    Hugues<br>
    <br>
    <div class="moz-cite-prefix">Le 26/04/2014 09:09, Joel Roth a
      écrit :<br>
    </div>
    <blockquote cite="mid:20140426070910.GA15770@sprite" type="cite">
      <pre wrap="">Hi Dancers,

During development, I've put a file with the
username/email/password on the site (generally bad practice,
I know), and have a button for sending a password reminder.

When the user posts a form with an "email" field to
the /reminder route (see below) it results in the following error,
which is issued *after* the mail is sent out:

The page '<a class="moz-txt-link-freetext" href="http://localhost:3000/reminder">http://localhost:3000/reminder</a>' couldn't be
loaded.  Message Corrupt

I would expect a redirect to '/reminder-has-been-sent'

Do you have any suggestions?

Kind regards,

Joel

--

post '/reminder'    => sub {
    my $email = params->{email};
    my $result = find_passwd($email);
        session user => undef;
        session failed_login => undef;
    if ($result)
    {   
        send_out_mail($result->{email}, $result->{body}); # sent!
        redirect '/reminder-has-been-sent';               # no redirect
    }
    else
    {   
        redirect '/email-not-found';
    }
};

There is a "before" clause.


before sub {
    if (!session('user') and request->path_info !~
        m{(login|bye|reminder|reminder-has-been-sent|email-not-found)$}) {
        # Pass the original path requested along to the handler:
        session requested_path => request->path_info;
        redirect('/login');
    }
    elsif ( ! authorized( session('user'), request->path_info)
            and request->path_info ne '/forbidden'){
        # Pass the original path requested along to the handler:
        session requested_path => request->path_info;
        redirect('/forbidden');                                   
    }
};  

sub find_passwd {
        my $user_or_email = shift;
        # we expect to be in Dancer app directory
        my @entries = read_file('pass');
        my $body;
        my ($name, $email, $username, $password);
        map { chomp } @entries;
        foreach my $entry( @entries ){
                ($name, $email, $username, $password) = split /\s*:\s*/, $entry;
                next if $user_or_email ne $username and $user_or_email ne $email;
                say "-->$name";
                $body = mail_body($name, $username, $password);
                last 
        }
        return({ email => $email, name => $name, password => $password, body => $body })
                if $body;
}
sub mail_body {
        my ($name, $username, $password) = @_;
        my ($first) = $name =~ /(.+?)\s*\S*$/;
        #say $first;
<<FORM;
Dear $first,

This automated mail contains your login credentials for

<a class="moz-txt-link-freetext" href="http://example.com">http://example.com</a>

Your username is $username
Your password is $password

FORM
}
sub send_out_mail {
        my( $email, $body) = @_; 
        my $sender = '<a class="moz-txt-link-abbreviated" href="mailto:joelz@pobox.com">joelz@pobox.com</a>';
        Email::Stuffer->from($sender)
                                   ->to($email)
                                   ->subject("Reminder")
                                   ->text_body($body)
                                   ->send;
        
}

1
</pre>
    </blockquote>
    <br>
  </body>
</html>