We have a HTML form that collect data and submit to a PHP page that send the form data to our email. The issue is that we are using AWS server and AWS has a block on port 25 which the result the email not sending properly. Our situation now when we send the form to a regular Gmail account the email goes to spam, however when we send it to a Gsuite account the email never received not even as a spam email. We are thinking of just using SMTP to send our email without going to spam or not receiving it.
Our HTML Code:
<form name='form1' action="/wp-includes/phpmailer2/sendMail.php" >
<input id="first_name" name="first_name" required="required" type="text" value="" placeholder="" >
</form>
PHP Code:
<?php
$webmaster_email = "[email protected]";
$feedback_page = "feedback_form.html";
$error_page = "error_message.html";
$thankyou_page = "https://rentersshield.org/success/";
$msg = "First Name: " . $first_name . "\r\n" .
function isInjected($str) {
$injections = array('(\n )',
'(\r )',
'(\t )',
'(
)',
'(
)',
'( )',
'( )'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str)) {
return true;
} else {
return false;
}
}
if (!isset($_REQUEST['first_name'])) {
header( "Location: $feedback_page" );
} elseif (empty($first_name) ) {
header( "Location: $error_page" );
} elseif ( isInjected($first_name) || isInjected($comments) ) {
header( "Location: $error_page" );
} else {
mail( "$webmaster_email", "New Form Submission", $msg );
header( "Location: $thankyou_page" );
}
?>
$first_name = $_REQUEST['first_name'] ;
Please let us know if there is something to be done to send it to our Gsuite email account without going into spam
CodePudding user response:
I would suggest you take a look at PHPMailer or any SMTP class where you have more control over authentication and server responses. The php function mail is too basic.
PHPMailer is a classic, and it works quite well on all my applications. Here is a link if you might want to take a look:
https://github.com/PHPMailer/PHPMailer
The documentaton is quite complete so you would be able to implement it without no problems.
CodePudding user response:
We only managed to fix our issue by sending it via SMTP but we have to install PHPmailer first on cPanel using the following command composer require phpmailer/phpmailer
Instructions: https://muftsabazaar.com/blog/post/how-to-install-the-phpmailer-in-cpanel Then we updated our PHP code as below
<?php
use phpmailer\phpmailer\PHPMailer;
use phpmailer\phpmailer\Exception;
require '/home/rentersshield/vendor/phpmailer/phpmailer/src/Exception.php';
require '/home/rentersshield/vendor/phpmailer/phpmailer/src/PHPMailer.php';
require '/home/rentersshield/vendor/phpmailer/phpmailer/src/SMTP.php';
// Instantiation and passing [ICODE]true[/ICODE] enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'aaa@wa2020!'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, [ICODE]ssl[/ICODE] also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('[email protected]', 'New Form submission on Rentersshield Website');
$mail->addAddress('[email protected]', 'JohnUser'); // Add a recipient
$mail->addAddress('[email protected]'); // Name is optional
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');
$feedback_page = "feedback_form.html";
$error_page = "error_message.html";
$thankyou_page = "https://rentersshield.org/success/";
// Content
$first_name = $_REQUEST['first_name'] ;
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body =
"First Name: " . $first_name . "\r\n" ;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
header( "Location: $thankyou_page" );
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}