Contact form - replace PHP mail() with SMTP Functionality

7 replies · opened Mar 17, 2022

VvollwebdesignMar 17, 2022

Hey there,

For my contact form I am still using the old quickcontact.php and PHP mail but the emails stopped sending.
So I wanna change to SMTP.

Can I still use quickcontact.php and how would I have to change it?
Currently it looks like this:

 'info@dirazi.de', // Your Email Address
			'email' => 'torsten.voll@gmail.com',
			'name' => 'Manfred Jung' // Your Name
			);
// Form Processing Messages
$message_success = 'Wir haben Ihre Nachricht soeben **erhalten** und werden uns umgehend mit Ihnen in Verbindung setzen!';
// Add this only if you use reCaptcha with your Contact Forms
$recaptcha_secret = ''; // Your reCaptcha Secret
$mail = new PHPMailer();
// If you intend you use SMTP, add your SMTP Code after this Line
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
	if( $_POST['quick-contact-form-email'] != '' ) {
		$name = $_POST['quick-contact-form-name'];
		$email = $_POST['quick-contact-form-email'];
		$message = $_POST['quick-contact-form-message'];
		$subject = 'New Message From Quick Contact Form';
		$botcheck = $_POST['quick-contact-form-botcheck'];
		if( $botcheck == '' ) {
			$mail->SetFrom( $email , $name );
			$mail->AddReplyTo( $email , $name );
			foreach( $toemails as $toemail ) {
				$mail->AddAddress( $toemail['email'] , $toemail['name'] );
			}
			$mail->Subject = $subject;

			$name = isset($name) ? "Name: $name

" : '';
			$email = isset($email) ? "E-Mail: $email

" : '';
			$message = isset($message) ? "Nachricht: $message

" : '';

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

Mitteilung über das Kontaktformular: ' . $_SERVER['HTTP_REFERER'] : '';

			$body = "$name $email $message $referrer";

			// Runs only when reCaptcha is present in the Contact Form
			if( isset( $_POST['g-recaptcha-response'] ) ) {
				$recaptcha_response = $_POST['g-recaptcha-response'];
				$response = file_get_contents( "https://www.google.com/recaptcha/api/siteverify?secret=" . $recaptcha_secret . "&response=" . $recaptcha_response );

				$g_response = json_decode( $response );

				if ( $g_response->success !== true ) {
					echo '{ "alert": "error", "message": "Captcha not Validated! Please Try Again." }';
					die;
				}
			}
			// Uncomment the following Lines of Code if you want to Force reCaptcha Validation
			// if( !isset( $_POST['g-recaptcha-response'] ) ) {
			// 	echo '{ "alert": "error", "message": "Captcha not Submitted! Please Try Again." }';
			// 	die;
			// }
			$mail->MsgHTML( $body );
			$mail->CharSet = "UTF-8";
			$sendEmail = $mail->Send();

			if( $sendEmail == true ):
				echo '{ "alert": "success", "message": "' . $message_success . '" }';
			else:
				echo '{ "alert": "error", "message": "Email **could not** be sent due to some Unexpected Error. Please Try Again later.

**Reason:**
' . $mail->ErrorInfo . '" }';
			endif;
		} else {
			echo '{ "alert": "error", "message": "Bot **Detected**.! Clean yourself Botster.!" }';
		}
	} else {
		echo '{ "alert": "error", "message": "Please **Fill up** all the Fields and Try Again." }';
	}
} else {
	echo '{ "alert": "error", "message": "An **unexpected error** occured. Please Try Again later." }';
}
?>

Cheers,
Torsten

VvollwebdesignMar 18, 2022

Can please look after this?

Now I used the form.php with the same result:
Error message:
"{ "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/Troubleshooting" }"

Those are the settings I'm using:

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "sslout.df.eu";
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->Username = "info@dirazi.de";
$mail->Password = "Password email account";

URL: https://dirazi.de/kontakt.html

Any idea what I can do to fix this?

THanks a lot,
Torsten

VvollwebdesignMar 18, 2022

I changed the settings again and got a failed mail delivery message from the host at least so looks like the old quick form is still working with PHP 7.1. Now I just need to know why the message is not delivering properly:

This message was created automatically by mail delivery software.

A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

kontaktform@dirazi.de
host dirazi-de.mail.protection.outlook.com [104.47.9.36]
SMTP error from remote mail server after RCPT TO:<kontaktform@dirazi.de>:
550 5.4.1 Recipient address rejected: Access denied. AS(201806281) [VE1EUR03FT032.eop-EUR03.prod.protection.outlook.com]
Reporting-MTA: dns; smtprelay06.ispgateway.de

