Sending email from PHP using SMTP and email goes to spam.What will be easiest way to send email to inbox?

4.9k views Asked by At

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;
}
2

There are 2 answers

3
darryn.ten On

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.

0
Sona On
// Use phpmailer library from github install and use
require_once('PHPMailer/PHPMailerAutoload.php');
if(isset($_REQUEST['submit']))
{
    $mail = new PHPMailer(); // defaults to using php "mail()"

    $body = "Name : ".$_REQUEST['name']."<br> Email Id ".$_REQUEST['email']."<br> message ".$_REQUEST['message'];

    $mail->IsSMTP();                                      // set mailer to use SMTP
    $mail->SMTPAuth = true; // authentication enabled
    $mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
    $mail->Host = "mail.xx.co";
    $mail->Port = 25; 
    $mail->Username = "[email protected]";  // SMTP username
    $mail->Password = "xxxxxxxxxx"; // SMTP password
    $mail->SetFrom('[email protected]',$_REQUEST['subject']);
    $mail->AddAddress('[email protected]', $_REQUEST['name']);
    $mail->IsHTML(true);                                  // set email format to HTML
    $mail->Subject = $_REQUEST['subject'];
    $mail->Body    = $body;

    if(!$mail->Send()) {
      echo '<strong>Email</strong> sent failed.';
    } else {
      echo '
        <strong>Email</strong> s`enter code here`ent successfully.';
    }
}

// get smtp host detail from the cpanel