Jump to content

FrontendContact - Module for creating one or more contact forms on the frontend


Juergen
 Share

Recommended Posts

I guess that something stops the sending process, but without TracyDebugger we get no information whats going on. You can take a look into the log files if you find something.

Another try would be to remove the creation of the page for testing purpose - let only the mail function inside the isValid() method and see what happens.

The code seems to be OK

 

  • Like 1
Link to comment
Share on other sites

<?php namespace Processwire;
$form = new \FrontendForms\Form('myForm');

$firstname = new \FrontendForms\InputText('scf_firstname');
$firstname->setLabel($fields->get('scf_firstname')->$label);
$firstname->setRule('required');
$form->add($firstname);
    
$lastname = new \FrontendForms\InputText('scf_lastname');
$lastname->setLabel($fields->get('scf_lastname')->$label);
$lastname->setRule('required');
$form->add($lastname);
    
$phone = new \FrontendForms\InputText('scf_phone');
$phone->setLabel($fields->get('scf_phone')->$label);
$phone->setRule('required');
$form->add($phone);      
    
$email = new \FrontendForms\InputText('scf_email');
$email->setLabel($fields->get('scf_email')->$label);
$email->setRule('required');
$form->add($email); 
    
$legalservice = new \FrontendForms\Select('scf_legalservice');
$legalservice->setLabel(__($fields->get('scf_legalservice')->$label));
// Add options dynamically based on existing pages
$legalservices = wire('pages')->find('template=legal_service');
foreach ($legalservices as $service) {
$legalservice->addOption($service->title, $service->id);
} 
$form->add($legalservice);
    
$message = new \FrontendForms\Textarea('scf_message');
$message->setLabel($fields->get('scf_message')->$label);
$message->setRule('required');
$form->add($message);

$button = new \FrontendForms\Button('submit');
$button->setAttribute('value', $fields->get('label_send')->label);
$form->add($button);

if($form->isValid()){
    
    
    $p = new Page();
    $p->template = 'contact_inquiry';
    $p->parent = wire('pages')->get('template=contact');
    $p->title = $form->getValue('scf_firstname') . ' ' . $form->getValue('scf_lastname');
    $p->scf_firstname = $form->getValue('scf_firstname');
    $p->scf_lastname = $form->getValue('scf_lastname');
    $p->scf_phone = $form->getValue('scf_phone');
    $p->scf_email = $form->getValue('scf_email');
    $p->scf_legalservice = $form->getValue('scf_legalservice');
    $p->scf_message = $form->getValue('scf_message');
    
  
    // Set the scf_date field to the current date and time with the correct timezone
    $dateTime = new DateTime('now', new DateTimeZone('Europe/Stockholm'));
    $p->scf_date = $dateTime->format('U'); // 'U' format returns the Unix timestamp
    
    
    $p->save();
    
    
    $m = wireMail();
    $m->to('info@...'); // please change it
    $m->subject('Nytt ärende');
  
    $messageBody .= "<strong>{$fields->get('scf_firstname')->label}:</strong> " . $form->getValue('scf_firstname') . "<br>";
    $messageBody .= "<strong>{$fields->get('scf_lastname')->label}:</strong> " . $form->getValue('scf_lastname') . "<br>";
    $messageBody .= "<strong>{$fields->get('scf_phone')->label}:</strong> " . $form->getValue('scf_phone') . "<br>";
    $messageBody .= "<strong>{$fields->get('scf_email')->label}:</strong> " . $form->getValue('scf_email') . "<br>";
    
    // Retrieve the title of the selected legal service
    $selectedLegalServiceID = $form->getValue('scf_legalservice');
    $selectedLegalService = wire('pages')->get($selectedLegalServiceID);
    $selectedLegalServiceTitle = $selectedLegalService ? $selectedLegalService->title : 'Not specified';
    
    $messageBody .= "<strong>{$fields->get('scf_legalservice')->label}:</strong> " . $selectedLegalServiceTitle . "<br>";
    
    $messageBody .= "<strong>{$fields->get('scf_message')->label}:</strong> " . $form->getValue('scf_message') . "<br>";
    
    $m->bodyHTML($messageBody);
    $m->send();
    
// Redirect to the success page
    wire('session')->redirect(wire('config')->urls->root . 'forfragan-skickades/');
}

echo $form->render();
?>

 

Link to comment
Share on other sites

There is nothing wrong with your redirect, but in your case a better place for the redirect would be only after the mail has been sent (inside the if condition):

if($m->send()){
   wire('session')->redirect(wire('config')->urls->root . 'forfragan-skickades/');
}

I have tested it on my site with a redirect to another page -> works without problems.

There must be another problem with your system. Sometimes ProcessWire does unconventional things and struggles even all codes are OK. Closing the browser completely to delete all sessions would help in most cases (Reset). I have struggled sometimes with those problems during development of modules, where I am changing code, got errors and afterwards ProcessWire was a little bit out of control. Reseting it by closing and opening again is sometimes the best way.

10 hours ago, Flashmaster82 said:

I think the problem was $m->from('noreply@...'); but im not sure.

I am pretty sure that this was not the problem. My guess: Either it was the system problem as described above or you had a misspelling of your receiver email address (not the from address)

  • Like 1
Link to comment
Share on other sites

Hello @Flashmaster82

The new update could be interesting for you:

https://processwire.com/talk/topic/26015-frontendforms-a-module-for-creating-and-validating-forms-on-the-frontend/?do=findComment&comment=238354

On 1/6/2024 at 9:45 PM, Flashmaster82 said:

I´m facing a new problem though.. I can´t get it to redirect after sending?

This update includes a new method for redirecting after form submission in an easy way.

Use the setRedirectURL() method

$form->setRedirectURL('www.google.com'); // enter the redirect URL inside the parenthesis

This will redirect to another page/URL after successful form submission. No more need to add the redirect inside the isValid() method.

Best regards

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Version 1.2.0 ist out!

This version supports mail sending with Postmark mail sending service.

Read the full changelog.md for more information about Postmark and what has been changed.

If you have one of the Postmark modules as mentioned in the changelog installed, you will find a new configuration field inside the module configuration.

postmark.thumb.jpg.cf86da38e2a3edd27fb268273662bf1f.jpg

Here you can select if you want to send your mails with Postmark or not. Selecting "none" means using the default WireMail class for sending mails.

In order to work properly, you will need at least FrontendForms 2.1.57 installed (as mentiond in the changelog.md).

As always, this is Beta-status, so please take care that everything works as expected.

Happy testing! ?

Jürgen

Link to comment
Share on other sites

  • 6 months later...

Version 1.3.6 comes with 2 new features:

  • New configuration field to limit the filesize of uploaded files globally
  • Save mails as pages too (user request by @Flashmaster82 in the forum here

save-action.jpg.e4ed656f8820fb60d9c53e49eef85def.jpg

You can read more about the new features here in the changelogs.

Please test it carefully before using it on live sites and have fun! 🙂

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...