Action: failed
Final-Recipient: rfc822;kontaktform@dirazi.de
Status: 5.0.0
Remote-MTA: dns; dirazi-de.mail.protection.outlook.com
Diagnostic-Code: smtp; 550 5.4.1 Recipient address rejected: Access denied. AS(201806281) [VE1EUR03FT032.eop-EUR03.prod.protection.outlook.com]
Return-path: <info@vollwebdesign.de>
Received: from [134.119.234.176] (helo=dirazi.de)
by smtprelay06.ispgateway.de with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
(Exim 4.94.2)
(envelope-from <info@vollwebdesign.de>)
id 1nVKvy-0002Vz-4p
for kontaktform@dirazi.de; Fri, 18 Mar 2022 23:18:58 +0100
Date: Fri, 18 Mar 2022 23:18:57 +0100
To: Manfred Jung <kontaktform@dirazi.de>
From: Torsten Voll <info@vollwebdesign.de>
Reply-To: Torsten Voll <info@vollwebdesign.de>
Subject: New Message From Quick Contact Form
Message-ID: <bf59f7e80abad48863c8289549a9b3eb@dirazi.de>
X-Mailer: PHPMailer 5.2.23 (https://github.com/PHPMailer/PHPMailer)
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="b1_bf59f7e80abad48863c8289549a9b3eb"
Content-Transfer-Encoding: 8bit
X-Df-Sender: a29udGFrdGZvcm1AZGlyYXppLmRl

Those are the SMTP setting in the quickcontact.php:

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "sslout.df.eu";
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->Username = "kontaktform@dirazi.de";
$mail->Password = "password";

SSemicolon WebSTAFFMar 19, 2022

Hello,

Thanks for your Kind Patience!

There some tests that might be required to make sure everything is working correctly as this appears to be a Server Issue:

  1. Make sure that $mail->Host is correctly configured.

  2. Add the .customjs Class to the .form-widget Element in the HTML Codes.

  3. Change the $mail->SMTPDebug = 0; to $mail->SMTPDebug = 2;. This will display the issues with SMTP.

  4. Make sure that the Ports are correct based on your Server Requirements.

Now, try submitting the Form and it will display issues with why SMTP is not connecting and paste the entire text here.

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.

VvollwebdesignMar 19, 2022

Hey there,

Thanks for your reply! :-)

I am not using the .form-widget but the .quick-contact-widget so I added the .customjs there. Hope that is ok?

Here is what I got:

