diff options
author | Struan Donald <struan@exo.org.uk> | 2011-05-25 17:44:37 +0100 |
---|---|---|
committer | Struan Donald <struan@exo.org.uk> | 2011-05-25 17:44:37 +0100 |
commit | 09a64afec4e023a184de075df3780c8b525b6066 (patch) | |
tree | cad6484a2c8d074251960cd781139d28e68bb2ef | |
parent | bbe4d5975fddfc731b0177119bf4cda401d41d97 (diff) |
use c->send_email on contact page and add more tests
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Contact.pm | 47 | ||||
-rw-r--r-- | t/app/controller/contact.t | 15 | ||||
-rw-r--r-- | templates/email/default/contact.txt | 5 |
3 files changed, 32 insertions, 35 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Contact.pm b/perllib/FixMyStreet/App/Controller/Contact.pm index a3e397022..0e6e60944 100644 --- a/perllib/FixMyStreet/App/Controller/Contact.pm +++ b/perllib/FixMyStreet/App/Controller/Contact.pm @@ -181,14 +181,6 @@ sub prepare_params_for_email : Private { ); } - my $postfix = '[ Sent by contact.cgi on ' . - $ENV{'HTTP_HOST'} . '. ' . - "IP address " . $ENV{'REMOTE_ADDR'} . - ($ENV{'HTTP_X_FORWARDED_FOR'} ? ' (forwarded from '.$ENV{'HTTP_X_FORWARDED_FOR'}.')' : '') . '. ' . - ' ]'; - - $c->stash->{message} .= "\n\n$postfix"; - return 1; } @@ -227,30 +219,21 @@ sub send_email : Private { my $recipient = $c->cobrand->contact_email(); my $recipient_name = $c->cobrand->contact_name(); - my $email = mySociety::Email::construct_email( - { - _body_ => $c->stash->{message}, - From => [ $c->stash->{em}, $c->stash->{form_name} ], - To => [ [ $recipient, _($recipient_name) ] ], - Subject => 'FMS message: ' . $c->stash->{subject}, - 'Message-ID' => sprintf( - '<contact-%s-%s@mysociety.org>', - time(), unpack( 'h*', random_bytes( 5, 1 ) ) - ), - } - ); - - # FIXME: do something more sensible here - if ( FixMyStreet->test_mode ) { - $c->stash->{success} = 1; - } else { - my $result = - mySociety::EmailUtil::send_email( $email, $c->stash->{em}, $recipient ); - - if ( $result == mySociety::EmailUtil::EMAIL_SUCCESS ) { - $c->stash->{success} = 1; - } - } + $c->stash->{host} = $c->req->header('HOST'); + $c->stash->{ip} = $c->req->address; + $c->stash->{ip} .= + $c->req->header('X-Forwarded-For') + ? ' ( forwarded from ' . $c->req->header('X-Forwarded-For') . ' )' + : ''; + + $c->send_email( 'contact.txt', { + to => [ [ $recipient, _($recipient_name) ] ], + from => [ $c->stash->{em}, $c->stash->{form_name} ], + subject => 'FMS message: ' . $c->stash->{subject}, + }); + + # above is always succesful :( + $c->stash->{success} = 1; return 1; } diff --git a/t/app/controller/contact.t b/t/app/controller/contact.t index 860ada4ce..fbf794b6b 100644 --- a/t/app/controller/contact.t +++ b/t/app/controller/contact.t @@ -163,13 +163,13 @@ for my $test ( subtest 'check submit page error handling' => sub { $mech->get_ok( $test->{url} ? $test->{url} : '/contact' ); $mech->submit_form_ok( { with_fields => $test->{fields} } ); - is_deeply $mech->page_errors, $test->{page_errors}; - is_deeply $mech->form_errors, $test->{field_errors}; + is_deeply $mech->page_errors, $test->{page_errors}, 'page errors'; + is_deeply $mech->form_errors, $test->{field_errors}, 'field_errors'; # we santise this when we submit so need to remove it delete $test->{fields}->{id} if $test->{fields}->{id} and $test->{fields}->{id} eq 'invalid'; - is_deeply $mech->visible_form_values, $test->{fields}; + is_deeply $mech->visible_form_values, $test->{fields}, 'form values'; }; } @@ -186,9 +186,18 @@ for my $test ( ) { subtest 'check email sent correctly' => sub { + $mech->clear_emails_ok; $mech->get_ok('/contact'); $mech->submit_form_ok( { with_fields => $test->{fields} } ); $mech->content_contains('Thanks for your feedback'); + $mech->email_count_is(1); + + my $email = $mech->get_email; + + is $email->header('Subject'), 'FMS message: ' . $test->{fields}->{subject}, 'subject'; + is $email->header('From'), "\"$test->{fields}->{name}\" <$test->{fields}->{em}>", 'from'; + like $email->body, qr/$test->{fields}->{message}/, 'body'; + like $email->body, qr/Sent by contact.cgi on \S+. IP address (?:\d{1,3}\.){3,}\d{1,3}/, 'body footer' }; } done_testing(); diff --git a/templates/email/default/contact.txt b/templates/email/default/contact.txt new file mode 100644 index 000000000..59a778ad5 --- /dev/null +++ b/templates/email/default/contact.txt @@ -0,0 +1,5 @@ +Subject: FMS message: [% subject %] + +[% message %] + +[ Sent by contact.cgi on [% host %]. IP address [% ip %] ] |