Issue
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 = "user@example.com";
$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+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$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
Solution
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 = 'example@domain.com'; // SMTP username
$mail->Password = 'emailpass'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, [ICODE]ssl[/ICODE] also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('example@domain.com', 'New Form submission on Rentersshield Website');
$mail->addAddress('example@domain.com', 'JohnUser'); // Add a recipient
$mail->addAddress('example@domain.com'); // Name is optional
$mail->addReplyTo('example@domain.com', 'Information');
$mail->addCC('example@domain.com');
$mail->addBCC('example@domain.com');
$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}";
}
Answered By - John Adams
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.