Some Php Configuration

well i m member of this community since 3-4 year infinityfree is like my family if cant do anything or help anyone here then just go away man just dont reply to that post, thats it there are so many helpers and developers those helped me previously without any their own perosnal benifits if u cant do then just leave it i m sure there will someone who will help me for sure

Looking at this topic, helping you without personal benefit is exactly what @anon95807532, @anon42008019 and @ChrisPAR been doing here. They have been checking your code, suggesting improvements, and giving you new blocks of code that should fix it.

But what I also see is that you then took the improvements, tried to integrate it, but in doing so ended up making changes to the code that broke it again. I completely understand how it’s frustrating to them to have to develop your software for you in this way.

Looking at the code you’ve created, I don’t get the feeling that you properly understand either how the code works, is supposed to work, or even understand PHP features like functions and exceptions.

But I’m willing to give it a shot. That’s one shot, I can take the code you have and fix the syntax. Then it’s up to you to keep working on it.

To do so, please first share the latest code you have. That’s all relevant code as text , shared in a formatted code block in the forum. Not just a few lines, and definitely not screenshots.

7 Likes

require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/functions.php';
require_once __DIR__.'/config.php';

session_start();

// Basic check to make sure the form was submitted.
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
    redirectWithError("The form must be submitted with POST data.");
}

// Do some validation, check to make sure the name, email and message are valid.
if (empty($_POST['g-recaptcha-response'])) {
    redirectWithError("Please complete the CAPTCHA.");
}

$recaptcha = new \ReCaptcha\ReCaptcha(CONTACTFORM_RECAPTCHA_SECRET_KEY);
$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_REQUEST['REMOTE_ADDR']);

if (!$resp->isSuccess()) {
    $errors = $resp->getErrorCodes();
    $error = $errors[0];

    $recaptchaErrorMapping = [
        'missing-input-secret' => 'No reCAPTCHA secret key was submitted.',
        'invalid-input-secret' => 'The submitted reCAPTCHA secret key was invalid.',
        'missing-input-response' => 'No reCAPTCHA response was submitted.',
        'invalid-input-response' => 'The submitted reCAPTCHA response was invalid.',
        'bad-request' => 'An unknown error occurred while trying to validate your response.',
        'timeout-or-duplicate' => 'The request is no longer valid. Please try again.',
    ];

    $errorMessage = $recaptchaErrorMapping[$error];
    redirectWithError("Please retry the CAPTCHA: ".$errorMessage);
}

if (empty($_POST['name'])) {
    redirectWithError("Please enter your name in the form.");
}

if (empty($_POST['email'])) {
    redirectWithError("Please enter your email address in the form.");
}

if (empty($_POST['subject'])) {
    redirectWithError("Please enter your message in the form.");
}

if (empty($_POST['message'])) {
    redirectWithError("Please enter your message in the form.");
}

if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    redirectWithError("Please enter a valid email address.");
}

if (strlen($_POST['message']) < 10) {
    redirectWithError("Please enter at least 10 characters in the message field.");
}

// Everything seems OK, time to send the email.

$mail = new \PHPMailer\PHPMailer\PHPMailer(true);

    try {
        
    $mail = newMail();
    $mail = new \PHPMailer\PHPMailer\PHPMailer(true);
    $mail->SMTPDebug = CONTACTFORM_PHPMAILER_DEBUG_LEVEL;
    $mail->isSMTP();
    $mail->Host = CONTACTFORM_SMTP_HOSTNAME;
    $mail->SMTPAuth = true;
    $mail->Username = CONTACTFORM_SMTP_USERNAME;
    $mail->Password = CONTACTFORM_SMTP_PASSWORD;
    $mail->SMTPSecure = CONTACTFORM_SMTP_ENCRYPTION;
    $mail->Port = CONTACTFORM_SMTP_PORT;
    $mail->Priority = 1;
    $mail->setFrom(CONTACTFORM_FROM_ADDRESS, CONTACTFORM_FROM_NAME);
    return $mail;

}
    // SEND EMAIL FOR INTERNAL
$mail = newMail();
$mail->addAddress(CONTACTFORM_TO_ADDRESS, CONTACTFORM_TO_NAME);
$mail->Subject = "Dopenews-Contact us ".$_POST['Dopenews Support'];
$mail->Body    = <<<EOT
Name: {$_POST['name']}
Email: {$_POST['email']}
Message:
{$_POST['message']}
EOT;
$mail->send();

