Hi Cumbe
1. First, Sorry I have written so much ! !
2. I am using your Contact Form plugin in the main body of a page, using the tag format (% cbcontact …. %) . Also I am wanting to use the PHPMailer option for sending an email because I need SMTP authorisation with username and password
3. Note I have implemented the code for the PHPMailer which now seems to work for me. Note the SMTP settings are specific to me, eg I defined the server as localhost, because on the hoster, the smtp mail server is local, etc .. I attach the moded comprueba.php for info
4. But in the process, I have seen a few problems with Contact Form .. in the area of the PHPMailer and also disabling captcha validation ….
Serious problems
5. If have a mail send SMTP problem with PHPMailer, eg the server name is not valid or bad To address, then we get a crash with no form display, because of the following PHPMailer uncaught exception
a. Fatal error: Uncaught exception 'phpmailerException' with message 'SMTP Error: Could not connect to SMTP host.' …
b. Adding in a try catch block solves this … see attached code
6. For the PHPMailer you need a foreach loop to add the other GS user TO addresses … see attached code
7. You have $message->FromName = $from; .. where $from is the from email address from the form field ‘Email’ , but $message->FromName is intended for the from name that appears infront of the from email address , eg From : fromName <me@myemail.com>
a. This gives a mail send SMTP Error
b. This should be
i. $message->From = $from;
ii. $message->FromName = $_POST['contact'][i18n_r('cbcontact/Nb')]; .
c. Where
i. $message->From is the from email address
ii. $message->FromName is the name that appears infrin of the from email address , eg From : fromName <me@myemail.com>
8. $ captcha problem …
a. If I enter false or true in (% cbcontact …. %) for the $captcha boolean parameter, then the function ALWAYS wants to validate the captcha code.
b. That is, it seems that in the captcha validate code the IF statement : if ($captcha == "true" or $captcha == true) ALWAYS returns true, independent of the value of $captcha …
c. I did a test to confirm this, where in my case I am using the (% .. %) syntax, and found that $captcha is a STRING, and it seems from the reference below, that if $captcha = “true” or “false”, then the statement if ($captcha = = true) ALWAYS returns true. It only returns false if $captcha = “0” or “”
d. Check ref :
http://stackoverflow.com/questions/47752...lue-in-php
e. Note - I understand that if I use the function directly then the variable $captcha is a boolean … so IN GENERAL it can be EITHER a string or a boolean …
i. In a boolean comparison, String "" and "0" are considered FALSE, else true
ii. if ($mybool == "hello") => It looks like it converts the left hand side to "" if false, or "1" if true, and then does the comp
iii. if ($mystring == true) => It looks like if $mystring is equal to "0" or "", then the IF equates to false, else it equates to true … so if (“false” == true) equates to TRUE !!
f. Also have the same problem with
if ($sendphpmail == false or $sendphpmail == 'false') {
… Use simple PHP mail()
} else if ($sendphpmail == true or $sendphpmail== 'true') {
… Use PHPMailer class
}
… but here this works … for all cases “false”, false, “true”, true … because the first IF tests for “false”/false and not “true”/true …
BUT I think the following would NOT work , even though the sense is the same
if ($sendphpmail == true or $sendphpmail== 'true') {
… Use PHPMailer class
} else if ($sendphpmail == false or $sendphpmail == 'false') {
… Use simple PHP mail()
}
… because the first test ($sendphpmail == true) would return true for BOTH strings “true” and “false” .. ! !
g. SUGGESTION
i. Convert $captcha (which can be either string or boolean) to a DEFINITE boolean, eg $captchaboolean, BEFORE it is used in the captcha validation code … and then it is clear what is happening … Then the validation code can use a simple
if ($captchaboolean = = = true) { do captcha validation }
ii. Looking at the above url ref, I think the following works
$captchaboolean = false;
if (($captcha == false) or (strtolower($captcha) == "false"))
{ $captchaboolean = false; }
else
{ $captchaboolean = true; }
iii. The above gives : $captcha -> $captchabool : "1" ->T, "true"->T, "TRUE"->T, "0"->F, "false"->F, "FALSE"->F, ""->F, "Any"->T, true->T, false->F
iv. … Also can do the same for $sendphpmail … and then do not have to have the complicated if statement above for $sendphpmail for choosing the mail send method. Instead can do
if ($sendphpmailboolean = = = false) {
… Use simple PHP mail()
}
else
{
… Use PHPMailer class
}
v. Also $echocontact .. ??
vi. Can put string/bool to Bool f conversion in a helper fn
Minor Problems
9. $result variable in comprueba.php
a. Mixes string and int .. confusing .. but seems to work
b. Just to say : the php simple mail() function returns a boolean true = success / false = send error
c. And the PHPMailer class send() function also returns boolean true/false
d. .. So why not assign / compare booölean values true/false instead of string / integer ?? … it would be less confusing
10. Why have $message->MsgHTML("$body") and not $message->MsgHTML($body); .. no “ ” around $body => but BOTH ways seem to work anyway.
11. If have a mail send SMTP problem with PHPMailer, eg the server name is not valid or bad To address, then the displayed form fields are reset to empty .. should these be left filled if we have an error ? … the user may think that he email has been sent ..
12. If have a mail send SMTP problem with PHPMailer, eg the server name is not valid or bad To address, then the Javascript alert box is not displayed … So you do not see in he alert box the error message "MSG_guestERR" => 'There was an error sending your message'. I added an echo statement directly before the Javascript alert code and this DOES SHOW the message 'There was an error sending your message' … ? I am not sure why the alert box does not show .. ..
13. Can you do a better docs text description of the installation / How to use the plugin / description of the cbcontact_page( ) function input parameteres … In the beginning I did not fully understand this !
14. Have a warning : Notice: Undefined index: cbcontact/CONTACT in … \plugins\cbcontact_form.php on line 43 ( Line : add_action('pages-sidebar','createSideMenu',array('cbcontact_form', $i18n['cbcontact/CONTACT'])); )
Feature request
15. Feature question - If disable captcha then also not display it ?
Other stuff
16. What does function vermensajes() do ?
Cheers Aldebaran
Moded comprueba.php file :
PHP Code:
<?php
//-----------------------------------------------------------------//
//--- comprueba.php: action if form is submit //
//-----------------------------------------------------------------//
if (!isset($_SESSION)) { session_start(); }
$imagenCadena = $_SESSION["imagencadena"];
$pot = trim(strtolower($imagenCadena));
$server_name = getenv ("SERVER_NAME"); // Server Name
$request_uri = getenv ("REQUEST_URI"); // Requested UR
// check antispam
if (isset($_POST['contact-submit'])) {
if ($_POST['contact']['leaveso'] != 'leaveso' or $_POST['contact']['leaveblank']!=''){
echo '<html>';
echo '<head>';
echo '</head>';
echo '<body>';
echo '<div style="padding: 20px; border: 4px double; margin: 20%;">';
echo i18n_r('cbcontact/spam').'<br />';
echo '<a href="'.$idpret.'">'.i18n_r('cbcontact/back').'</a>';
echo '</div>';
echo '</body>';
echo '</html>';
exit;
}
}
//------------------------
//START CONTACT
//------------------------
if (@$_GET['tp'] == 'contact') {
if (isset($_POST['contact-submit'])) {
// -------------------------------------------------- //
// Aldebaran Mod START //
// -------------------------------------------------- //
// $captcha -> $captchabool : "1" ->T, "true"->T, "TRUE"->T, "0"->F, "false"->F, "FALSE"->F, ""->F, "Any"->T, true->T, false->F
$captchaboolean = false;
if (($captcha == false) or (strtolower($captcha) == "false"))
{ $captchaboolean = false; }
else
{ $captchaboolean = true; }
// Check captcha
if ($captchaboolean === true) {
// ORIG if ($captcha == "true" or $captcha == true) {
// -------------------------------------------------- //
// Aldebaran Mod END //
// -------------------------------------------------- //
if ( $pot == trim(strtolower($_POST['contact']['pot']))) {
$err = '';
} else {
$err = i18n_r('cbcontact/MSG_CAPTCHA_FAILED');
}
}
//check name
if ( $_POST['contact'][i18n_r('cbcontact/Nb')] != '' ) {
if ($_POST['contact'][i18n_r('cbcontact/Nb')] == i18n_r('cbcontact/Nb')){
$_POST['contact'][i18n_r('cbcontact/Nb')] = '';
}
}
//check email
if ( $_POST['contact'][i18n_r('cbcontact/Em')] != '' ) {
if ($_POST['contact'][i18n_r('cbcontact/Em')] == '(*)'.i18n_r('cbcontact/Em')) {
$_POST['contact'][i18n_r('cbcontact/Em')] = ''; //i18n_r('cbcontact/Em');
}
if ($_POST['contact'][i18n_r('cbcontact/Em')] != i18n_r('cbcontact/Em')){
$from = $_POST['contact'][i18n_r('cbcontact/Em')];
} else {
$_POST['contact'][i18n_r('cbcontact/Em')]='';
}
}
//check subject
$subject2 = '';
if ( $_POST['contact'][i18n_r('cbcontact/Sub')] != '' ) {
if ($_POST['contact'][i18n_r('cbcontact/Sub')] != i18n_r('cbcontact/Sub')){
$subject = stripslashes(html_entity_decode($_POST['contact'][i18n_r('cbcontact/Sub')]));
} else {
$subject = i18n_r('cbcontact/CONTACT_FORM_SUB').' '.i18n_r('cbcontact/WHO').' '.$SITENAME;
$_POST['contact'][i18n_r('cbcontact/Sub')] = i18n_r('cbcontact/CONTACT_FORM_SUB').' '.i18n_r('cbcontact/WHO').' '.$SITENAME;
$subject2 = i18n_r('cbcontact/Sub');
}
}
//check message
if ( $_POST['contact'][i18n_r('cbcontact/Ms')] != '' ) {
if ($_POST['contact'][i18n_r('cbcontact/Ms')] == '(*)'.i18n_r('cbcontact/Ms')) {
$_POST['contact'][i18n_r('cbcontact/Ms')] = '';
}
if ($_POST['contact'][i18n_r('cbcontact/Ms')] == i18n_r('cbcontact/Ms')){
$_POST['contact'][i18n_r('cbcontact/Ms')] = '';
}
}
$temp = $_POST['contact'];
//release variables
unset($temp['pot']);
unset($temp['contact-submit']);
unset($temp['submit']);
unset($temp['leaveso']);
unset($temp['leaveblank']);
if ($err == '' && trim($_POST['contact'][i18n_r('cbcontact/Em')]) !='' && trim($_POST['contact'][i18n_r('cbcontact/Ms')]) !='') {
$headers = "From: ".$from."\r\n";
$headers .= "Return-Path: ".$from."\r\n";
$headers .= "Content-type: text/html; charset=UTF-8 \r\n";
$body = '"'.$subject.'": <a href="'.substr($idpret,0,-1).'">'.substr($idpret,0,-1).'</a>';
$body .= "<hr><br />";
if ( ! file_exists($log_file) ) {
$xml = new SimpleXMLExtended('<channel></channel>');
} else {
$xmldata = file_get_contents($log_file);
$xml = new SimpleXMLExtended($xmldata);
}
$thislog = $xml->addChild('entry');
$thislog->addChild('date', date('r'));
$cdata = $thislog->addChild('captcha');
$cdata->addCData($captcha);
$cdata = $thislog->addChild('ip_address');
$ip = getenv("REMOTE_ADDR");
$cdata->addCData(htmlentities($ip, ENT_QUOTES, 'UTF-8'));
$body .= "Ip: ". $ip ."<br />";
foreach ( $temp as $key => $value ) {
if (substr($key, 0, 2) != 'q_') {
$body .= ucfirst($key) .": ". stripslashes(html_entity_decode($value, ENT_QUOTES, 'UTF-8')) ."<br />";
$cdata = $thislog->addChild(clean_url($key));
$cdata->addCData(stripslashes(html_entity_decode($value, ENT_QUOTES, 'UTF-8')));
}
}
XMLsave($xml, $log_file);
//$seg = 2;
$result = 0; // Aldebaran question, Why not : $result = false;
if ($sendphpmail == false or $sendphpmail == 'false') {
$result = mail($EMAIL,$subject,$body,$headers);
if ($usus != ''){
foreach ($qusus as $key=>$value){
$result = mail($value,$subject,$body,$headers);
$result1[$key] = $result;
}
}
} else if ($sendphpmail == true or $sendphpmail== 'true') {
//echo 'GSPLUGINPATH: '.GSPLUGINPATH.'<br /<';
if (is_dir(GSPLUGINPATH.'PHPMailer_v5.1') and file_exists(GSPLUGINPATH.'PHPMailer_v5.1/class.phpmailer.php')){
require(GSPLUGINPATH.'PHPMailer_v5.1/class.phpmailer.php');
$message = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
//GMAIL Configuration
/* $message->SMTPSecure = "ssl"; // sets the prefix to the servier
$message->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP se
$message->Port = 465; // set the SMTP port for the GMAIL server
$message->Username = "....@gmail.com"; // GMAIL user account: youuser@gmail.com
$message->Password = "...."; // GMAIL password
$message->From = "....@gmail.com"; // you GMAIL email
*/ //end GMAIL Configuration
//ONO configuration
/* $message->SMTPSecure = ""; // sets the prefix to the servier
$message->Host = "smtp.ono.com"; // sets ONO as the SMTP server
$message->Port = 25; // set the SMTP port for the ONO server
$message->Username = "username"; // ONO username
$message->Password = "pass"; // ONO password
$message->From = "user@ono.com"; // you ono email
*/ //end ONO Configuration
//HOTMAIL configuration
/* $message->SMTPSecure = "tls"; // sets the prefix to the servier
$message->Host = "smtp.live.com"; // sets hotmail as the SMTP server
$message->Port = 587; // set the SMTP port for hotmail server
$message->Username = "youruser@hotmail.com"; // hotmail user account
$message->Password = "yourpass"; // hotmail password
$message->From = "youruser@hotmail.com"; */ // you hotmail email
//end HOTMAIL Configuration
$message->CharSet = "utf-8";
$message->SMTPDebug = false; // enables SMTP debug information (for testing)
// false = disabled debug
// 1 = errors and messages
// 2 = messages only
$message->IsSMTP(); // telling the class to use SMTP
$message->SMTPAuth = true; // enable SMTP authentication
// -------------------------------------------------- //
// Aldebaran Mod START //
// -------------------------------------------------- //
// Aldebaran : In my case I do not need to set this
// $message->SMTPSecure = "ssl";
// Aldebaran : As this email.php script lives on the same server as
// my email server I am etting the HOST to localhost
$message->Host = "localhost"; // specify main and backup server
$message->Port = 25; // Don't actually need to set this, 25 is the default anyway
// Set the SMTP server username/password
$message->Username = "myusername"; // SMTP username
$message->Password = "mypassword"; // SMTP password
// set word wrap to 50 characters
$message->WordWrap = 50;
// set email format to HTML
$message->IsHTML(true);
$fromEmail = $from;
$fromName = $_POST['contact'][i18n_r('cbcontact/Nb')];
$message->From = $fromEmail; // The FROM email address - Form field 'Email'
$message->FromName = $fromName; // Form field 'Name' => The from name seen outside of the < > of the From email address
$message->Subject = $subject; // Form field 'Subject'
$message->MsgHTML($body); // Set the message body
// Add the TO email address of the main GS admin user.
$message->AddAddress($EMAIL, '');
// echo 'Sent email to : '.$EMAIL.'<br>';
// Add the TO email addresses of the other GS users
if ($usus != ''){
foreach ($qusus as $key=>$value) {
$message->AddAddress($value, '');
// echo 'Sent email to : '.$value.'<br>';
}
}
try {
$result = $message->Send();
}
catch (phpmailerException $ex){
// echo $ex;
}
catch (Exception $ex){
// echo $ex;
}
// -------------------------------------------------- //
// Aldebaran Mod END //
// -------------------------------------------------- //
} else {
echo strtoupper(i18n_r('cbcontact/errphphmail'))."\n";
}
}
//results
if ($result == '1') { // Aldebaran question : Why not : if ($result == true)
$msgshw = i18n_r('cbcontact/MSG_CONTACTSUC');
} else {
//$seg = 10;
$msgshw = '<p>'.i18n_r('cbcontact/MSG_guestERR')."</p>\n";
$msgshw .= "Mailer Error: " . $message->ErrorInfo;
}
} // finaliza if $err=''
else
{ // if $err != ''
if ($subject2 != '') {
$temp[i18n_r('cbcontact/Sub')] = $subject2;
}
foreach($temp as $key=>$value){
$mi_array[$key] = stripslashes(html_entity_decode($value));
}
if (trim($err) !=''){
$msgshw = $err.'\nCaptcha code: '.$pot.'\nCode wrote: '.$_POST['contact']['pot'];
} else {
$msgshw = '*** '.strtoupper(i18n_r('cbcontact/Co')).' ***';
$err = $msgshw;
}
}
}
}
// Aldebaran Mod .. Add some Echos
echo '<br> msgshw = '.$msgshw.'<br>';
echo '<br>';
echo '<br> JUST BEFORE Javascript Alert code <br>';
////////////////////////////////////////////////////////////////
//
// html page or alert of javascript
//
////////////////////////////////////////////////////////////////
?>
<script type="text/javascript">
<!--
alert ("<?php echo $msgshw; ?>");
-->
</script>
<?php
/*
echo '<html>';
echo '<head>';
echo '<meta http-equiv="Refresh" content="'.$seg.';url=http://'.$server_name.$MIURL.$miarr.'">';
echo '</head>';
echo '<body>';
echo '<div style="padding: 20px; border: 4px double; margin: 20%;">';
echo $msgshw;
echo i18n_r('cbcontact/redir'].$seg.i18n_r('cbcontact/redir1'].i18n_r('cbcontact/back'].'.<br />';
echo '<a href="http://'.$server_name.$MIURL.$miarr.'">'.i18n_r('cbcontact/back'].'</a>';
echo '</div>';
echo '</body>';
echo '</html>';*/
?>