loader = $twig->getLoader(); $this->mailerService = $mailerService; $this->staticSiteBuilder = $staticSiteBuilder; $this->curlService = $curlService; } public function checkPost(Request $request) : ?Response { $post = $request->getPayload()->all(); //no post, return if (count($post) == 0) { return null; } if (!isset($post['csrf_token']) || !$this->isCsrfTokenValid(StaticSiteBuilderConfig::$csrfTokenName, $post['csrf_token'])) { return null; } if (isset($post['_wpcf7'])) { return $this->contactFormSubmission($post); } } public function contactFormSubmission(array $post) : ?Response { //example of contact form post // Array ( // [_wpcf7] => 119 // [_wpcf7_version] => 6.0.3 // [_wpcf7_locale] => en_US // [_wpcf7_unit_tag] => wpcf7-f119-p536-o1 // [_wpcf7_container_post] => 536 // [_wpcf7_posted_data_hash] => // [your-name] => test // [your-phone] => 07000000000 // [your-email] => test@test.com // [your-enquiry] => test // [_wpcf7_ak_hp_textarea] => // [_wpcf7_ak_js] => 249 // [_wpcf7_ak_bib] => 1737395046480 // [_wpcf7_ak_bfs] => 1737395054127 // [_wpcf7_ak_bkpc] => 19 // [_wpcf7_ak_bkp] => 83;100,8;92,113;73,26;72;88,172;80,182;80,91;92,79;1,513;88;94,14;111,125;59,13;2,783;80;96,19;96,103;73,7; // [_wpcf7_ak_bmc] => 120;116,8591; // [_wpcf7_ak_bmcc] => 2 // [_wpcf7_ak_bmk] => // [_wpcf7_ak_bck] => // [_wpcf7_ak_bmmc] => 5 // [_wpcf7_ak_btmc] => 0 // [_wpcf7_ak_bsc] => 6 // [_wpcf7_ak_bte] => // [_wpcf7_ak_btec] => 0 // [_wpcf7_ak_bmm] => 147,608;219,270;145,17;128,628;1522,831; // ) $yourName = ''; $yourPhone = ''; $yourEmail = ''; $yourEnquiry = ''; if (isset($post['your-name']) && is_string($post['your-name'])) { $yourName = trim(substr($post['your-name'], 0, 255)); } else { //contact page has different name format... 2 part string if (isset($post['firstname']) && is_string($post['firstname'])) { $yourName = trim(substr($post['firstname'], 0, 255)); } if (isset($post['lastname']) && is_string($post['lastname'])) { if ($yourName != '') { $yourName .= ' '; } $yourName .= trim(substr($post['lastname'], 0, 255)); } } if (isset($post['your-phone']) && is_string($post['your-phone'])) { $yourPhone = trim(substr($post['your-phone'], 0, 255)); } if (isset($post['your-email']) && is_string($post['your-email'])) { $yourEmail = trim(substr($post['your-email'], 0, 255)); } if (isset($post['your-enquiry']) && is_string($post['your-enquiry'])) { $yourEnquiry = trim(substr($post['your-enquiry'], 0, 32640)); } elseif (isset($post['your-message']) && is_string($post['your-message'])) { //contact page has different message fieldname... $yourEnquiry = trim(substr($post['your-message'], 0, 32640)); } if ($yourName && $yourPhone && $yourEmail && $yourEnquiry) { //first email: forward enquiry to generalAddress from noReplyAddress $body = ''; $body .= '

Name: ' . $yourName . '

'; $body .= '

Phone: ' . $yourPhone . '

'; $body .= '

Email: ' . $yourEmail . '

'; $body .= '

Message: ' . strip_tags($yourEnquiry) . '

'; $fromAddress = new Address( $this->mailerService->noReplyAddress, $this->mailerService->noReplyName ); $email = (new Email()) ->from($fromAddress) ->to($this->mailerService->generalAddress) ->subject('New Enquiry from Website') ->text(strip_tags($body)) ->html($body); $this->mailerService->sendEmail($email); //second email: thank customer for their enquiry //without captcha, too risky /* $body = ''; $body .= '

Dear ' . $yourName . ',

'; $body .= '

Thank you for your enquiry. A member of the team will be in touch shortly.'; $body .= '

Best regards,

'; $body .= '

' . $this->mailerService->generalName .'

'; $fromAddress = new Address( $this->mailerService->generalAddress, $this->mailerService->generalName ); $email = (new Email()) ->from($fromAddress) ->to($yourEmail) ->subject('Thanks for your Enquiry') ->text(strip_tags($body)) ->html($body); $this->mailerService->sendEmail($email); */ return $this->redirectToRoute('thanks_enquiry'); } else { //some error } return null; } #[Route('/thanks-enquiry/', name: 'thanks_enquiry')] public function thanksEnquiry(Request $request): Response { return $this->render('/static/thanks-enquiry.html'); } }