I am using smtp auth then also mail going into spam folder. If the body of mail contains an external file (file_get_containts) then the mail is going in the Spam folder.
but, if the body of mail contains only string then the mail is going in the inbox folder.
Can someone please help me out with this?
here is my code:-
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
if( isset($_POST['name']) && isset($_POST['email']) && isset($_POST['phone']) && isset($_POST['message']) ){
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$m = nl2br($_POST['message']);
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'mail.example.in';
$mail->SMTPAuth = true;
$mail->Username = 'info@example.in';
$mail->Password = 'nsdfdk^^dsfx7wffdsry8e^';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->From = 'info@example.in';
$mail->FromName = 'John Smith';
$mail->addCustomHeader('MIME-Version: 1.0');
$mail->addCustomHeader('Content-Type: text/html; charset=ISO-8859-1');
$mail->addAddress('example@gmail.com', 'Jay Senghani');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = "New Enquiry from website";
$message = file_get_contents('emails/admin.html');
$patterns = array();
$patterns[0] = '/{name}/';
$patterns[1] = '/{email}/';
$patterns[2] = '/{number}/';
$patterns[3] = '/{message}/';
$replacements = array();
$replacements[0] = $name;
$replacements[1] = $email;
$replacements[2] = $phone;
$replacements[3] = $m;
$message = preg_replace($patterns, $replacements, $message);
$mail->Body = $message;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent /n';
}
}
// For User Automated Email
if( isset($_POST['name']) && isset($_POST['email']) && isset($_POST['phone'])){
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'mail.example.in';
$mail->SMTPAuth = true;
$mail->Username = 'info@example.in';
$mail->Password = 'ndfgk^dfgg^gfdggfdgdfgdfx7wfy8e^';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->From = 'info@example.in';
$mail->FromName = 'John Smith';
$mail->addAddress($email, $name);
$mail->addCustomHeader('MIME-Version: 1.0');
$mail->addCustomHeader('Content-Type: text/html; charset=ISO-8859-1');
$mail->isHTML(true);
$mail->Subject = "Thank you for your interest Website ";
// $mail->addAttachment('Attachment Path', 'pdf');
$message = file_get_contents('emails/user.html');
$message = preg_replace('/{name}/', $name, $message);
$mail->Body = $message;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent /n';
}
}
?>
Here is my admin template:-
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="emailWrapper">
<div id="emailHeader">
<div class="topBar"></div>
<a class="branding" href="http://example.com/" target="_blank" >
<img src="https://i.imgur.com/JME5efdRs.png">
</a>
</div>
<div id="emailContent">
<h2 class="greetings">
Dear Admin,
</h2>
<div class="content">
<p class="intro">
New enquiry from XYZ Website
</p>
<p>
<strong>Name :</strong> {name}
</p>
<p>
<strong>Number :</strong> {number}
</p>
<a class="email">
<strong>Email :</strong> {email}
</a>
<p>
<strong>Message :</strong> {message}
</p>
</div>
<div class="regards">
<h5><strong>Thanks & Regards,</strong></h5>
<h6>XyZ</h6>
</div>
</div> <!-- END #emailContent -->
<div id="emailFooter">
<div class="bottomBar">
<p>
© 2018 Xyz. All rights reserved
</p>
</div>
</div>
</div>
</body>
</html>
Troubleshooting email deliverability is tricky mostly due to the lack of details about the inner workings of anti-spam features at the main email service providers (Gmail, Msn, mail.com, Yahoo etc.). The main aspect of good email deliverability is usually your domain reputation. If you are just starting out with a new email domain most receiving services tend to be sceptic towards your first xx number of emails. If your email contains invalid or poor html it will up the spam score. If you add attachments to it will certainly up the score.
I figure your emails without attachments are just below the threshold for ending up in the spam folder. When adding an attachment it gets just above the same threshold. Most services also apply per-user rules, so the handling of your emails can be treated differently between receivers within the same service.
As a step in troubleshooting if PHPmailer is responsible for the poor delivery, I suggest setting up an email client like Mozilla Thunderbird and sending some emails from there. That will help you figure out the source of the deliverability of your emails.
If you want to avoid thinking about deliverability you can spin up an account with SMTP services like Mailgun.