// SEND EMAIL FOR VISITOR
$mail = newMail();
$mail->addAddress($_POST['email'], $_POST['name']);
$mail->addReplyTo(CONTACTFORM_TO_ADDRESS, CONTACTFORM_TO_NAME);
$mail->Subject = "Dopenews-Contact us ".$_POST['Dopenews Support'];
$mail->Body    = <<<EOT
Name: {$_POST['name']}
Email: {$_POST['email']}
Message:
Thanks for contacting us we will Respond Shortly.
EOT;
$mail->send();


try {     
    $mail->Body    = <<<EOT  
    Name: {$_POST['name']} 
    Email: {$_POST['email']}
     Message:  {$_POST['message']}
      EOT;
      $mail->send();     
      redirectSuccess();           
  }    catch (Exception $ex) {     redirectWithError("An error occurred while trying to send your message.");     error_log("PHPMailer Exception: ".print_r($ex->errorMessage(),true));  }

```

Thank you for sharing the code.

Looking at what you currently have, there are a number of issues:

  • You’re attempting to create new email objects using a newMail() function, which @anon95807532 created here: Some Php Configuration - #4 by chiucs123. However, there is no function being created.
  • Where the function keyword should be is the try keyword. But a try must always be accompanied by a catch or finally, which are missing. Which is why the code is invalid.
  • You have a $mail->send() call for both the internal and visitor emails, without any error checking, then another $mail->send() call at the end, which does have error checking. But either PHPMailer will complain that you’re trying to send the message twice, or the message will just be sent twice to the visitor. You should only have a $mail->send() call once per email, and check for errors every time you do.
  • Just before the last send call, you have some trailing spaces after the <<EOT block. This is not valid PHP.
  • You’re calling error_log after redirectWithError. However, redirectWithError calls die() so, this code can never be reached.

When fixing all of these issues, and hitting the auto-format button on my editor, the final code is this:

fixed.php (3.7 KB)

This code should work. If you want to make any further changes, please make them yourself.

To do that, I VERY strongly recommend to use a device with a desktop OS and a proper IDE, like VSCode or PHPStorm, because those can inspect your code for you and point out obvious issues while you’re writing the code. Even experienced developers such as myself use it to catch errors, and it’s even more useful to inexperienced coders.

8 Likes

Thanks So Much Brother. :heart: Love you always You always Helped Us Whenever we need Yes I will Fix Try to Fix my Brain Issues also. Brother and maybe i will never disturb you again. :relieved: Have A Great Day Takecare!

Hi Admin, Ziverre and other devs,

I think it’s up to us as developers to build a sense of identifying those who simply ask for free help whenever they need it without ever thinking of respecting our profession. From my perspective, it was never about the help being free or not, it’s about whether they have honoured the programming task and appreciated the support. Whether other devs are generous enough and willing to help is their own choice, but I have over a decade of experience in both developing in the industry and answering support requests (both formal and informal ones), and see a typical group of people amongst those nationalities often asking for help in a passive-demanding manner.

They all follow the same pattern and attitude whenever asking for support and a hardship story that is not necessarily related to the issue. Most of the time I find it hard to believe as the hardship is far off-proportionate to the simple issue that they are facing. In very frank words, they aren’t looking for explanations, they want quick done-for-them solutions by abusing someone else’s empathy. They do not value the profession nor would they be thankful for the guidance unless you do things their way, which is the part where I find them not respecting the entire industry, and at which point I refuse to provide the ultimate done-for-you solution by pointing out the issue directly.

Normally this will result in a response of psychological insult and kidnapping your moral values by saying things like “If you only care about personal benefits, then please go away, someone else would have done this for me”.

That is the reason why these are easily identifiable and I’ll not provide the ultimate solution to safeguard my value.

The best way is to offer a complete guide as to how the issue can be solved, forcing them to spend effort to actually look into the problem and solve it themselves. This way you’re not refusing to help, but you refuse to offer the ultimate solution. Those who honour the profession will find this very easy to follow and move on, while those who seek quick solutions will have their caps-lock on with a demanding tone, or try to shift the focus to a more convincing hardship story.

If those genuinely need the help, we will devote effort in explaining and helping them to learn as much time as they need but giving them the solution straight off the table doesn’t help them in the long run, but also facilitates the growth of this “just give me solution” culture and our community will be flooded with this type of requests. Consider this as anti-plagiarism on assignments back in school days.

For devs: If you do not respect your work, no one else would.

Cheers!

4 Likes

Its On Upto You How You Takes Other Words I cannot change Your thoughts but i always Respect Those Helped Us Even I said You too for that because you Replied me for the first I said That Because You guys knows that if they Helped someone it’s on upto him but the tough he says that wasnt fine whatver just leave that topic

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.