1

I am using PHPMAILER and I am fairly new in it.

I have used the following code but for some reason the email is being sent to spam. Please have a look at the code and tell me what I need to fix. (I am new to using email

<?php
require 'php-mailer-master/PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isMail();                                     // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'kfhcareer@gmail.com.com';                 // SMTP username
$mail->Password = 'password12345';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('kfhcareer@gmail.com', 'KFH Bahrain');
$mail->addAddress('kfhcareer@gmail.com', 'Joe User');     // Add a recipient
 $mail->AddReplyTo( 'mailer@blah.com', 'Contact BLah' );


$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'KFH house bahrain';
$mail->Body    = 'This is tthe message <b>in bold!</b>';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
ahmed
  • 41
  • 3
  • 10
  • Try removing the AddReplyTo, and keep the body free of HTML. See if your message still goes to spam. Then if that works, add HTML to the body. And if that works, add a different reply-to address. – dearsina Feb 26 '19 at 08:48
  • @dearsina this did not solve my issues unfortunatly – ahmed Feb 26 '19 at 08:56
  • View the headers of a message you receive on gmail and it will show information about why a message is classed as spam. You're also running an old version of PHPMailer, so upgrade. – Synchro Feb 26 '19 at 10:16

1 Answers1

-2
require("PHPMailer/class.phpmailer.php");
$sender = "sender@gmail.com"; //gmail of the sender
$password = "senderpassword"; //the password
$receiver = "receiver@gmail.com"; // the receiver email

$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->SMTPDebug  = 2; 
$mail->From = $sender;
$mail->FromName = $sender;
$mail->Host = "smtp.gmail.com"; // specif smtp server
$mail->SMTPSecure= "ssl"; // Used instead of TLS when only POP mail is selected
$mail->Port = 465; // Used instead of 587 when only POP mail is selected 465
$mail->SMTPAuth = true;
$mail->Username = $sender; // SMTP username
$mail->Password = $password; // SMTP password
$mail->AddAddress($receiver, $receiver); //replace myname and mypassword to yours
$mail->AddReplyTo($receiver, $receiver);
$mail->WordWrap = 50; // set word wrap


$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "This is Subject";
$mail->Body = "This is Body";

if(!$mail->Send()){
echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
  • This code will not work with recent versions of PHPMailer, and it will also do nothing to help resolve spam problems. – Synchro Feb 26 '19 at 10:15
  • Code dumps do not make for good answers. You should explain *how* and *why* this solves their problem. I recommend reading, "[How do I write a good answer?"](http://stackoverflow.com/help/how-to-answer) – John Conde Feb 26 '19 at 11:45
  • This actually helped me guys! it worked 1000% – ahmed Feb 27 '19 at 05:26
  • the email function is working but the email goes into spam box or at least marked as high-risk-email sender through gmail... any solution for this case guys? @JohnConde – gumuruh Mar 08 '22 at 08:01