Skip to content

Commit

Permalink
Merge pull request #38 from Matteo-Peronnet/fix-email-reply
Browse files Browse the repository at this point in the history
Add email reply-to to the user
  • Loading branch information
lenybernard authored Aug 1, 2018
2 parents f93e829 + 6c4e0f4 commit 17d8e04
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 22 deletions.
15 changes: 8 additions & 7 deletions Controller/FormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ public function addFormAnswerAction(Request $request)
}
}

$email = null;
foreach ($_taintedValues['questions'] as $question) {
if ($question['label'] == 'Email' || $question['label'] == 'email') {
$email = $question[0];
}
}

///////////////////////// SEND EMAIL TO ADMIN (set in the form or default one) //////////////////////////////////////////

//$isSpam = $this->testForSpam($taintedValues, $request);
Expand Down Expand Up @@ -135,19 +142,13 @@ public function addFormAnswerAction(Request $request)
);
if (count($regexErrors) == 0) {
$emailSend = true;
$this->createAndSendMail($widget->getAdminSubject(), $from, $targetEmail, $body, 'text/html', null, [], $mailer);
$this->createAndSendMail($widget->getAdminSubject(), $from, $targetEmail, $body, 'text/html', $email, [], $mailer);
}
} catch (\Exception $e) {
throw $e;
}
}
///////////////////////// AUTOANSWER (if email field exists and is filled properly) //////////////////////////////////////////
$email = null;
foreach ($_taintedValues['questions'] as $question) {
if ($question['label'] == 'Email' || $question['label'] == 'email') {
$email = $question[0];
}
}
if ($widget->isAutoAnswer() === true && $email) {
if ($errors = $this->get('validator')->validate($widget->getNoReply(), new EmailConstraint())) {
try {
Expand Down
37 changes: 37 additions & 0 deletions Tests/Context/EmailContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Victoire\Widget\FormBundle\Tests\Context;

use Alex\MailCatcher\Behat\MailCatcherContext;

class EmailContext extends MailCatcherContext
{

/**
* @Given /^I should see "([^"]*)" in mail reply\-to$/
*/
public function iShouldSeeInMailReplyTo($email)
{
if (null === $this->currentMessage) {
throw new \RuntimeException('No message selected');
}

if ($this->currentMessage->getHeaders()->get('reply-to') !== $email) {
throw new \InvalidArgumentException(sprintf('Unable to find reply-to header with email : "%s".', $email));
}
}

/**
* @Given /^I should see nothing in mail reply\-to$/
*/
public function iShouldSeeNothingInMailReplyTo()
{
if (null === $this->currentMessage) {
throw new \RuntimeException('No message selected');
}

if ($this->currentMessage->getHeaders()->has('reply-to')) {
throw new \InvalidArgumentException(sprintf('Unable reply-to headers is defined'));
}
}
}
41 changes: 41 additions & 0 deletions Tests/Features/Email/create.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@mink:selenium2 @alice(Page) @reset-schema
Feature: Receive an email from the form

Background:
Given I maximize the window
And I am on homepage
And I wait 3 second
When I switch to "layout" mode
Then I should see "New content"
When I select "Form" from the "1" select of "main_content" slot
And I wait 5 seconds
Then I fill in "_a_static_widget_form[submitLabel]" with "send"
Then I fill in "_a_static_widget_form[adminSubject]" with "Contact"

Scenario: I simple a form email contact
And I click the "#questions-form-list a.add_question_link" element
Then I fill in "_a_static_widget_form[questions][0][title]" with "Email"
And I submit the widget
And I wait 5 seconds
Then I switch to "readonly" mode
When I fill in "cms_form_content[questions][0][]" with "[email protected]"
And I press "form-1-submit"
And I wait 2 second
Then 1 mail should be sent
When I open mail from "[email protected]"
Then I should see "[email protected]" in mail
And I should see "[email protected]" in mail reply-to

Scenario: I simple a form without email contact
And I click the "#questions-form-list a.add_question_link" element
Then I fill in "_a_static_widget_form[questions][0][title]" with "Name"
And I submit the widget
And I wait 5 seconds
Then I switch to "readonly" mode
When I fill in "cms_form_content[questions][0][]" with "[email protected]"
And I press "form-1-submit"
And I wait 2 second
Then 1 mail should be sent
When I open mail from "[email protected]"
Then I should see "[email protected]" in mail
And I should see nothing in mail reply-to
19 changes: 12 additions & 7 deletions Tests/config.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
swiftmailer:
transport: smtp
host: 127.0.0.1
username: null
password: null
spool: { type: memory }

parameters:
mailer_transport: smtp
mailer_port: 1025
mailer_host: 127.0.0.1
mailer_user: ''
mailer_password: ''
victoire_widget_form.default_email_address: "[email protected]"
victoire_widget_form.default_email_label: "anakin"

swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
port: "%mailer_port%"
30 changes: 22 additions & 8 deletions Tests/dependencies.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
#!/usr/bin/env bash

echo ">>> Installing Mailhog"

# Download binary from github
wget https://github.com/mailhog/MailHog/releases/download/v1.0.0/MailHog_linux_amd64
sudo cp MailHog_linux_amd64 /usr/bin/mailhog
sudo chmod +x /usr/bin/mailhog
nohup mailhog > /dev/null 2>&1 &
sed -i -e 's/firefox/chrome/g' behat.yml.dist
echo ">>> Installing MailCatcher"

sudo apt install -y build-essential libsqlite3-dev ruby-dev
gem install mailcatcher
echo "sendmail_path = /usr/bin/env $(which catchmail) -f 'www-data@localhost'" >> /etc/php/7.1/mods-available/mailcatcher.ini
phpenmod mailcatcher
/usr/bin/env $(which mailcatcher) --ip=0.0.0.0

echo ">>> Update Behat config"

# Get the Namespace of EmailContext file with regular expression
# Alter the namespace to remove ';' then use triple backslashes instead of a single backslash
namespace="$(cat ../../../Tests/Context/EmailContext.php | sed -rn 's/namespace ((\\{1,2}\w+|\w+\\{1,2})(\w+\\{0,2})+)/\1/p' | sed -r 's/;+$//' | sed -e 's|\\|\\\\\\|g' )"
# Add EmailContext path in the behat.yml.dist file to load the context
sed -i "s@contexts:@contexts: \n - $namespace\\\EmailContext@" behat.yml.dist
rm ../../../Tests/Context/EmailContext.php
sed -i "s@extensions:@extensions: \n Alex\\\MailCatcher\\\Behat\\\MailCatcherExtension\\\Extension:\n url: http://fr.victoire.io:1080\n purge_before_scenario: true@" behat.yml.dist
# Use Chrome instead of firefox
sed -i -e 's/firefox/chrome/g' behat.yml.dist

echo ">>> Install dev Dependencies"
php -d memory_limit=-1 /usr/local/bin/composer require alexandresalome/mailcatcher

0 comments on commit 17d8e04

Please sign in to comment.