2022-03-19 12:20:46 SERVER -> CLIENT: 220 smtprelay05.ispgateway.de ESMTP dfex 2022-03-19 12:20:46 CLIENT -> SERVER: EHLO dirazi.de 2022-03-19 12:20:46 SERVER -> CLIENT: 250-smtprelay05.ispgateway.de Hello dirazi.de [134.119.234.176] 250-SIZE 104857600 250-8BITMIME 250-PIPELINING 250-PIPE_CONNECT 250-AUTH PLAIN LOGIN 250 HELP 2022-03-19 12:20:46 CLIENT -> SERVER: AUTH LOGIN 2022-03-19 12:20:46 SERVER -> CLIENT: 334 VXNlcm5hbWU6 2022-03-19 12:20:46 CLIENT -> SERVER: a29udGFrdGZvcm1AZGlyYXppLmRl 2022-03-19 12:20:46 SERVER -> CLIENT: 334 UGFzc3dvcmQ6 2022-03-19 12:20:46 CLIENT -> SERVER: ISFNYXlhMTMxMTA4ISE= 2022-03-19 12:20:46 SERVER -> CLIENT: 235 Authentication succeeded 2022-03-19 12:20:46 CLIENT -> SERVER: MAIL FROM: 2022-03-19 12:20:46 SERVER -> CLIENT: 250 OK 2022-03-19 12:20:46 CLIENT -> SERVER: RCPT TO: 2022-03-19 12:20:46 SERVER -> CLIENT: 250 Accepted 2022-03-19 12:20:46 CLIENT -> SERVER: DATA 2022-03-19 12:20:46 SERVER -> CLIENT: 354 Enter message, ending with "." on a line by itself 2022-03-19 12:20:46 CLIENT -> SERVER: Date: Sat, 19 Mar 2022 13:20:46 +0100 2022-03-19 12:20:46 CLIENT -> SERVER: To: Manfred Jung 2022-03-19 12:20:46 CLIENT -> SERVER: From: Torsten Voll 2022-03-19 12:20:46 CLIENT -> SERVER: Reply-To: Torsten Voll 2022-03-19 12:20:46 CLIENT -> SERVER: Subject: New Message From Quick Contact Form 2022-03-19 12:20:46 CLIENT -> SERVER: Message-ID: 2022-03-19 12:20:46 CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.23 (https://github.com/PHPMailer/PHPMailer) 2022-03-19 12:20:46 CLIENT -> SERVER: MIME-Version: 1.0 2022-03-19 12:20:46 CLIENT -> SERVER: Content-Type: multipart/alternative; 2022-03-19 12:20:46 CLIENT -> SERVER: boundary="b1_c9705fe825845807268a3810d2a3d062" 2022-03-19 12:20:46 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit 2022-03-19 12:20:46 CLIENT -> SERVER: 2022-03-19 12:20:46 CLIENT -> SERVER: This is a multi-part message in MIME format. 2022-03-19 12:20:46 CLIENT -> SERVER: 2022-03-19 12:20:46 CLIENT -> SERVER: --b1_c9705fe825845807268a3810d2a3d062 2022-03-19 12:20:46 CLIENT -> SERVER: Content-Type: text/plain; charset=UTF-8 2022-03-19 12:20:46 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit 2022-03-19 12:20:46 CLIENT -> SERVER: 2022-03-19 12:20:46 CLIENT -> SERVER: Name: Torsten Voll E-Mail: info@vollwebdesign.de Nachricht: test Mitteilung über das Kontaktformular: https://dirazi.de/kontakt.html 2022-03-19 12:20:46 CLIENT -> SERVER: 2022-03-19 12:20:46 CLIENT -> SERVER: 2022-03-19 12:20:46 CLIENT -> SERVER: --b1_c9705fe825845807268a3810d2a3d062 2022-03-19 12:20:46 CLIENT -> SERVER: Content-Type: text/html; charset=UTF-8 2022-03-19 12:20:46 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit 2022-03-19 12:20:46 CLIENT -> SERVER: 2022-03-19 12:20:46 CLIENT -> SERVER: Name: Torsten Voll

E-Mail: info@vollwebdesign.de

Nachricht: test

Mitteilung über das Kontaktformular: https://dirazi.de/kontakt.html 2022-03-19 12:20:46 CLIENT -> SERVER: 2022-03-19 12:20:46 CLIENT -> SERVER: 2022-03-19 12:20:46 CLIENT -> SERVER: 2022-03-19 12:20:46 CLIENT -> SERVER: --b1_c9705fe825845807268a3810d2a3d062-- 2022-03-19 12:20:46 CLIENT -> SERVER: 2022-03-19 12:20:46 CLIENT -> SERVER: . 2022-03-19 12:20:46 SERVER -> CLIENT: 250 OK id=1nVY4f-0007oQ-2D 2022-03-19 12:20:46 CLIENT -> SERVER: QUIT 2022-03-19 12:20:46 SERVER -> CLIENT: 221 smtprelay05.ispgateway.de closing connection { "alert": "success", "message": "Wir haben Ihre Nachricht soeben erhalten und werden uns umgehend mit Ihnen in Verbindung setzen!" }

SSemicolon WebSTAFFMar 19, 2022

Hello,

Thanks for the response test. We are seeing a success message in your Reply. So the Form appears to be submitting correctly. Can you please again and confirm?

Also, once you are ready, you can simply change the $mail->SMTPDebug = 2; to $mail->SMTPDebug = 0; again.

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.

VvollwebdesignMar 19, 2022

Hey there,

Well it does sent but still cannot be delivered as I said before:

This message was created automatically by mail delivery software.

A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

kontaktform@dirazi.de
host dirazi-de.mail.protection.outlook.com [104.47.10.36]
SMTP error from remote mail server after RCPT TO:<kontaktform@dirazi.de>:
550 5.4.1 Recipient address rejected: Access denied. AS(201806281) [DB5EUR03FT046.eop-EUR03.prod.protection.outlook.com]
Reporting-MTA: dns; smtprelay01.ispgateway.de

Action: failed
Final-Recipient: rfc822;kontaktform@dirazi.de
Status: 5.0.0
Remote-MTA: dns; dirazi-de.mail.protection.outlook.com
Diagnostic-Code: smtp; 550 5.4.1 Recipient address rejected: Access denied. AS(201806281) [DB5EUR03FT046.eop-EUR03.prod.protection.outlook.com]
Return-path: <info@vollwebdesign.de>
Received: from [134.119.234.176] (helo=dirazi.de)
by smtprelay01.ispgateway.de with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
(Exim 4.94.2)
(envelope-from <info@vollwebdesign.de>)
id 1nVYqR-0003Ol-Sp
for kontaktform@dirazi.de; Sat, 19 Mar 2022 14:10:11 +0100
Date: Sat, 19 Mar 2022 14:10:05 +0100
To: Manfred Jung <kontaktform@dirazi.de>
From: Torsten Voll <info@vollwebdesign.de>
Reply-To: Torsten Voll <info@vollwebdesign.de>
Subject: New Message From Quick Contact Form
Message-ID: <55170a1feb2368d7f2f51452e97ec658@dirazi.de>
X-Mailer: PHPMailer 5.2.23 (https://github.com/PHPMailer/PHPMailer)
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="b1_55170a1feb2368d7f2f51452e97ec658"
Content-Transfer-Encoding: 8bit
X-Df-Sender: a29udGFrdGZvcm1AZGlyYXppLmRl

Any idea why?

Btw that .customjs class breaks my form. What is it for?

Cheers,
Torsten

SSemicolon WebSTAFFMar 19, 2022

Hello,

According to the Message from the Mail Client, this does appear to be an issue with the Mail Client where some kind of Filtering or Firewall is active. Consider delivering the Email to a different Email like a Gmail Account and check if the Email gets delivered because Mail Delivery Failure after a successful SMTP Submission is very very rare.

The .customjs Class is to just disable AJAX on the Form so that we can check the Mail Delivery Messages from the SMTP Server properly. You can can remove this class to enable AJAX again.

Hope this Helps!

Let us know if we can help you with anything else or if you find any further issues.

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