Form writing to DB also attempting to send email

Issues related to forms.
Post Reply
User avatar
Magical
 
 
Posts: 111
Joined: Thu Dec 14, 2017 5:08 pm
Contact:

Form writing to DB also attempting to send email

Post by Magical »

Greetings,

On a form which is only writing to MYSQL DB the PHP script also tries to send an email to 'yourname@address.com'. This gets bounced by the email server and is tracked as spam. For a future version I would suggest that the option should be selectable send email or write to db or both. I ended up with several emails in the server temp queue all sent from yourname@address.com. If you leave it blank then it errors out due to the validate email function.

So far I patched it using the custom form processing feature and setting $mailto=''; added just before the if $mailto !='';

Once again I am amazed at some of the flexibility built in, but of course we will be asking for more 8) 8) :lol:


if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['formid']) && $_POST['formid'] == 'someformid')
{
$mailto = 'yourname@address.com';
$mailfrom = isset($_POST['email']) ? $_POST['email'] : $mailto;
$subject = 'Contact Information';
$message = 'Values submitted from web site form:';
$success_url = './reunion_tracker_success.html';
$error_url = './reunion_tracker_error.html';
$mysql_server = 'localhost';
$mysql_database = 'someDB';
$mysql_table = 'someTable';
$mysql_username = 'someUser';
$mysql_password = 'somePwd';

*** Irrelevant lines removed to improve issue readability ***
try
{
if (!ValidateEmail($mailfrom))
{
$error .= "The specified email address (" . $mailfrom . ") is invalid!\n<br>";
throw new Exception($error);
} ** This will terminate the POST process if the mailfrom is left blank.

*** Irrelevant lines removed to improve issue readability ***
if ($mailto != '')
{
mail($mailto, $subject, $body, $header);
} ** this attempts to send the mail to the default "youremail@address.com"


*** Irrelevant lines removed to improve issue readability ***
** here we prepare the SQL and write to the DB ***
mysqli_query($db, "INSERT INTO $mysql_table (`DATESTAMP`, `TIME`, `IP`, `BROWSER`, `REFERER`)
VALUES ('".date("Y-m-d")."',
'".date("G:i:s")."',
'".$_SERVER['REMOTE_ADDR']."',
'".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."',
'".$_SERVER['HTTP_USER_AGENT']."')")or die('Failed to insert data into table!<br>'.mysqli_error($db));
$id = mysqli_insert_id($db);
catch (Exception $e)
{
$errorcode = file_get_contents($error_url);
$replace = "##error##";
$errorcode = str_replace($replace, $e->getMessage(), $errorcode);
echo $errorcode;
}
exit;
}
?>
User avatar
Pablo
 
Posts: 21708
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: Form writing to DB also attempting to send email

Post by Pablo »

If you leave the email address in the form properties empty then no email will be sent.
See also the notes in the help.
User avatar
Magical
 
 
Posts: 111
Joined: Thu Dec 14, 2017 5:08 pm
Contact:

Re: Form writing to DB also attempting to send email

Post by Magical »

That works. Thanks!
Post Reply