Email Question

Issues related to forms.
Post Reply
peters
 
 
Posts: 65
Joined: Thu Sep 28, 2023 12:29 pm

Email Question

Post by peters »

Good day,

I have a question about emailing with the built in PHP form processing.
The question is not how to write the script or how to use the PHPMailer that WWB uses internally as this is the PHPMailer library from the github site from what I can see in the code used.

This is a question about the order of things and when the email is actually being sent?
I have a form and have 7-8 fields in the form and in the PHP script I have validation checks for each field.
If say a required input is missing I call up a input_error webpage that outlines the error and takes the user back to the original input form to fix the error.

When I check off: "The Use built in PHP processor script", the email is triggered on a validation call to the PHP code with the email sending code in it. See beginning of the php code below with the function ValidateEmail($email)
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require __DIR__.'/Exception.php';
require __DIR__.'/PHPMailer.php';
require __DIR__.'/SMTP.php';

function ValidateEmail($email)
{
$pattern = '/^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i';
return preg_match($pattern, $email);[/size][/size]

My issue is this is called and by-passes the other validation code I have in the main form's PHP script.
So the beginning of my php script for the form is shown below . . .
<?php
session_start();
$mysql_server = 'localhost';
$mysql_username = 'appusers';
$mysql_password = 'xxxxxxxxxxxxxx';
$mysql_database = 'Pickups';
$mysql_table = 'cuslogin';
$success_page = './cus-VerifyAccnt.php';
$error_page = './cus-AccntFailed.php';
$error_message = "";
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['form_name']) && $_POST['form_name'] == 'signupform')
{
$newusername = $_POST['username'];
$newemail = $_POST['email'];
$newpassword = $_POST['password'];
$confirmpassword = $_POST['confirmpassword'];
$newfullname = $_POST['fullname'];
$city = $_POST['city'];
$address = $_POST['address'];
$province = $_POST['province'];
$postalcode = $_POST['postalcode'];
$mobilephone = $_POST['mobilephone'];
$homephone = $_POST['homephone'];
$code = 'NA';
[/size][/size]

Example of my validation code for address . .
if (strlen($address) == 0)
{
$error_message = 'Address is missing and required!';
$_SESSION['errmsg'] = $error_message;
header('Location: '.$error_page);
}

Is there any trick that can be done so that the email function fires off after all my validation checks have been completed and then send the email?

I realize I can just cut and past the PHPMailer code where I want and rename the function and call it where I want, bu if I do that each time I generate the project my custom copy and paste code gets overwritten and I manually have to copy and paste again.

So is there a way in the WWB interface itself to change when the PHPMailer function call happens?

Pete,
User avatar
Pablo
 
Posts: 23608
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: Email Question

Post by Pablo »

I'm sorry, you cannot change the order of the code.
peters
 
 
Posts: 65
Joined: Thu Sep 28, 2023 12:29 pm

Re: Email Question

Post by peters »

Thanks for your confirmation on this. Wanted to make sure that I was not missing something in the WWB interface.
I would say however, the PHP internal emailer would be far better if it were to be called at the end of all validations on the form and after the insert of the actual data into the table.
Right now I can see of no way other than manually placing in the emailer code in every form I want to have an auto-response emails sent on and the only reason for this is that the email event is firing off too early.
I'm going to take a hard look at this one and see if I can find an easier work-a-round versus manually pasting code into 4-5 forms every time I regenerate the project.
I was thinking that maybe I could add a secondary success webpage to be called when all the form data has been validated and after INSERT and on this page add the PHPmailer?

This is kind of what I am thinking . . .
After the INSERT statement, take the focus to a dummy $success_Page
$sql = "INSERT `".$mysql_table."` (`username`, `password`, `fullname`, `email`, `active`, `code`, `role`, `address`, `city`, `province`, `postalcode`, `mobilephone`, `homephone`) VALUES ('$newusername', '$crypt_pass', '$newfullname', '$newemail', 1, '$code', '', '$address', '$city', '$province', '$postalcode', '$mobilephone', '$homephone')";
$result = mysqli_query($db, $sql);
// - Store the newusername into a session memvar to be passed into the $success_page
// - The $success_page is physically named: './Cus-AccntCreated.php'
$_SESSION['username'] = $newusername;
$_SESSION['email'] = $newemail;
// - Add in below all inputted form date you want to be emailed out.
header('Location: '.$success_page);
exit;
I would declare SESSION memory variables for the inputted data to expose them in this secondary success_page and from there use the PHP Built in emailer to email the form data out to the user.
This would leave my input forms stock as a $success_page and an $error_page seems to be common all through the generated code.

I'll keep you posted on this with a follow up in a few day!

Have a great weekend!
Pete,
peters
 
 
Posts: 65
Joined: Thu Sep 28, 2023 12:29 pm

Re: Email Question

Post by peters »

Just in followup the previous methods I was going to try in my project worked out extremely well.
Pete,
Post Reply