Form doesn't work

4 replies · opened Jul 10, 2023

AassiyadesignJul 10, 2023

Hello, SemiColonWeb,

This is my code:

$mail = new PHPMailer();

/* Add your SMTP Codes after this Line */ $mail->IsSMTP(); $mail->Host = “xxxx.allstarco.swanmedia.ca”; $mail->SMTPDebug = 0; $mail->SMTPAuth = true; $mail->SMTPSecure = “ssl”; $mail->Port = 587; $mail->Username = “xxxxx”; $mail->Password = “xxxxx”; // End of SMTP``

Redirected to the form.php page and got an error after submitting:

{ “alert”: “error”, “message”: “Email could not be sent due to some Unexpected Error. Please Try Again later.

Reason: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/TroubleshootingSMTP server error: Failed to connect to server Additional SMTP info: php_network_getaddresses: getaddrinfo failed: Name or service not known” }

Thank you


My whole form.php

 'info@swanmedia.ca', // Your Email Address
	'name' => 'Swan Media' // Your Name
);

/*-------------------------------------------------
	Sender's Email
---------------------------------------------------*/

$fromemail = array(
	'email' => 'info@swanmedia.ca', // Company's Email Address (preferably currently used Domain Name)
	'name' => 'Swan Media' // Company Name
);

/*-------------------------------------------------
	reCaptcha
---------------------------------------------------*/

// Add this only if you use reCaptcha with your Contact Forms
$recaptcha_secret = ''; // Your reCaptcha Secret

/*-------------------------------------------------
	hCaptcha
---------------------------------------------------*/

// Add this only if you use hCaptcha with your Contact Forms
$hcaptcha_secret = ''; // Your hCaptcha Secret

/*-------------------------------------------------
	PHPMailer Initialization
---------------------------------------------------*/

$mail = new PHPMailer();
/* Add your SMTP Codes after this Line */
$mail->IsSMTP();
$mail->Host = "em6131.allstarco.swanmedia.ca";
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Port = 587;
$mail->Username = "assernfo";
$mail->Password = "as68sm88";
// End of SMTP

/*-------------------------------------------------
	Form Messages
---------------------------------------------------*/

$message = array(
	'success'			=> 'We have **successfully** received your Message and will get Back to you as soon as possible.',
	'error'				=> 'Email **could not** be sent due to some Unexpected Error. Please Try Again later.',
	'error_bot'			=> 'Bot Detected! Form could not be processed! Please Try Again!',
	'error_unexpected'	=> 'An **unexpected error** occured. Please Try Again later.',
	'captcha_invalid'	=> 'Captcha not Validated! Please Try Again!',
	'captcha_error'		=> 'Captcha not Submitted! Please Try Again.'
);

/*-------------------------------------------------
	SPAM Protection Settings
---------------------------------------------------*/

$spam_keywords = array(
	'viagra',
	'cialis',
	'levitra'
);

$allowed_urls = 1;

/*-------------------------------------------------
	Form Processor
---------------------------------------------------*/

