Hi…So there’s this issue I can’t solve for a couple weeks already. I’m trying to send emails from my website’s form to my Gmail inbox, but it keeps landing up as a dangerous message and into the spam folder.
I’ve done my research and even set up the DKIM on cpanel, and the SPF settings, and telling my gmail account to accept-less-secure-apps as I’ve read from some guidelines.
Nothing has been working for me so far. Which leads me to think that there’s something wrong with my actual code and the way it’s written. Can anybody point me in the right direction?
Here’s the full code, and I’m quite certain there aren’t any typos. I think it’s just the way I’m sending the email that’s causing a problem. But I have no idea what to look for.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$msg = '';
include_once 'dbh.inc.php'; //connection to database
if (isset($_POST['confirm'])) {
$order_number = $_POST['order_number'];
$fname = mysqli_real_escape_string($conn, $_POST['fname']);
$lname = mysqli_real_escape_string($conn, $_POST['lname']);
$num = mysqli_real_escape_string($conn, $_POST['num']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$shipping = $_POST['shipping'];
$addr1 = mysqli_real_escape_string($conn, $_POST['addr1']);
$addr2 = mysqli_real_escape_string($conn, $_POST['addr2']);
$addr3 = mysqli_real_escape_string($conn, $_POST['addr3']);
$addr4 = mysqli_real_escape_string($conn, $_POST['addr4']);
$date = $_POST['date'];
$u_weight = $_POST['u_weight'];
$u_qty = $_POST['u_qty'];
$u_total = $_POST['u_total'];
$sql = "INSERT INTO orders (order_number, fname, lname, num, email, shipping, addr1, addr2, addr3, addr4, date, u_weight, u_qty, u_total) VALUES ('$order_number', '$fname', '$lname', '$num', '$email', '$shipping', '$addr1', '$addr2', '$addr3', '$addr4', '$date', '$u_weight', '$u_qty', '$u_total');";
mysqli_query($conn, $sql);
if (array_key_exists('email', $_POST)) {
date_default_timezone_set('Etc/UTC');
//Load Composer's autoloader
require '../mailer/vendor/autoload.php';
$body = "message body here";
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
//$mail->SMTPDebug = 1; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.fishgelatine.co.za'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'info@fishgelatine.co.za'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls';
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('info@fishgelatine.co.za', 'Sir Francis Fish Gelatine'); //from address must be yourself! otherwise will land in spam folder as forgery
$mail->addAddress('info@fishgelatine.co.za', 'Sir Francis Fish Gelatine'); // Add a recipient
$mail->addAddress('moosagiea@gmail.com', 'Asma Moosagie'); // add more than 1 person
//Content
if ($mail->addReplyTo($_POST['email'], $_POST['fname'].' '.$_POST['lname'])) {
$mail->Subject = "Order #".$order_number." received on FishGelatine";
//Keep it simple - don't use HTML
$mail->isHTML(true);
//Build a simple message body
$mail->AddEmbeddedImage('../img/header.png', 'emailheader');
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
$mail->DKIM_domain = "fishgelatine.co.za";
$mail->DKIM_private = "mailer/rsa.private"; //path to file on the disk.
$mail->DKIM_selector = "default"; //use public key in mailer folder and update in CPanel
$mail->DKIM_passphrase = "";
$mail->DKIM_identify = $mail->From;
if (!$mail->send()) {
//The reason for failing to send will be in $mail->ErrorInfo
//but you shouldn't display errors to users - process the error, log it on your server.
$msg = 'Sorry, something went wrong. Please try again later.';
} else {
header("Location: ../thankyou.php?date=".$_POST['date']."&order=".$_POST['order_number']."&product=".$_POST['u_weight']."&qty=".$_POST['u_qty']."&total=".$_POST['u_total']."&fname=".$_POST['fname']."&lname=".$_POST['lname']."&delivery=".$_POST['shipping']."&addr1=".$_POST['addr1']."&addr2=".$_POST['addr2']."&addr3=".$_POST['addr3']."&addr4=".$_POST['addr4']);
exit();
}
} else {
$msg = 'Invalid email address, message ignored.';
}
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}
} else {
header("Location: ../order.php?error");
exit();
}