Some Php Configuration

Hello ! everyone I need an Help regarding that my Contact actually i am using php mailer but i just want to add an Auto Responer Mail Which Will send to that person whom trying to “contact” my website is dopenews.in/contact us via email so he will recieve an email for Like :“Thanks for contacting us we will Respond Shortly” i can show my mailer functions

<?php

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 {
    //Server settings
    $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; // High priority flag is set.  

    // Recipients
    $mail->setFrom(CONTACTFORM_FROM_ADDRESS, CONTACTFORM_FROM_NAME);
    $mail->Priority = 1; // High priority flag is set.  
	$mail->addAddress(CONTACTFORM_TO_ADDRESS, CONTACTFORM_TO_NAME);
    $mail->addReplyTo($_POST['email'], $_POST['name']);

    // Content
    $mail->Subject = "Dopenews-Contact us ".$_POST['Dopenews Support'];
    $mail->Priority = 1; // High priority flag is set.  

    $mail->Body    = <<send();
    redirectSuccess();
}
catch (Exception $ex) {
    redirectWithError("An error occurred while trying to send your message.");
    error_log("PHPMailer Exeption: ".print_r($ex->errorMessage(),true));  

Hi dopebrest,

The code you shared should be working fine with 2 assumptions, 1) all functions called exist and are functional and 2) the constarts are properly defined.

To change the content of the email, you can focus on $mail->Body, where you have already included the message inputted by the user in the contact form. From your statement, you want to change this part to your message, i.e.: "Thanks for contacting us we will Respond Shortly”.

However, you still need some sort of record for the user message so you know what to reply. Therefore, you want to either create another email for yourself recording everything or saving the message into a database for your own reference.

Cheers!

2 Likes

atually i am using gmail smtp so i dont want to use database to this perform i want the system automatically detect that whoever used our Customer support for any issue they get auto mail from the same email which i have been set for mailing. just add an code for auto email using same configguration setting. i can show you all of my mailer files
[m.phpmailer.zip - Google Drive]

Hi dopebrest,

You need to send 2 emails using the same smtp configuration in this case while specifying a different address in each case.

<?php

function 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();

Cheers!

2 Likes

As a practice, do not dump the whole code like that, you should format your code as this for readablity:
```
Test
```

4 Likes

not working i am facing now https error (ssl error or what didnt get that

Hi dopebrest,

HTTPS errors relates to your browser connectivity to the server being correctly encrypted or not, rather than the code. Please share a screenshot so we can see better and solve the issue together. :+1:

Cheers!

Hi Ziverre,

Maybe he/she tried, but there are some special characters in between that broke the formatting, which is understandable.

Cheers!

1 Like

i have already shown u my website and all my php mailer brother i can show u what i have done now before my email was working but after that auto responder i m facing that http error now. please check my Php mailer Correcly. i have already mentioned Function.php in my and config and .php also on my previous post You can also try by visiting my site dopenews.in/contact

<?php

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 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();

    $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 Exeption: ".print_r($ex->errorMessage(),true)); 
}

What is your website’s URL, and do you have an SSL certificate installed?

2 Likes

i have valid ssl for my site but still that issue u can see through my website -_- its fully protected i have mentioned my website you can check it through

Sorry, I missed the last part of your previous message.

I do not get an SSL error, but rather an HTTP ERROR 500. Can you activate the Display Errors setting and see what you get?

5 Likes

No Problem but…I have already Deleted my htacess file i have removed all permission.

I don’t suspect your .htaccess, I think the error may be in your PHP code. Can you do this?

6 Likes

Done i have enabled that

Thats an error

Parse error : syntax error, unexpected identifier “newMail”, expecting “{” in htdocs/submit.php on line 67

This line is incorrect:

try newMail(){

You shouldn’t have anything after the try statement, it should be:

try {
    /* your code here */
}
catch (Exception $ex) {
    /* your code here */
}

Your code seems to be structured wrong in any case, as you close your try statement, then try to call the function newMail() (which you have not declared, I’m guessing it is meant to be where the try statement is) and later on have a catch statement that does not do anything on its own.

Did you try to copy @anon95807532’s code? If so you might want to replace lines 67 up to the end with something like this:

function 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;
}

try {
	// 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();
}
catch (Exception $ex) {
    redirectWithError("An error occurred while trying to send your message.");
    error_log("PHPMailer Exeption: ".print_r($ex->errorMessage(),true)); 
}

try {
	// 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();
	redirectSuccess();
}
catch (Exception $ex) {
    redirectWithError("An error occurred while trying to send your message.");
    error_log("PHPMailer Exeption: ".print_r($ex->errorMessage(),true)); 
}

I wrapped the internal and visitor emails in two separate try-catch statements, as I was unsure if it was right to have them merged in one. I think there might be something wrong with the way you use error_log as well (does redirectWithError not prevent it from executing?), but I’m unsure.

5 Likes

I am using an Cloudflare i didnt do anything with Error Logs You can Check my Php mailer Dude i have sent All My files to you With with Full Trust ‘:’) Actually u told to add Those lines For new mail thats why i have Tried So What Code should i use on their instead of that i am also New in this So dont know much instead Of Html thats why i am asking for that code help… sorry if you mind… :sweat_smile: i had Set my Functions setting i configured my Functions.php also doesnt seems any error from there

Cloudflare is unrelated to the current issue that you are facing, 500 indicates that there is a problem with the code when the display error isn’t enabled.

Getting started with something complex such as that as a starter is not like shooting a fish in the barrel, community members here provide you the help as if you are able to help yourself based on the provided answer or feedback. You need to make use of Google or the search engine of preference, learn basics such as how to debug etc.

I understand that this may not be the response that you were looking for nor an answer, but learning at least the basics will save you and others from headache.

5 Likes

Hi dopebrest,

My code was meant to demonstrate what you’re trying to achieve, I didn’t expect a full copy and paste from your end.

For try catch blocks, please refer to ChrisPAR for the correct syntax and apply the code to the appropriate positions, which he has already pointed for you.

For me, I’m not simply taking a stack of code and have the solution done for you. You have to learn and code it yourself. It was never about trust issues.

Cheers!

4 Likes