if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {

	$prefix		= !empty( $_POST['prefix'] ) ? $_POST['prefix'] : '';
	$submits	= $_POST;
	$botpassed	= false;

	$message_form					= !empty( $submits['message'] ) ? $submits['message'] : array();
	$message['success']				= !empty( $message_form['success'] ) ? $message_form['success'] : $message['success'];
	$message['error']				= !empty( $message_form['error'] ) ? $message_form['error'] : $message['error'];
	$message['error_bot']			= !empty( $message_form['error_bot'] ) ? $message_form['error_bot'] : $message['error_bot'];
	$message['error_unexpected']	= !empty( $message_form['error_unexpected'] ) ? $message_form['error_unexpected'] : $message['error_unexpected'];
	$message['captcha_invalid']	= !empty( $message_form['captcha_invalid'] ) ? $message_form['captcha_invalid'] : $message['captcha_invalid'];
	$message['captcha_error']		= !empty( $message_form['captcha_error'] ) ? $message_form['captcha_error'] : $message['captcha_error'];

	/*-------------------------------------------------
		Bot Protection
	---------------------------------------------------*/

	if( isset( $submits[ $prefix . 'botcheck' ] ) ) {
		$botpassed = true;
	}

	if( !empty( $submits[ $prefix . 'botcheck' ] ) ) {
		$botpassed = false;
	}

	if( $botpassed == false ) {
		echo '{ "alert": "error", "message": "' . $message['error_bot'] . '" }';
		exit;
	}

	/*-------------------------------------------------
		SPAM Protection
	---------------------------------------------------*/

	function spam_keyword_check( $submitted, $spamwords ) {
		if( is_array( $submitted ) ) {
			return false;
		}
		if( !is_array( $spamwords ) ) $spamwords = array( $spamwords );
		foreach( $spamwords as $spamstring ) {
			if( ( $position = stripos( $submitted, $spamstring ) ) !== false ) return $position;
		}
		return false;
	}

	function spam_url_check( $submitted ) {
		if( is_array( $submitted ) ) {
			return false;
		}

		$pattern = "/(http|https)\:\/\/(www\.)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

		if( preg_match_all( $pattern, $submitted, $urls ) ) {
			return count($urls);
		}

		return false;
	}

	foreach( $submits as $spam_submit ) {
		if( spam_keyword_check( $spam_submit, $spam_keywords ) || spam_url_check( $spam_submit ) > $allowed_urls ) {
			// A successful message is displayed to the submitter that makes him think that the Form has been sent so that he cannot modify the keywords to prevent SPAM
			echo '{ "alert": "success", "message": "' . $message['success'] . '" }';
			exit;
		}
	}

	/*-------------------------------------------------
		reCaptcha
	---------------------------------------------------*/

	if( isset( $submits['g-recaptcha-response'] ) && !isset( $submits['h-captcha-response'] ) ) {

		$recaptcha_data = array(
			'secret' => $recaptcha_secret,
			'response' => $submits['g-recaptcha-response']
		);

		$recap_verify = curl_init();
		curl_setopt( $recap_verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify" );
		curl_setopt( $recap_verify, CURLOPT_POST, true );
		curl_setopt( $recap_verify, CURLOPT_POSTFIELDS, http_build_query( $recaptcha_data ) );
		curl_setopt( $recap_verify, CURLOPT_SSL_VERIFYPEER, false );
		curl_setopt( $recap_verify, CURLOPT_RETURNTRANSFER, true );
		$recap_response = curl_exec( $recap_verify );

		$g_response = json_decode( $recap_response );

		if ( $g_response->success !== true ) {
			echo '{ "alert": "error", "message": "' . $message['captcha_invalid'] . '" }';
			exit;
		}
	}

	/*-------------------------------------------------
		hCaptcha
	---------------------------------------------------*/

	if( isset( $submits['h-captcha-response'] ) ) {

		$hcaptcha_data = array(
			'secret' => $hcaptcha_secret,
			'response' => $submits['h-captcha-response']
		);

		$hcap_verify = curl_init();
		curl_setopt( $hcap_verify, CURLOPT_URL, "https://hcaptcha.com/siteverify" );
		curl_setopt( $hcap_verify, CURLOPT_POST, true );
		curl_setopt( $hcap_verify, CURLOPT_POSTFIELDS, http_build_query( $hcaptcha_data ) );
		curl_setopt( $hcap_verify, CURLOPT_SSL_VERIFYPEER, false );
		curl_setopt( $hcap_verify, CURLOPT_RETURNTRANSFER, true );
		$hcap_response = curl_exec( $hcap_verify );

		$h_response = json_decode( $hcap_response );

		if ( $h_response->success !== true ) {
			echo '{ "alert": "error", "message": "' . $message['captcha_invalid'] . '" }';
			exit;
		}
	}

	$template	= !empty( $submits['template'] ) ? $submits['template'] : 'html';
	$html_title	= !empty( $submits['html_title'] ) ? $submits['html_title'] : 'Form Response';
	$forcerecap	= ( !empty( $submits['force_recaptcha'] ) && $submits['force_recaptcha'] != 'false' ) ? true : false;
	$replyto	= !empty( $submits['replyto'] ) ? explode( ',', $submits['replyto'] ) : false;

	if( $forcerecap ) {
		if( !isset( $submits['g-recaptcha-response'] ) ) {
			echo '{ "alert": "error", "message": "' . $message['captcha_error'] . '" }';
			exit;
		}
	}

	/*-------------------------------------------------
		Auto-Responders
	---------------------------------------------------*/

	$autores	= ( !empty( $submits['autoresponder'] ) && $submits['autoresponder'] != 'false' ) ? true : false;
	$ar_subject	= !empty( $submits['ar_subject'] ) ? $submits['ar_subject'] : 'Thanks for your Email';
	$ar_title	= !empty( $submits['ar_title'] ) ? $submits['ar_title'] : 'Its so good to hear from You!';
	$ar_message	= !empty( $submits['ar_message'] ) ? $submits['ar_message'] : 'Autoresponder Message';

	preg_match_all('#\{(.*?)\}#', $ar_message, $ar_matches);
	if( !empty( $ar_matches[1] ) ) {
		foreach( $ar_matches[1] as $ar_key => $ar_value ) {
			$ar_message = str_replace( '{' . $ar_value . '}' , ( is_array($submits[ $ar_value ]) ? implode( ', ', $submits[ $ar_value ] ) : $submits[ $ar_value ] ), $ar_message );
		}
	}

	$ar_footer	= !empty( $submits['ar_footer'] ) ? $submits['ar_footer'] : 'Copyrights © ' . date('Y') . ' **Allstarco**. All Rights Reserved.';

	$mail->Subject = !empty( $submits['subject'] ) ? $submits['subject'] : 'A new message from Allstarco Shool page';
	$mail->SetFrom( $fromemail['email'] , $fromemail['name'] );

	if( !empty( $replyto ) ) {
		if( count( $replyto ) > 1 ) {
			$replyto_e = $submits[ $replyto[0] ];
			$replyto_n = $submits[ $replyto[1] ];
			$mail->AddReplyTo( $replyto_e , $replyto_n );
		} elseif( count( $replyto ) == 1 ) {
			$replyto_e = $submits[ $replyto[0] ];
			$mail->AddReplyTo( $replyto_e );
		}
	}

	foreach( $toemails as $toemail ) {
		$mail->AddAddress( $toemail['email'] , $toemail['name'] );
	}

	$unsets = array( 'prefix', 'subject', 'replyto', 'template', 'html_title', 'message', 'autoresponder', 'ar_subject', 'ar_title', 'ar_message', 'ar_footer', $prefix . 'botcheck', 'g-recaptcha-response', 'h-captcha-response', 'force_recaptcha', $prefix . 'submit' );

	foreach( $unsets as $unset ) {
		unset( $submits[ $unset ] );
	}

	$fields = array();

	foreach( $submits as $name => $value ) {

		if( empty( $value ) ) continue;

		$name = str_replace( $prefix , '', $name );
		$name = function_exists('mb_convert_case') ? mb_convert_case( $name, MB_CASE_TITLE, "UTF-8" ) : ucwords($name);

		if( is_array( $value ) ) {
			$value = implode( ', ', $value );
		}

		$fields[$name] = nl2br( filter_var( $value, FILTER_SANITIZE_SPECIAL_CHARS ) );

	}

	$files = $_FILES;

	foreach( $files as $file => $filevalue ) {

		if( is_array( $filevalue['name'] ) ) {
			$filecount = count( $filevalue['name'] );

			for( $f = 0; $f IsHTML(true);
					$mail->AddAttachment( $_FILES[ $file ]['tmp_name'][ $f ], $_FILES[ $file ]['name'][ $f ] );
				}
			}
		} else {
			if ( isset( $_FILES[ $file ] ) && $_FILES[ $file ]['error'] == UPLOAD_ERR_OK ) {
				$mail->IsHTML(true);
				$mail->AddAttachment( $_FILES[ $file ]['tmp_name'], $_FILES[ $file ]['name'] );
			}
		}

	}

	$response = array();

	foreach( $fields as $fieldname => $fieldvalue ) {
		if( $template == 'text' ) {
			$response[] = $fieldname . ': ' . $fieldvalue;
		} else {
			$fieldname = '
								' . $fieldname . '
							';
			$fieldvalue = '
								' . $fieldvalue . '
							';
			$response[] = $fieldname . $fieldvalue;
		}
	}

	$referrer = $_SERVER['HTTP_REFERER'] ? '

This Form was submitted from: ' . $_SERVER['HTTP_REFERER'] : '';

	$html_before = '
				
					
						
							
								
									';

	$html_after = '
							
						
					
				
			';

	if( $template == 'text' ) {
		$body = implode( "
", $response ) . $referrer;
	} else {
		$html = $html_before . '
									
										
											
												' . $html_title . '
											
										
									

									
									
										' . implode( '', $response ) . '
									

									
									
										
											
												
													
														' . strip_tags( $referrer ) . '
													
												
											
										
									
									' . $html_after;

		$body = $html;
	}

	if( $autores && !empty( $replyto_e ) ) {
		$autoresponder = new PHPMailer();

		/* Add your Auto-Responder SMTP Codes after this Line */

		// End of Auto-Responder SMTP

		$autoresponder->SetFrom( $fromemail['email'] , $fromemail['name'] );
		if( !empty( $replyto_n ) ) {
			$autoresponder->AddAddress( $replyto_e , $replyto_n );
		} else {
			$autoresponder->AddAddress( $replyto_e );
		}
		$autoresponder->Subject = $ar_subject;

		$ar_body = $html_before . '
					
						
							
								' . $ar_title . '
							
						
					

					
					
						
							' . $ar_message . '
						
					

					
					
						
							
								
									
										' . $ar_footer . '
									
								
							
						
					
					' . $html_after;

		$autoresponder->MsgHTML( $ar_body );
		$autoresponder->CharSet = "UTF-8";
	}

	$mail->MsgHTML( $body );
	$mail->CharSet = "UTF-8";
	$sendEmail = $mail->Send();

	if( $sendEmail == true ):

		if( $autores && !empty( $replyto_e ) ) {
			$send_arEmail = $autoresponder->Send();
		}

		echo '{ "alert": "success", "message": "' . $message['success'] . '" }';
	else:
		echo '{ "alert": "error", "message": "' . $message['error'] . '

**Reason:**
' . $mail->ErrorInfo . '" }';
	endif;

} else {
	echo '{ "alert": "error", "message": "' . $message['error_unexpected'] . '" }';
}

?>
SSemicolon WebSTAFFJul 11, 2023

Hello,

Thanks for your Patience!

We have checked this out and the issue appears to be with the em6131.allstarco.swanmedia.ca SMTP Host. Please consider contacting your Hosting Server Provider and verify if this is the correct host and it exists. Also verify the Port and SMTPSecure settings as all these configs are required for the SMTP to properly send emails.

Simply update the Settings after confirming with your Hosting Providers and if the issue still exists, please let us know and we can look into this further.

Meanwhile, do let us know if we can help you with anything else or if you find any further issues with Canvas.

AassiyadesignJul 19, 2023

Thank you. I'll check it out

AassiyadesignJul 19, 2023

Another question: why can't I use the package generator if I've renewed the support?

SSemicolon WebSTAFFJul 20, 2023

Hello,

Apologies for the Inconveniences caused!

There was a cache issue with the Support Days Left with your Account but we have already fixed this. Please consider checking again and you should be able to generate Packages. Thanks for your Patience.

Meanwhile, do let us know if we can help you with anything else or if you find any further issues with Canvas.

Have the same question, or something new?

Sign in to the Canvas dashboard to reply or open your own topic. Canvas owners get direct help from the SemiColonWeb team.

Reply on the dashboard