I am trying to send email from PHP using SMTP but every time I am getting emails in my spam. I searched on google and got some solution but still I am getting email in spam. Would you help me in this?
//$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = '***'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption,'ssl' also accepted
$mail->Port = 587; //Set the SMTP port number - 587 for authenticated TLS
$mail->setFrom('[email protected]', 'admin'); //Set who the message is to be sent from
$mail->addReplyTo('[email protected]', 'First Last'); //Set an alternative reply-to address
$mail->addAddress($to, 'user'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Hello';
$mail->Body = "<html>
<head>
<title>HTML email</title>
</head>
<body>
<a href='/changepassword.php?user_id=" .$User_id1."'>Create your password here</a>
</body>
</html>";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
Your problem is not caused by your code.
You need to make sure that the email is coming from a server that is associated with the domain you are sending from.
Every email you send needs to be signed with an SPF record as per the Sender Policy Framework in order to not end up in spam boxes.
You can usually add an SPF record to your DNS yourself.
Another thing to check is that the SMTP server you are using is not blacklisted in any way.